diff options
author | Niranjana <niranjana.y60@wipro.com> | 2021-09-14 10:59:23 +0000 |
---|---|---|
committer | Niranjana Y <niranjana.y60@wipro.com> | 2021-09-17 10:50:01 +0000 |
commit | b2e2c6688737230608c3dd8e14e1c09d2b54c86d (patch) | |
tree | 8a5e82f74ed7dfef5e5044f23a6039586f5e38f4 | |
parent | 76344df7812bfc4feed27d218743448c35373992 (diff) |
Support delete query and dynamic anchor
Issue-ID: CPS-661
Signed-off-by: Niranjana <niranjana.y60@wipro.com>
Change-Id: I720c348d49c53573ef8897d02d641e53232fc941
4 files changed, 123 insertions, 6 deletions
diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/client/CpsRestClient.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/client/CpsRestClient.java index 8bf0bed..c54355f 100644 --- a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/client/CpsRestClient.java +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/client/CpsRestClient.java @@ -47,7 +47,7 @@ public class CpsRestClient { private static final String POST_API_PATH = "/anchors/{anchor}/nodes"; - private static final String LIST_NODE_API_PATH = "/anchors/{anchor}/list-node"; + private static final String LIST_NODE_API_PATH = "/anchors/{anchor}/list-nodes"; @Autowired private RestTemplate restTemplate; @@ -154,6 +154,49 @@ public class CpsRestClient { } } + /** + * Delete data from the CPS using xpath. + * + * @param anchor anchor + * @param xpath xpath query + * @return result Response string from CPS + */ + public String deleteData(final String anchor, final String xpath, + final String requestType) throws CpsClientException { + final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>(); + queryParams.add("xpath", xpath); + final CpsConfiguration cpsConfiguration = "cpsCore".equals(appConfiguration.getCpsClient()) + ? appConfiguration.getCpsCoreConfiguration() : appConfiguration.getNcmpConfiguration(); + + String uri = buildCpsUrl(cpsConfiguration.getUrl(), POST_API_PATH, anchor, queryParams); + + final HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); + headers.setBasicAuth(cpsConfiguration.getUsername(), cpsConfiguration.getPassword()); + final HttpEntity<String> entity = new HttpEntity<>(headers); + + ResponseEntity<String> responseEntity = null; + try { + if ("delete-list-node".equalsIgnoreCase(requestType)) { + uri = buildCpsUrl(cpsConfiguration.getUrl(), LIST_NODE_API_PATH, anchor, queryParams); + responseEntity = restTemplate.exchange(uri, HttpMethod.DELETE, entity, String.class); + } else { + responseEntity = restTemplate.exchange(uri, HttpMethod.DELETE, entity, String.class); + } + } catch (final Exception e) { + throw new CpsClientException(e.getLocalizedMessage()); + } + + final int statusCode = responseEntity.getStatusCodeValue(); + + if (statusCode == 204) { + return "Success"; + } else { + throw new CpsClientException( + String.format("Response code from CPS other than 204: %d", statusCode)); + } + } + private String buildCpsUrl(final String baseUrl, final String path, final String anchor, final MultiValueMap<String, String> queryParams) { diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogic.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogic.java index a8004be..5b3bc60 100644 --- a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogic.java +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogic.java @@ -76,7 +76,7 @@ public class ExecutionBusinessLogic { executionRequest.getPayload()); } else { return execute(templateOptional.get(), executionRequest.getInputParameters(), - executionRequest.getPayload()); + executionRequest.getPayload(), schemaSet); } } throw new TemplateNotFoundException("Template does not exist"); @@ -98,7 +98,8 @@ public class ExecutionBusinessLogic { final List<String> transformParamList = new ArrayList<String>( Arrays.asList(multipleQueryTemplate.get().getTransformParam().split("\\s*,\\s*"))); final String inputKey = transformParamList.get(transformParamList.size() - 1); - final String queryParamString = execute(multipleQueryTemplate.get(), inputParameters, payload); + final String queryParamString = execute(multipleQueryTemplate.get(), inputParameters, + payload, template.getModel()); final List<String> queryParamList = new ArrayList<String>(); final JsonParser jsonParser = new JsonParser(); final Gson gson = new Gson(); @@ -115,7 +116,7 @@ public class ExecutionBusinessLogic { queryParamList.forEach(queryParam -> { final Map<String, String> inputParameter = new HashMap<String, String>(); inputParameter.put(inputKey, queryParam); - final Object result = execute(template, inputParameter, payload); + final Object result = execute(template, inputParameter, payload, template.getModel()); processedQueryOutput.add(result); }); } catch (final Exception e) { @@ -126,9 +127,10 @@ public class ExecutionBusinessLogic { } private String execute(final Template template, final Map<String, String> inputParameters, - final Map<String, Object> payload) { + final Map<String, Object> payload, final String schemaSet) { - final String anchor = appConfiguration.getSchemaToAnchor().get(template.getModel()); + final String anchor = "dynamic".equalsIgnoreCase(template.getModel()) + ? schemaSet : appConfiguration.getSchemaToAnchor().get(template.getModel()); if (anchor == null) { throw new ExecuteException("Anchor not found for the schema"); } @@ -138,6 +140,9 @@ public class ExecutionBusinessLogic { || "post".equalsIgnoreCase(template.getRequestType()) || "post-list-node".equalsIgnoreCase(template.getRequestType())) { return cpsRestClient.addData(anchor, xpath, template.getRequestType(), payload); + } else if ("delete".equalsIgnoreCase(template.getRequestType()) + || "delete-list-node".equalsIgnoreCase(template.getRequestType())) { + return cpsRestClient.deleteData(anchor, xpath, template.getRequestType()); } else { final String result = cpsRestClient.fetchNode(anchor, xpath, template.getRequestType(), template.getIncludeDescendants()); diff --git a/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/client/CpsRestClientTest.java b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/client/CpsRestClientTest.java index 4697479..76033c9 100644 --- a/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/client/CpsRestClientTest.java +++ b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/client/CpsRestClientTest.java @@ -164,4 +164,40 @@ public class CpsRestClientTest { assertEquals("sample response", cpsRestClient.addData("coverage-area-onap", "NearRTRIC", "patch", payload)); } + + @Test + public void testAddDataForPostListNodesRequest() throws Exception { + final String uri = "http://localhost:8000/anchors/coverage-area-onap/list-nodes?xpath=NearRTRIC"; + Mockito.when(restTemplate.postForEntity(ArgumentMatchers.eq(uri), ArgumentMatchers.any(), + ArgumentMatchers.<Class<String>>any())).thenReturn(response); + final Map<String, Object> payload = new HashMap<String, Object>(); + payload.put("idNearRTRIC", 11); + assertEquals("sample response", cpsRestClient.addData("coverage-area-onap", "NearRTRIC", + "post-list-node", payload)); + + } + + @Test + public void deleteListNodeData() throws Exception { + final String uri = "http://localhost:8000/anchors/coverage-area-onap/list-nodes?xpath=sample"; + response = new ResponseEntity<String>(HttpStatus.NO_CONTENT); + Mockito.when(restTemplate.exchange(ArgumentMatchers.eq(uri), + ArgumentMatchers.any(HttpMethod.class), + ArgumentMatchers.any(), + ArgumentMatchers.<Class<String>>any())) + .thenReturn(response); + assertEquals("Success", cpsRestClient.deleteData("coverage-area-onap", "sample", "delete-list-node")); + } + + @Test + public void deleteNodeData() throws Exception { + final String uri = "http://localhost:8000/anchors/coverage-area-onap/nodes?xpath=sample"; + response = new ResponseEntity<String>(HttpStatus.NO_CONTENT); + Mockito.when(restTemplate.exchange(ArgumentMatchers.eq(uri), + ArgumentMatchers.any(HttpMethod.class), + ArgumentMatchers.any(), + ArgumentMatchers.<Class<String>>any())) + .thenReturn(response); + assertEquals("Success", cpsRestClient.deleteData("coverage-area-onap", "sample", "delete")); + } } diff --git a/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogicTest.java b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogicTest.java index 60f0050..56c55c0 100644 --- a/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogicTest.java +++ b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogicTest.java @@ -221,6 +221,39 @@ public class ExecutionBusinessLogicTest { } } + @Test + public void testDeleteDataRequest() { + final Map<String, String> input = new HashMap<>(); + input.put("idNearRTRIC", "11"); + final String transformParam = "GNBDUFunction, NRCellDU, attributes, cellLocalId"; + final Template template = new Template("delete-snssai", "ran-network", + "/NearRTRIC/[@idNearRTRIC='11']/attributes/" + + "pLMNInfoList[@mcc='370' and '@mnc='410']/sNSSAIList[@sNssai='111-1111']", + "delete-list-node", true, null, null); + try { + final String result = "Success"; + Mockito.when(cpsRestClient.deleteData("ran-network", "/NearRTRIC/[@idNearRTRIC='11']/attributes/" + + "pLMNInfoList[@mcc='370' and '@mnc='410']/sNSSAIList[@sNssai='111-1111']", "delete-list-node")) + .thenReturn(result); + Mockito.when(templateRepository.findById(ArgumentMatchers.any())).thenReturn(Optional.of(template)); + assertEquals("Success", + executionBusinessLogic.executeTemplate("ran-network", "delete-snssai", request)); + } catch (final Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testDeleteDataFailRequest() throws Exception { + final Template template = new Template("deleteNbr", "ran-network", "sample", "delete-list-node", true, "", ""); + Mockito.when(cpsRestClient.deleteData("ran-network", "sample", "delete-list-node")) + .thenThrow(new ExecuteException("Response code from CPS other than 200: 401")); + Mockito.when(templateRepository.findById(ArgumentMatchers.any())) + .thenReturn(Optional.of(template)); + exception.expect(ExecuteException.class); + executionBusinessLogic.executeTemplate("ran-net", "deleteNbr", request); + } + /** * Reads a file from classpath. * |