diff --git a/src/main/java/com/teragrep/cfe18/CaptureMetaMapper.java b/src/main/java/com/teragrep/cfe18/CaptureMetaMapper.java index 9b1f59e..75b77f1 100644 --- a/src/main/java/com/teragrep/cfe18/CaptureMetaMapper.java +++ b/src/main/java/com/teragrep/cfe18/CaptureMetaMapper.java @@ -46,6 +46,7 @@ package com.teragrep.cfe18; import com.teragrep.cfe18.handlers.entities.CaptureMeta; +import com.teragrep.cfe18.handlers.entities.CaptureRelp; import org.apache.ibatis.annotations.Mapper; import java.util.List; @@ -63,4 +64,5 @@ CaptureMeta addNewCaptureMeta( CaptureMeta deleteCaptureMeta(int capture_id); + List getCaptureMetaByKeyValue(String key, String value); } diff --git a/src/main/java/com/teragrep/cfe18/handlers/CaptureMetaController.java b/src/main/java/com/teragrep/cfe18/handlers/CaptureMetaController.java index 127d48f..9c537e7 100644 --- a/src/main/java/com/teragrep/cfe18/handlers/CaptureMetaController.java +++ b/src/main/java/com/teragrep/cfe18/handlers/CaptureMetaController.java @@ -47,6 +47,7 @@ import com.teragrep.cfe18.CaptureMetaMapper; import com.teragrep.cfe18.handlers.entities.CaptureMeta; +import com.teragrep.cfe18.handlers.entities.CaptureRelp; import com.teragrep.cfe18.handlers.entities.Flow; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; @@ -201,4 +202,34 @@ public ResponseEntity removeCaptureMeta(@PathVariable("capture_id") int } + // Key value fetch + @RequestMapping(path="/{key}/{value}",method= RequestMethod.GET, produces="application/json") + @Operation(summary = "Fetch capture definitions by key and value") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "Found the capture definitions", + content = {@Content(mediaType = "application/json", + schema = @Schema(implementation = CaptureMeta.class))}), + @ApiResponse(responseCode = "400", description = "Capture meta key or value does not exist", + content = @Content) + }) + public ResponseEntity getApplicationMetaKeyValue(@PathVariable("key") String key, @PathVariable("value") String value) { + try { + // Using CaptureRelp is sufficient here since we are only returning capture definition details. + List am = captureMetaMapper.getCaptureMetaByKeyValue(key,value); + return new ResponseEntity<>(am, HttpStatus.OK); + } catch(Exception ex){ + JSONObject jsonErr = new JSONObject(); + 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 KEY or VALUE does not exist with given parameters"); + return new ResponseEntity<>(jsonErr.toString(), HttpStatus.BAD_REQUEST); + } + } + return new ResponseEntity<>("Unexpected error", HttpStatus.INTERNAL_SERVER_ERROR); + } + } + } diff --git a/src/main/resources/com/teragrep/cfe18/CaptureMetaMapper.xml b/src/main/resources/com/teragrep/cfe18/CaptureMetaMapper.xml index b56a3bf..33b81f8 100644 --- a/src/main/resources/com/teragrep/cfe18/CaptureMetaMapper.xml +++ b/src/main/resources/com/teragrep/cfe18/CaptureMetaMapper.xml @@ -45,5 +45,16 @@ - + + + + + + + + + + diff --git a/src/main/resources/db/migration/R__Procedure_Retrieve_Capture_meta_key_value.sql b/src/main/resources/db/migration/R__Procedure_Retrieve_Capture_meta_key_value.sql new file mode 100644 index 0000000..a5e925f --- /dev/null +++ b/src/main/resources/db/migration/R__Procedure_Retrieve_Capture_meta_key_value.sql @@ -0,0 +1,86 @@ + /* + * 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 . + * + * + * 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. + */ + use cfe_18; +DELIMITER // +CREATE OR REPLACE PROCEDURE retrieve_capture_meta_key_value(meta_key varchar(1024),meta_value varchar(1024)) +BEGIN + DECLARE EXIT HANDLER FOR SQLEXCEPTION + BEGIN + ROLLBACK; + RESIGNAL; + END; + START TRANSACTION; + -- check for existence of capture meta value before attempting retrieval. Throws custom error. + if((select count(cmk.meta_key_name) from cfe_18.capture_meta_key cmk where cmk.meta_key_name=meta_key)=0) then + -- standardized JSON error response + SELECT JSON_OBJECT('id', 0, 'message', 'Capture meta KEY does not exist with given KEY value') into @nokey; + signal sqlstate '42000' set message_text = @nokey; + -- check for existence of capture meta key before attempting retrieval. Throws custom error. + elseif((select count(cm.meta_value) from cfe_18.capture_meta cm where cm.meta_value=meta_value)=0) then + -- standardized JSON error response + SELECT JSON_OBJECT('id', 0, 'message', 'Capture meta VALUE does not exist with given meta VALUE') into @nokey; + signal sqlstate '42000' set message_text = @nokey; + end if; + -- return list of capture_definitions which are linked to the given key value pair. + select cd.id as capture_id, + t.tag as tag, + cS.captureSourceType as sourcetype, + a.app as application, + cI.captureIndex as captureIndex + from cfe_18.capture_definition cd + inner join capture_meta c on cd.id = c.capture_id + inner join capture_meta_key cmk on c.meta_key_id = cmk.meta_key_id + inner join tags t on cd.tag_id=t.id + inner join captureSourcetype cS on cd.captureSourcetype_id = cS.id + inner join application a on cd.application_id = a.id + inner join captureIndex cI on cd.captureIndex_id = cI.id + WHERE c.meta_value=meta_value AND + cmk.meta_key_name=meta_key; + + COMMIT; +END; +// +DELIMITER ; \ No newline at end of file diff --git a/src/test/java/com/teragrep/cfe18/controllerTests/CaptureMetaControllerTest.java b/src/test/java/com/teragrep/cfe18/controllerTests/CaptureMetaControllerTest.java index efb442e..b7921cb 100644 --- a/src/test/java/com/teragrep/cfe18/controllerTests/CaptureMetaControllerTest.java +++ b/src/test/java/com/teragrep/cfe18/controllerTests/CaptureMetaControllerTest.java @@ -388,8 +388,97 @@ public void testGetAllCaptureMetas() { assertEquals(json, responseStringGet); } + @Test @Order(6) + public void testCaptureMetaKeyValue() { + // expected capture + ArrayList expected = new ArrayList<>(); + CaptureRelp captureRelp = new CaptureRelp(); + captureRelp.setId(1); + captureRelp.setTag("relpTag"); + captureRelp.setApplication("relp"); + captureRelp.setIndex("audit_relp"); + captureRelp.setSource_type("relpsource1"); + expected.add(captureRelp); + String jsonFile = gson.toJson(expected); + + // Fetching capture definition via key value pair from capture meta + HttpGet requestGet = new HttpGet("http://localhost:" + port + "/capture/meta/relpKey2/relpValue2"); + + requestGet.setHeader("Authorization", "Bearer " + token); + + HttpResponse responseGet = Assertions.assertDoesNotThrow(() -> HttpClientBuilder.create().build().execute(requestGet)); + + HttpEntity entityGet = responseGet.getEntity(); + + String responseStringGet = Assertions.assertDoesNotThrow(() -> EntityUtils.toString(entityGet, "UTF-8")); + + // Assertions + assertEquals(jsonFile, responseStringGet); + assertThat( + responseGet.getStatusLine().getStatusCode(), + equalTo(HttpStatus.SC_OK)); + + } + @Test + @Order(7) + public void testCaptureMetaKeyValueNoKey() { + // Fetching capture definition via key value pair from capture meta + HttpGet requestGet = new HttpGet("http://localhost:" + port + "/capture/meta/missingKey/relpValue2"); + + requestGet.setHeader("Authorization", "Bearer " + token); + + HttpResponse responseGet = Assertions.assertDoesNotThrow(() -> HttpClientBuilder.create().build().execute(requestGet)); + + HttpEntity entityGet = responseGet.getEntity(); + + String responseStringGet = Assertions.assertDoesNotThrow(() -> EntityUtils.toString(entityGet, "UTF-8")); + + // Parsin respponse as JSONObject + JSONObject responseAsJson = Assertions.assertDoesNotThrow(() -> new JSONObject(responseStringGet)); + + // Creating string from Json that was given as a response + String actual = Assertions.assertDoesNotThrow(() -> responseAsJson.get("message").toString()); + + String expected = "Capture meta KEY or VALUE does not exist with given parameters"; + // Assertions + assertEquals(expected, actual); + assertThat( + responseGet.getStatusLine().getStatusCode(), + equalTo(HttpStatus.SC_BAD_REQUEST)); + + } + @Test + @Order(8) + public void testCaptureMetaKeyValueNoValue() { + // Fetching capture definition via key value pair from capture meta + HttpGet requestGet = new HttpGet("http://localhost:" + port + "/capture/meta/relpKey2/missingValue"); + + requestGet.setHeader("Authorization", "Bearer " + token); + + HttpResponse responseGet = Assertions.assertDoesNotThrow(() -> HttpClientBuilder.create().build().execute(requestGet)); + + HttpEntity entityGet = responseGet.getEntity(); + + String responseStringGet = Assertions.assertDoesNotThrow(() -> EntityUtils.toString(entityGet, "UTF-8")); + + // Parsin respponse as JSONObject + JSONObject responseAsJson = Assertions.assertDoesNotThrow(() -> new JSONObject(responseStringGet)); + + // Creating string from Json that was given as a response + String actual = Assertions.assertDoesNotThrow(() -> responseAsJson.get("message").toString()); + + String expected = "Capture meta KEY or VALUE does not exist with given parameters"; + // Assertions + assertEquals(expected, actual); + assertThat( + responseGet.getStatusLine().getStatusCode(), + equalTo(HttpStatus.SC_BAD_REQUEST)); + + } + @Test + @Order(9) public void testDeleteCaptureMeta() { HttpDelete delete = new HttpDelete("http://localhost:" + port + "/capture/meta/"+1); @@ -416,7 +505,7 @@ public void testDeleteCaptureMeta() { } @Test - @Order(7) + @Order(10) public void testDeleteNonExistentCaptureMeta() { HttpDelete delete = new HttpDelete("http://localhost:" + port + "/capture/meta/"+122); @@ -441,4 +530,5 @@ public void testDeleteNonExistentCaptureMeta() { assertThat(deleteResponse.getStatusLine().getStatusCode(), equalTo(HttpStatus.SC_BAD_REQUEST)); assertEquals(expected, actual); } + }