diff options
author | Niranjana <niranjana.y60@wipro.com> | 2021-08-12 04:16:00 +0000 |
---|---|---|
committer | Niranjana Y <niranjana.y60@wipro.com> | 2021-08-12 04:40:01 +0000 |
commit | 433ba106cf99762039b7bd6a430eaf24b45d2271 (patch) | |
tree | 7535dbeb9643a4ab61e4387ad2fe2a070f257258 /cps-tbdmt-service | |
parent | 3e5957464b6b4fa18bcae18bcfdce5fc9bace787 (diff) |
Support edit query
Issue-ID: CPS-511
Signed-off-by: Niranjana <niranjana.y60@wipro.com>
Change-Id: Icc22fb6143c01dcc388339ffa57818f41ed50cfd
Diffstat (limited to 'cps-tbdmt-service')
6 files changed, 112 insertions, 17 deletions
diff --git a/cps-tbdmt-service/pom.xml b/cps-tbdmt-service/pom.xml index d9181c5..f136023 100644 --- a/cps-tbdmt-service/pom.xml +++ b/cps-tbdmt-service/pom.xml @@ -81,6 +81,10 @@ <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + </dependency> <!--Test dependencies--> <dependency> <groupId>com.openpojo</groupId> @@ -98,4 +102,4 @@ <scope>test</scope> </dependency> </dependencies> -</project>
\ No newline at end of file +</project> 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 0d356d8..e1301fb 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 @@ -21,6 +21,7 @@ package org.onap.cps.tbdmt.client; import java.util.Arrays; +import java.util.Map; import org.onap.cps.tbdmt.exception.CpsClientException; import org.onap.cps.tbdmt.model.AppConfiguration; import org.onap.cps.tbdmt.model.CpsConfiguration; @@ -30,6 +31,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.stereotype.Component; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -43,6 +45,8 @@ public class CpsRestClient { private static final String QUERY_API_PATH = "/anchors/{anchor}/nodes/query"; + private static final String POST_API_PATH = "/anchors/{anchor}/nodes"; + @Autowired private RestTemplate restTemplate; @@ -91,6 +95,49 @@ public class CpsRestClient { } } + /** + * Post data to CPS using xpath. + * + * @param anchor anchor + * @param xpath xpath query + * @param requestType http request type + * @param payload request body + * @return result Response string from CPS + */ + public String addData(final String anchor, final String xpath, final String requestType, + final Map<String, Object> payload) throws CpsClientException { + + final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>(); + queryParams.add("xpath", xpath); + final CpsConfiguration cpsConfiguration = "cpsCore".equals(appConfiguration.getCpsClient()) + ? appConfiguration.getCpsCoreConfiguration() : appConfiguration.getNcmpConfiguration(); + + final HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.setBasicAuth(cpsConfiguration.getUsername(), cpsConfiguration.getPassword()); + final HttpEntity<String> entity = new HttpEntity<>(new com.google.gson.Gson().toJson(payload), headers); + + String uri = buildCpsUrl(cpsConfiguration.getUrl(), POST_API_PATH, anchor, queryParams); + try { + if (requestType.equalsIgnoreCase("post")) { + uri = buildCpsUrl(cpsConfiguration.getUrl(), POST_API_PATH, anchor, new LinkedMultiValueMap<>()); + return restTemplate.postForEntity(uri, entity, String.class).getBody(); + } else if (requestType.equalsIgnoreCase("patch")) { + final HttpComponentsClientHttpRequestFactory requestFactory + = new HttpComponentsClientHttpRequestFactory(); + requestFactory.setConnectTimeout(10000); + requestFactory.setReadTimeout(10000); + restTemplate.setRequestFactory(requestFactory); + return restTemplate.patchForObject(uri, entity, String.class); + } else { + return restTemplate.exchange(uri, HttpMethod.PUT, entity, String.class).getBody(); + } + } catch (final Exception e) { + throw new CpsClientException(e.getLocalizedMessage()); + } + } + 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/model/ExecutionRequest.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/ExecutionRequest.java index 322c6d4..8b6db17 100644 --- a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/ExecutionRequest.java +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/ExecutionRequest.java @@ -33,4 +33,6 @@ import lombok.Setter; public class ExecutionRequest { private Map<String, String> inputParameters; + + private Map<String, Object> payload; } 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 05aed38..6032b05 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 @@ -72,17 +72,18 @@ public class ExecutionBusinessLogic { final Optional<Template> templateOptional = templateRepository.findById(new TemplateKey(templateId)); if (templateOptional.isPresent()) { if (!StringUtils.isBlank(templateOptional.get().getMultipleQueryTemplateId())) { - return executeMultipleQuery(templateOptional.get(), executionRequest.getInputParameters()); + return executeMultipleQuery(templateOptional.get(), executionRequest.getInputParameters(), + executionRequest.getPayload()); } else { - return execute(templateOptional.get(), executionRequest.getInputParameters()); + return execute(templateOptional.get(), executionRequest.getInputParameters(), + executionRequest.getPayload()); } } throw new TemplateNotFoundException("Template does not exist"); } - private String executeMultipleQuery(final Template template, final Map<String, String> inputParameters) - throws OutputTransformationException { - + private String executeMultipleQuery(final Template template, final Map<String, String> inputParameters, + final Map<String, Object> payload) throws OutputTransformationException { final List<Object> processedQueryOutput = new ArrayList<Object>(); final String multipleQuerytemplateId = template.getMultipleQueryTemplateId(); final Optional<Template> multipleQueryTemplate = @@ -97,7 +98,7 @@ 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); + final String queryParamString = execute(multipleQueryTemplate.get(), inputParameters, payload); final List<String> queryParamList = new ArrayList<String>(); final JsonParser jsonParser = new JsonParser(); final Gson gson = new Gson(); @@ -114,7 +115,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); + final Object result = execute(template, inputParameter, payload); processedQueryOutput.add(result); }); } catch (final Exception e) { @@ -124,21 +125,27 @@ public class ExecutionBusinessLogic { } } - private String execute(final Template template, final Map<String, String> inputParameters) { + private String execute(final Template template, final Map<String, String> inputParameters, + final Map<String, Object> payload) { + final String anchor = appConfiguration.getSchemaToAnchor().get(template.getModel()); if (anchor == null) { throw new ExecuteException("Anchor not found for the schema"); } final String xpath = generateXpath(template.getXpathTemplate(), inputParameters); - try { - final String result = cpsRestClient.fetchNode(anchor, xpath, template.getRequestType(), - template.getIncludeDescendants()); - if (StringUtils.isBlank(template.getTransformParam())) { - return result; + if (template.getRequestType().equalsIgnoreCase("put") || template.getRequestType().equalsIgnoreCase("patch") + || template.getRequestType().equalsIgnoreCase("post")) { + return cpsRestClient.addData(anchor, xpath, template.getRequestType(), payload); } else { - final List<JsonElement> json = transform(template, result); - return new Gson().toJson(json); + final String result = cpsRestClient.fetchNode(anchor, xpath, template.getRequestType(), + template.getIncludeDescendants()); + if (StringUtils.isBlank(template.getTransformParam())) { + return result; + } else { + final List<JsonElement> json = transform(template, result); + return new Gson().toJson(json); + } } } catch (final CpsClientException e) { throw new ExecuteException(e.getLocalizedMessage()); 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 6e0ae4b..4697479 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 @@ -22,6 +22,8 @@ package org.onap.cps.tbdmt.client; import static org.junit.Assert.assertEquals; +import java.util.HashMap; +import java.util.Map; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -130,4 +132,36 @@ public class CpsRestClientTest { cpsRestClient.fetchNode("coverage-area-onap", "sample", "get", true); } + @Test + public void testAddDataForPostRequest() throws Exception { + final String uri = "http://localhost:8000/anchors/coverage-area-onap/nodes"; + 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", payload)); + + } + + @Test + public void testAddDataForPutRequest() throws Exception { + final String uri = "http://localhost:8000/anchors/coverage-area-onap/nodes?xpath=NearRTRIC"; + Mockito.when(restTemplate.exchange(ArgumentMatchers.eq(uri), ArgumentMatchers.any(HttpMethod.class), + 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", "put", payload)); + + } + + @Test + public void testAddDataForPatchRequest() throws Exception { + final String uri = "http://localhost:8000/anchors/coverage-area-onap/nodes?xpath=NearRTRIC"; + Mockito.when(restTemplate.patchForObject(ArgumentMatchers.eq(uri), ArgumentMatchers.any(), + ArgumentMatchers.<Class<String>>any())).thenReturn("sample response"); + final Map<String, Object> payload = new HashMap<String, Object>(); + payload.put("idNearRTRIC", 11); + assertEquals("sample response", cpsRestClient.addData("coverage-area-onap", "NearRTRIC", "patch", payload)); + + } } 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 e8689a9..60f0050 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 @@ -91,8 +91,9 @@ public class ExecutionBusinessLogicTest { @Before public void setup() { final Map<String, String> input = new HashMap<>(); + final Map<String, Object> payload = new HashMap<>(); input.put("coverageArea", "Zone 1"); - request = new ExecutionRequest(input); + request = new ExecutionRequest(input, payload); final String xpathTemplate = "/ran-coverage-area/pLMNIdList[@mcc='310' and @mnc='410']" + "/coverage-area[@coverageArea='{{coverageArea}}']"; template = new Template("getNbr", "ran-network", xpathTemplate, "get", true, "", ""); |