Support delete query and dynamic anchor 39/124139/3
authorNiranjana <niranjana.y60@wipro.com>
Tue, 14 Sep 2021 10:59:23 +0000 (10:59 +0000)
committerNiranjana Y <niranjana.y60@wipro.com>
Fri, 17 Sep 2021 10:50:01 +0000 (10:50 +0000)
Issue-ID: CPS-661
Signed-off-by: Niranjana <niranjana.y60@wipro.com>
Change-Id: I720c348d49c53573ef8897d02d641e53232fc941

cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/client/CpsRestClient.java
cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogic.java
cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/client/CpsRestClientTest.java
cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/ExecutionBusinessLogicTest.java

index 8bf0bed..c54355f 100644 (file)
@@ -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) {
 
index a8004be..5b3bc60 100644 (file)
@@ -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());
index 4697479..76033c9 100644 (file)
@@ -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"));
+    }
 }
index 60f0050..56c55c0 100644 (file)
@@ -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.
      *