From 60c9a8e05d329d53a7bd7321a52c83a7272f641c Mon Sep 17 00:00:00 2001 From: krishnaa96 Date: Wed, 21 Apr 2021 13:06:06 +0530 Subject: Add support for query api Issue-ID: CPS-349 Signed-off-by: krishnaa96 Change-Id: Ib68b7163e3e457378208e72f3566191f7c36d714 --- cps-tbdmt-dependencies/pom.xml | 2 +- cps-tbdmt-parent/pom.xml | 4 +-- .../cps/tbdmt/rest/TemplateControllerTest.java | 4 +-- .../org/onap/cps/tbdmt/client/CpsRestClient.java | 31 ++++++++++++++--- .../java/org/onap/cps/tbdmt/model/Template.java | 2 ++ .../org/onap/cps/tbdmt/model/TemplateRequest.java | 3 ++ .../cps/tbdmt/service/ExecutionBusinessLogic.java | 3 +- .../cps/tbdmt/service/TemplateBusinessLogic.java | 2 +- .../onap/cps/tbdmt/client/CpsRestClientTest.java | 40 +++++++++++++++++----- .../tbdmt/service/ExecutionBusinessLogicTest.java | 27 ++++++++++++--- .../tbdmt/service/TemplateBusinessLogicTest.java | 4 +-- 11 files changed, 95 insertions(+), 27 deletions(-) diff --git a/cps-tbdmt-dependencies/pom.xml b/cps-tbdmt-dependencies/pom.xml index 34650b9..468dd7d 100644 --- a/cps-tbdmt-dependencies/pom.xml +++ b/cps-tbdmt-dependencies/pom.xml @@ -29,7 +29,7 @@ This artifact contains dependencyManagement declarations of upstream versions. - 0.0.1-SNAPSHOT + 1.0.0 1.0.1.Final 2.5.6 1.18.16 diff --git a/cps-tbdmt-parent/pom.xml b/cps-tbdmt-parent/pom.xml index ce5b000..f50e4a8 100644 --- a/cps-tbdmt-parent/pom.xml +++ b/cps-tbdmt-parent/pom.xml @@ -16,7 +16,7 @@ pom - 0.0.1-SNAPSHOT + 1.0.0 11 0.9 https://nexus.onap.org @@ -126,7 +126,7 @@ ${project.groupId} spotbugs - ${project.version} + ${cps.version} diff --git a/cps-tbdmt-rest/src/test/java/org/onap/cps/tbdmt/rest/TemplateControllerTest.java b/cps-tbdmt-rest/src/test/java/org/onap/cps/tbdmt/rest/TemplateControllerTest.java index ce1178f..f4f383f 100644 --- a/cps-tbdmt-rest/src/test/java/org/onap/cps/tbdmt/rest/TemplateControllerTest.java +++ b/cps-tbdmt-rest/src/test/java/org/onap/cps/tbdmt/rest/TemplateControllerTest.java @@ -65,12 +65,12 @@ public class TemplateControllerTest { @Before public void setup() { objectMapper = new ObjectMapper(); - template = new Template("getNbr", "ran-network", "sample"); + template = new Template("getNbr", "ran-network", "sample", "get"); } @Test public void testCreateTemplate() throws Exception { - final TemplateRequest templateRequest = new TemplateRequest("getNbr", "ran-network", "sample"); + final TemplateRequest templateRequest = new TemplateRequest("getNbr", "ran-network", "sample", "get"); final String templateJson = objectMapper.writeValueAsString(templateRequest); Mockito.when(templateBusinessLogic.createTemplate(ArgumentMatchers.any())) .thenReturn(template); 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 c58ebd0..879efb5 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 @@ -30,12 +30,17 @@ import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; @Component public class CpsRestClient { - private static final String NODES_API_PATH = "%s/anchors/%s/nodes?cps-path=%s"; + private static final String NODES_API_PATH = "/anchors/{anchor}/nodes"; + + private static final String QUERY_API_PATH = "/anchors/{anchor}/nodes/query"; @Autowired private RestTemplate restTemplate; @@ -50,10 +55,14 @@ public class CpsRestClient { * @param xpath xpath query * @return result Response string from CPS */ - public String fetchNode(final String anchor, final String xpath) throws CpsClientException { - final String url = appConfiguration.getXnfProxyUrl(); - - final String uri = String.format(NODES_API_PATH, url, anchor, xpath); + public String fetchNode(final String anchor, final String xpath, + final String requestType) throws CpsClientException { + final MultiValueMap queryParams = new LinkedMultiValueMap<>(); + queryParams.add("cpsPath", xpath); + String uri = buildCpsUrl(NODES_API_PATH, anchor, queryParams); + if ("query".equals(requestType)) { + uri = buildCpsUrl(QUERY_API_PATH, anchor, queryParams); + } final HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); @@ -76,4 +85,16 @@ public class CpsRestClient { } } + private String buildCpsUrl(final String path, final String anchor, + final MultiValueMap queryParams) { + final String baseUrl = appConfiguration.getXnfProxyUrl(); + + return UriComponentsBuilder + .fromHttpUrl(baseUrl) + .path(path) + .queryParams(queryParams) + .buildAndExpand(anchor) + .toUriString(); + } + } diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/Template.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/Template.java index 14f159f..97353f2 100644 --- a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/Template.java +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/Template.java @@ -49,4 +49,6 @@ public class Template implements Serializable { private String xpathTemplate; + private String requestType; + } diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/TemplateRequest.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/TemplateRequest.java index c679a56..c8daf0f 100644 --- a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/TemplateRequest.java +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/model/TemplateRequest.java @@ -44,4 +44,7 @@ public class TemplateRequest implements Serializable { @NotEmpty(message = "template missing") private String xpathTemplate; + @NotEmpty(message = "request type missing") + private String requestType; + } 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 ff72cf7..b83a1f8 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 @@ -70,8 +70,9 @@ public class ExecutionBusinessLogic { throw new ExecuteException("Anchor not found for the schema"); } final String xpath = generateXpath(template.getXpathTemplate(), inputParameters); + try { - return cpsRestClient.fetchNode(anchor, xpath); + return cpsRestClient.fetchNode(anchor, xpath, template.getRequestType()); } catch (final CpsClientException e) { throw new ExecuteException(e.getLocalizedMessage()); } diff --git a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/TemplateBusinessLogic.java b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/TemplateBusinessLogic.java index f75352f..06c48fa 100644 --- a/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/TemplateBusinessLogic.java +++ b/cps-tbdmt-service/src/main/java/org/onap/cps/tbdmt/service/TemplateBusinessLogic.java @@ -48,7 +48,7 @@ public class TemplateBusinessLogic { public Template createTemplate(final TemplateRequest templateRequest) { final Template template = new Template(templateRequest.getTemplateId(), templateRequest.getModel(), - templateRequest.getXpathTemplate()); + templateRequest.getXpathTemplate(), templateRequest.getRequestType()); return templateRepository.save(template); } 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 f69d4ad..9742bb1 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,7 @@ package org.onap.cps.tbdmt.client; import static org.junit.Assert.assertEquals; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -68,18 +69,30 @@ public class CpsRestClientTest { @Rule public ExpectedException exception = ExpectedException.none(); - @Test - public void testFetchNode() throws Exception { - final HttpHeaders responseHeaders = new HttpHeaders(); + private HttpHeaders responseHeaders; + private ResponseEntity response; + + /** + * Setup variables before test. + * + */ + @Before + public void setUp() { + responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.APPLICATION_JSON); - final ResponseEntity response = new ResponseEntity<>("sample response", responseHeaders, + response = new ResponseEntity<>("sample response", responseHeaders, HttpStatus.OK); - Mockito.when(restTemplate.exchange(ArgumentMatchers.anyString(), + } + + @Test + public void testFetchNode() throws Exception { + final String uri = "http://localhost:8000/anchors/coverage-area-onap/nodes?cpsPath=sample"; + Mockito.when(restTemplate.exchange(ArgumentMatchers.eq(uri), ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.any(), ArgumentMatchers.>any())) .thenReturn(response); - assertEquals("sample response", cpsRestClient.fetchNode("coverage-area-onap", "sample")); + assertEquals("sample response", cpsRestClient.fetchNode("coverage-area-onap", "sample", "get")); final ResponseEntity errorResponse = new ResponseEntity<>("sample response", responseHeaders, HttpStatus.NOT_FOUND); @@ -90,10 +103,21 @@ public class CpsRestClientTest { .thenReturn(errorResponse); exception.expect(CpsClientException.class); exception.expectMessage("Response code from CPS other than 200: 404"); - cpsRestClient.fetchNode("coverage-area-onap", "sample"); + cpsRestClient.fetchNode("coverage-area-onap", "sample", "get"); } + @Test + public void testQueryApi() throws Exception { + final String uri = "http://localhost:8000/anchors/coverage-area-onap/nodes/query?cpsPath=sample"; + Mockito.when(restTemplate.exchange(ArgumentMatchers.eq(uri), + ArgumentMatchers.any(HttpMethod.class), + ArgumentMatchers.any(), + ArgumentMatchers.>any())) + .thenReturn(response); + assertEquals("sample response", cpsRestClient.fetchNode("coverage-area-onap", "sample", "query")); + } + @Test public void testFetchNodeException() throws Exception { Mockito.when(restTemplate.exchange(ArgumentMatchers.anyString(), @@ -103,6 +127,6 @@ public class CpsRestClientTest { .thenThrow(new RestClientException("Connection refused")); exception.expect(CpsClientException.class); exception.expectMessage("Connection refused"); - cpsRestClient.fetchNode("coverage-area-onap", "sample"); + cpsRestClient.fetchNode("coverage-area-onap", "sample", "get"); } } 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 28a7a49..32dbc27 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 @@ -78,6 +78,8 @@ public class ExecutionBusinessLogicTest { private Template template; + private Template queryTemplate; + /** * Setup variables before test. * @@ -89,7 +91,8 @@ public class ExecutionBusinessLogicTest { request = new ExecutionRequest(input); final String xpathTemplate = "/ran-coverage-area/pLMNIdList[@mcc='310' and @mnc='410']" + "/coverage-area[@coverageArea='{{coverageArea}}']"; - template = new Template("getNbr", "ran-network", xpathTemplate); + template = new Template("getNbr", "ran-network", xpathTemplate, "get"); + queryTemplate = new Template("getNbr", "ran-network", xpathTemplate, "query"); } @Test @@ -97,7 +100,7 @@ public class ExecutionBusinessLogicTest { final String resultString = "[{\"key\": \"value\"}]"; Mockito.when(cpsRestClient .fetchNode("ran-network", "/ran-coverage-area/pLMNIdList[@mcc='310' and @mnc='410']" - + "/coverage-area[@coverageArea='Zone 1']")) + + "/coverage-area[@coverageArea='Zone 1']", "get")) .thenReturn(resultString); Mockito.when(templateRepository.findById(ArgumentMatchers.any())) .thenReturn(Optional.of(template)); @@ -117,7 +120,7 @@ public class ExecutionBusinessLogicTest { final String exceptionMessage = "Response from CPS other than 200: 404"; Mockito.when(cpsRestClient .fetchNode("ran-network", "/ran-coverage-area/pLMNIdList[@mcc='310' and @mnc='410']" - + "/coverage-area[@coverageArea='Zone 1']")) + + "/coverage-area[@coverageArea='Zone 1']", "get")) .thenThrow(new CpsClientException(exceptionMessage)); Mockito.when(templateRepository.findById(ArgumentMatchers.any())) .thenReturn(Optional.of(template)); @@ -125,7 +128,7 @@ public class ExecutionBusinessLogicTest { exception.expectMessage(exceptionMessage); executionBusinessLogic.executeTemplate("ran-network", "getNbr", request); - final Template template1 = new Template("getNbr", "ran-net", "sample"); + final Template template1 = new Template("getNbr", "ran-net", "sample", "get"); Mockito.when(templateRepository.findById(ArgumentMatchers.any())) .thenReturn(Optional.of(template1)); exception.expect(ExecuteException.class); @@ -136,7 +139,7 @@ public class ExecutionBusinessLogicTest { @Test public void testExecuteTemplateNoAnchor() { - final Template template = new Template("getNbr", "ran-net", "sample"); + final Template template = new Template("getNbr", "ran-net", "sample", "get"); Mockito.when(templateRepository.findById(ArgumentMatchers.any())) .thenReturn(Optional.of(template)); exception.expect(ExecuteException.class); @@ -144,4 +147,18 @@ public class ExecutionBusinessLogicTest { executionBusinessLogic.executeTemplate("ran-net", "getNbr", request); } + @Test + public void testExecuteTemplateQueryApi() throws Exception { + final String resultString = "[{\"key\": \"value\"}]"; + Mockito.when(cpsRestClient + .fetchNode("ran-network", "/ran-coverage-area/pLMNIdList[@mcc='310' and @mnc='410']" + + "/coverage-area[@coverageArea='Zone 1']", "query")) + .thenReturn(resultString); + Mockito.when(templateRepository.findById(ArgumentMatchers.any())) + .thenReturn(Optional.of(queryTemplate)); + assertEquals(resultString, + executionBusinessLogic.executeTemplate("ran-network", "getNbr", request)); + + } + } diff --git a/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/TemplateBusinessLogicTest.java b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/TemplateBusinessLogicTest.java index 5cf9fc1..aa7e28a 100644 --- a/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/TemplateBusinessLogicTest.java +++ b/cps-tbdmt-service/src/test/java/org/onap/cps/tbdmt/service/TemplateBusinessLogicTest.java @@ -71,13 +71,13 @@ public class TemplateBusinessLogicTest { @Before public void setup() { - template = new Template("getNbr", "ran-network", "sample"); + template = new Template("getNbr", "ran-network", "sample", "get"); final TemplateKey templateKey = new TemplateKey("getNbr", "ran-network"); } @Test public void testCreateTemplate() throws Exception { - final TemplateRequest templateRequest = new TemplateRequest("getNbr", "ran-network", "sample"); + final TemplateRequest templateRequest = new TemplateRequest("getNbr", "ran-network", "sample", "get"); Mockito.when(templateRepository.save(ArgumentMatchers.any())).thenReturn(template); assertEquals(template, templateBusinessLogic.createTemplate(templateRequest)); } -- cgit 1.2.3-korg