diff options
author | krishnaa96 <krishna.moorthy6@wipro.com> | 2021-04-21 13:06:06 +0530 |
---|---|---|
committer | krishnaa96 <krishna.moorthy6@wipro.com> | 2021-04-22 15:37:44 +0530 |
commit | 60c9a8e05d329d53a7bd7321a52c83a7272f641c (patch) | |
tree | 3b8504bcd802f8f182457da2ca863ac187c92402 /cps-tbdmt-service | |
parent | 88d908b7031c305bf6f249ec64e7258e9dd14b08 (diff) |
Add support for query api
Issue-ID: CPS-349
Signed-off-by: krishnaa96 <krishna.moorthy6@wipro.com>
Change-Id: Ib68b7163e3e457378208e72f3566191f7c36d714
Diffstat (limited to 'cps-tbdmt-service')
8 files changed, 90 insertions, 22 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 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<String, String> 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<String, String> 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<String> response; + + /** + * Setup variables before test. + * + */ + @Before + public void setUp() { + responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.APPLICATION_JSON); - final ResponseEntity<String> 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.<Class<String>>any())) .thenReturn(response); - assertEquals("sample response", cpsRestClient.fetchNode("coverage-area-onap", "sample")); + assertEquals("sample response", cpsRestClient.fetchNode("coverage-area-onap", "sample", "get")); final ResponseEntity<String> errorResponse = new ResponseEntity<>("sample response", responseHeaders, HttpStatus.NOT_FOUND); @@ -90,11 +103,22 @@ 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.<Class<String>>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(), ArgumentMatchers.any(HttpMethod.class), @@ -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)); } |