-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* implement capture meta * Changes for pull request #68 * Now returns capture_id as well * Get all and Remove capture meta procedures * Complete endpoints for Capture meta feature * E2E tests for new endpoints on Capture Meta * Procedure change to return last inserted ID without extra variable assigning * No longer defaults to 0 when returning ID on error * Removed exception signature from tests
- Loading branch information
1 parent
a7b6b09
commit edf191d
Showing
12 changed files
with
1,209 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Main data management system (MDMS) cfe_18 | ||
* Copyright (C) 2021 Suomen Kanuuna Oy | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>. | ||
* | ||
* | ||
* Additional permission under GNU Affero General Public License version 3 | ||
* section 7 | ||
* | ||
* If you modify this Program, or any covered work, by linking or combining it | ||
* with other code, such other code is not for that reason alone subject to any | ||
* of the requirements of the GNU Affero GPL version 3 as long as this Program | ||
* is the same Program as licensed from Suomen Kanuuna Oy without any additional | ||
* modifications. | ||
* | ||
* Supplemented terms under GNU Affero General Public License version 3 | ||
* section 7 | ||
* | ||
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified | ||
* versions must be marked as "Modified version of" The Program. | ||
* | ||
* Names of the licensors and authors may not be used for publicity purposes. | ||
* | ||
* No rights are granted for use of trade names, trademarks, or service marks | ||
* which are in The Program if any. | ||
* | ||
* Licensee must indemnify licensors and authors for any liability that these | ||
* contractual assumptions impose on licensors and authors. | ||
* | ||
* To the extent this program is licensed as part of the Commercial versions of | ||
* Teragrep, the applicable Commercial License may apply to this file if you as | ||
* a licensee so wish it. | ||
*/ | ||
package com.teragrep.cfe18; | ||
|
||
import com.teragrep.cfe18.handlers.entities.CaptureMeta; | ||
import org.apache.ibatis.annotations.Mapper; | ||
|
||
import java.util.List; | ||
|
||
@Mapper | ||
public interface CaptureMetaMapper { | ||
CaptureMeta addNewCaptureMeta( | ||
int capture_id, | ||
String capture_meta_key, | ||
String capture_meta_value); | ||
|
||
List<CaptureMeta> getCaptureMeta(int capture_id); | ||
|
||
List<CaptureMeta> getAllCaptureMetas(); | ||
|
||
CaptureMeta deleteCaptureMeta(int capture_id); | ||
|
||
} |
204 changes: 204 additions & 0 deletions
204
src/main/java/com/teragrep/cfe18/handlers/CaptureMetaController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
/* | ||
* Main data management system (MDMS) cfe_18 | ||
* Copyright (C) 2021 Suomen Kanuuna Oy | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>. | ||
* | ||
* | ||
* Additional permission under GNU Affero General Public License version 3 | ||
* section 7 | ||
* | ||
* If you modify this Program, or any covered work, by linking or combining it | ||
* with other code, such other code is not for that reason alone subject to any | ||
* of the requirements of the GNU Affero GPL version 3 as long as this Program | ||
* is the same Program as licensed from Suomen Kanuuna Oy without any additional | ||
* modifications. | ||
* | ||
* Supplemented terms under GNU Affero General Public License version 3 | ||
* section 7 | ||
* | ||
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified | ||
* versions must be marked as "Modified version of" The Program. | ||
* | ||
* Names of the licensors and authors may not be used for publicity purposes. | ||
* | ||
* No rights are granted for use of trade names, trademarks, or service marks | ||
* which are in The Program if any. | ||
* | ||
* Licensee must indemnify licensors and authors for any liability that these | ||
* contractual assumptions impose on licensors and authors. | ||
* | ||
* To the extent this program is licensed as part of the Commercial versions of | ||
* Teragrep, the applicable Commercial License may apply to this file if you as | ||
* a licensee so wish it. | ||
*/ | ||
package com.teragrep.cfe18.handlers; | ||
|
||
import com.teragrep.cfe18.CaptureMetaMapper; | ||
import com.teragrep.cfe18.handlers.entities.CaptureMeta; | ||
import com.teragrep.cfe18.handlers.entities.Flow; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import org.json.JSONObject; | ||
import org.mybatis.spring.SqlSessionTemplate; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.SQLException; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping(path="/capture/meta") | ||
@SecurityRequirement(name="api") | ||
public class CaptureMetaController { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(CaptureMetaController.class); | ||
|
||
@Autowired | ||
DataSource dataSource; | ||
|
||
@Autowired | ||
SqlSessionTemplate sqlSessionTemplate; | ||
|
||
@Autowired | ||
CaptureMetaMapper captureMetaMapper; | ||
|
||
|
||
@RequestMapping(path="/{capture_id}",method= RequestMethod.GET, produces="application/json") | ||
@Operation(summary = "Fetch capture meta by capture id") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "Found the capture meta", | ||
content = {@Content(mediaType = "application/json", | ||
schema = @Schema(implementation = CaptureMeta.class))}), | ||
@ApiResponse(responseCode = "400", description = "Capture meta does not exist", | ||
content = @Content) | ||
}) | ||
public ResponseEntity<?> getApplicationMeta(@PathVariable("capture_id") int capture_id) { | ||
try { | ||
List<CaptureMeta> am = captureMetaMapper.getCaptureMeta(capture_id); | ||
return new ResponseEntity<>(am, HttpStatus.OK); | ||
} catch(Exception ex){ | ||
JSONObject jsonErr = new JSONObject(); | ||
jsonErr.put("id", capture_id); | ||
final Throwable cause = ex.getCause(); | ||
if (cause instanceof SQLException) { | ||
LOGGER.error((cause).getMessage()); | ||
String state = ((SQLException) cause).getSQLState(); | ||
if (state.equals("42000")) { | ||
jsonErr.put("message", "Capture meta does not exist with given ID"); | ||
return new ResponseEntity<>(jsonErr.toString(), HttpStatus.BAD_REQUEST); | ||
} | ||
} | ||
return new ResponseEntity<>("Unexpected error", HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
||
@RequestMapping(path="/",method=RequestMethod.PUT,produces="application/json") | ||
@Operation(summary = "Insert new capture meta for capture") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "201", description = "Capture meta created for capture", | ||
content = {@Content(mediaType = "application/json", | ||
schema = @Schema(implementation = CaptureMeta.class))}), | ||
@ApiResponse(responseCode = "400", description = "Capture does not exist for inserting metadata", | ||
content = @Content), | ||
@ApiResponse(responseCode = "500", description = "Internal server error, contact admin", content = @Content) | ||
}) | ||
public ResponseEntity<String> newCaptureMeta(@RequestBody CaptureMeta newCaptureMeta){ | ||
LOGGER.info("About to insert <[{}]>", newCaptureMeta); | ||
JSONObject jsonErr = new JSONObject(); | ||
jsonErr.put("id", newCaptureMeta.getCapture_id()); | ||
try { | ||
CaptureMeta cm = captureMetaMapper.addNewCaptureMeta( | ||
newCaptureMeta.getCapture_id(), | ||
newCaptureMeta.getCapture_meta_key(), | ||
newCaptureMeta.getCapture_meta_value() | ||
); | ||
LOGGER.debug("Values returned <[{}]>",cm); | ||
JSONObject jsonObject = new JSONObject(); | ||
jsonObject.put("id", cm.getCapture_id()); | ||
jsonObject.put("message", "New capture meta created for = " + cm.getCapture_id()); | ||
return new ResponseEntity<>(jsonObject.toString(), HttpStatus.CREATED); | ||
} catch (Exception ex) { | ||
final Throwable cause = ex.getCause(); | ||
if (cause instanceof SQLException) { | ||
LOGGER.error((cause).getMessage()); | ||
String state = ((SQLException) cause).getSQLState(); | ||
if (state.equals("42000")) { | ||
jsonErr.put("message", "Capture does not exist"); | ||
return new ResponseEntity<>(jsonErr.toString(), HttpStatus.BAD_REQUEST); | ||
} | ||
} | ||
return new ResponseEntity<>("Unexpected error", HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
||
@RequestMapping(path = "", method = RequestMethod.GET, produces = "application/json") | ||
@Operation(summary = "Fetch all capture metas", description = "Will return empty list if there are no capture metas to fetch") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "Capture metas fetched", | ||
content = {@Content(mediaType = "application/json", | ||
schema = @Schema(implementation = CaptureMeta.class))}) | ||
}) | ||
public List<CaptureMeta> getAllCaptureMetas(){ | ||
return captureMetaMapper.getAllCaptureMetas(); | ||
} | ||
|
||
// Delete | ||
@RequestMapping(path = "/{capture_id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE) | ||
@Operation(summary = "Delete capture meta") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "Capture meta deleted", | ||
content = {@Content(mediaType = "application/json", | ||
schema = @Schema(implementation = CaptureMeta.class))}), | ||
@ApiResponse(responseCode = "400", description = "Capture meta does not exist", | ||
content = @Content), | ||
@ApiResponse(responseCode = "500", description = "Internal server error, contact admin", content = @Content) | ||
}) | ||
public ResponseEntity<String> removeCaptureMeta(@PathVariable("capture_id") int capture_id) { | ||
LOGGER.info("Deleting Capture meta <[{}]>",capture_id); | ||
try { | ||
captureMetaMapper.deleteCaptureMeta(capture_id); | ||
JSONObject j = new JSONObject(); | ||
j.put("id", capture_id); | ||
j.put("message", "capture meta " + capture_id + " deleted."); | ||
return new ResponseEntity<>(j.toString(), HttpStatus.OK); | ||
} catch (Exception ex) { | ||
JSONObject jsonErr = new JSONObject(); | ||
jsonErr.put("id", capture_id); | ||
final Throwable cause = ex.getCause(); | ||
if (cause instanceof SQLException) { | ||
LOGGER.error((cause).getMessage()); | ||
String state = ((SQLException) cause).getSQLState(); | ||
if (state.equals("45000")) { | ||
jsonErr.put("message", "Record does not exist"); | ||
} | ||
return new ResponseEntity<>(jsonErr.toString(), HttpStatus.BAD_REQUEST); | ||
} | ||
return new ResponseEntity<>("Unexpected error", HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
} | ||
|
||
|
||
} |
87 changes: 87 additions & 0 deletions
87
src/main/java/com/teragrep/cfe18/handlers/entities/CaptureMeta.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Main data management system (MDMS) cfe_18 | ||
* Copyright (C) 2021 Suomen Kanuuna Oy | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>. | ||
* | ||
* | ||
* Additional permission under GNU Affero General Public License version 3 | ||
* section 7 | ||
* | ||
* If you modify this Program, or any covered work, by linking or combining it | ||
* with other code, such other code is not for that reason alone subject to any | ||
* of the requirements of the GNU Affero GPL version 3 as long as this Program | ||
* is the same Program as licensed from Suomen Kanuuna Oy without any additional | ||
* modifications. | ||
* | ||
* Supplemented terms under GNU Affero General Public License version 3 | ||
* section 7 | ||
* | ||
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified | ||
* versions must be marked as "Modified version of" The Program. | ||
* | ||
* Names of the licensors and authors may not be used for publicity purposes. | ||
* | ||
* No rights are granted for use of trade names, trademarks, or service marks | ||
* which are in The Program if any. | ||
* | ||
* Licensee must indemnify licensors and authors for any liability that these | ||
* contractual assumptions impose on licensors and authors. | ||
* | ||
* To the extent this program is licensed as part of the Commercial versions of | ||
* Teragrep, the applicable Commercial License may apply to this file if you as | ||
* a licensee so wish it. | ||
*/ | ||
package com.teragrep.cfe18.handlers.entities; | ||
|
||
public class CaptureMeta { | ||
|
||
public int capture_id; | ||
public String capture_meta_key; | ||
public String capture_meta_value; | ||
|
||
public int getCapture_id() { | ||
return capture_id; | ||
} | ||
|
||
public void setCapture_id(int capture_id) { | ||
this.capture_id = capture_id; | ||
} | ||
|
||
|
||
public String getCapture_meta_key() { | ||
return capture_meta_key; | ||
} | ||
|
||
public void setCapture_meta_key(String capture_meta_key) { | ||
this.capture_meta_key = capture_meta_key; | ||
} | ||
|
||
public String getCapture_meta_value() { | ||
return capture_meta_value; | ||
} | ||
|
||
public void setCapture_meta_value(String capture_meta_value) { | ||
this.capture_meta_value = capture_meta_value; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "CaptureMeta{" + | ||
"capture_id=" + capture_id + | ||
", capture_meta_key='" + capture_meta_key + '\'' + | ||
", capture_meta_value='" + capture_meta_value + '\'' + | ||
'}'; | ||
} | ||
} |
Oops, something went wrong.