From b59cd347eae4b6f602debf818e06f8a731ed9092 Mon Sep 17 00:00:00 2001 From: "waqas.ikram" Date: Mon, 19 Aug 2019 16:39:18 +0000 Subject: [PATCH 1/1] Adding aai line of business endpoints Change-Id: Ic81d3bef49ad4549471f7b618e4aa98ac2725ca7 Issue-ID: SO-2219 Signed-off-by: waqas.ikram --- .../controller/LinesOfBusinessController.java | 88 +++++++++++++++++++++ .../controller/PlatformController.java | 6 +- .../LinesOfBusinessCacheServiceProvider.java | 36 +++++++++ .../LinesOfBusinessCacheServiceProviderImpl.java | 73 ++++++++++++++++++ .../org/onap/so/aaisimulator/utils/CacheName.java | 3 +- .../org/onap/so/aaisimulator/utils/Constants.java | 6 +- .../controller/GenericVnfsControllerTest.java | 52 +++++-------- .../controller/LinesOfBusinessControllerTest.java | 89 ++++++++++++++++++++++ .../controller/NodesControllerTest.java | 46 ++++------- .../controller/OwningEntityControllerTest.java | 72 +++++------------ .../controller/PlatformControllerTest.java | 41 +++------- .../controller/ProjectControllerTest.java | 73 +++++------------- .../onap/so/aaisimulator/utils/TestConstants.java | 2 + .../utils/TestRestTemplateService.java | 63 +++++++++++++++ .../org/onap/so/aaisimulator/utils/TestUtils.java | 21 +++++ .../test/resources/test-data/line-of-business.json | 3 + 16 files changed, 466 insertions(+), 208 deletions(-) create mode 100644 plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/LinesOfBusinessController.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/LinesOfBusinessCacheServiceProvider.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/LinesOfBusinessCacheServiceProviderImpl.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/LinesOfBusinessControllerTest.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/utils/TestRestTemplateService.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/line-of-business.json diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/LinesOfBusinessController.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/LinesOfBusinessController.java new file mode 100644 index 00000000..537760c8 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/LinesOfBusinessController.java @@ -0,0 +1,88 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aaisimulator.controller; + +import static org.onap.so.aaisimulator.utils.Constants.LINES_OF_BUSINESS_URL; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity; +import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion; +import java.util.Optional; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.MediaType; +import org.onap.aai.domain.yang.LineOfBusiness; +import org.onap.so.aaisimulator.service.providers.LinesOfBusinessCacheServiceProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@Controller +@RequestMapping(path = LINES_OF_BUSINESS_URL) +public class LinesOfBusinessController { + private static final Logger LOGGER = LoggerFactory.getLogger(LinesOfBusinessController.class); + + private final LinesOfBusinessCacheServiceProvider cacheServiceProvider; + + @Autowired + public LinesOfBusinessController(final LinesOfBusinessCacheServiceProvider cacheServiceProvider) { + this.cacheServiceProvider = cacheServiceProvider; + } + + @PutMapping(value = "{line-of-business-name}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}, + produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity putLineOfBusiness(@RequestBody final LineOfBusiness lineOfBusiness, + @PathVariable("line-of-business-name") final String lineOfBusinessName, final HttpServletRequest request) { + + LOGGER.info("Will add LineOfBusiness to cache with key 'line-of-business-name': {} ...", + lineOfBusiness.getLineOfBusinessName()); + + if (lineOfBusiness.getResourceVersion() == null || lineOfBusiness.getResourceVersion().isEmpty()) { + lineOfBusiness.setResourceVersion(getResourceVersion()); + + } + cacheServiceProvider.putLineOfBusiness(lineOfBusinessName, lineOfBusiness); + return ResponseEntity.accepted().build(); + } + + + @GetMapping(value = "/{line-of-business-name}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public ResponseEntity getLineOfBusiness(@PathVariable("line-of-business-name") final String lineOfBusinessName, + final HttpServletRequest request) { + LOGGER.info("retrieving Platform for 'platform-name': {} ...", lineOfBusinessName); + final Optional optional = cacheServiceProvider.getLineOfBusiness(lineOfBusinessName); + if (optional.isPresent()) { + final LineOfBusiness platform = optional.get(); + LOGGER.info("found LineOfBusiness {} in cache", platform); + return ResponseEntity.ok(platform); + } + LOGGER.error("Unable to find LineOfBusiness in cahce using {}", lineOfBusinessName); + return getRequestErrorResponseEntity(request); + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/PlatformController.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/PlatformController.java index a80477cd..5c585189 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/PlatformController.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/PlatformController.java @@ -19,6 +19,7 @@ */ package org.onap.so.aaisimulator.controller; +import static org.onap.so.aaisimulator.utils.Constants.PLATFORM; import static org.onap.so.aaisimulator.utils.Constants.PLATFORMS_URL; import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity; import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion; @@ -28,7 +29,6 @@ import javax.ws.rs.core.MediaType; import org.onap.aai.domain.yang.Platform; import org.onap.aai.domain.yang.Relationship; import org.onap.so.aaisimulator.service.providers.PlatformCacheServiceProvider; -import org.onap.so.aaisimulator.utils.RequestErrorResponseUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -81,7 +81,7 @@ public class PlatformController { return ResponseEntity.ok(platform); } LOGGER.error("Unable to find Platform in cahce using {}", platformName); - return getRequestErrorResponseEntity(request); + return getRequestErrorResponseEntity(request, PLATFORM); } @PutMapping(value = "/{platform-name}/relationship-list/relationship", @@ -103,7 +103,7 @@ public class PlatformController { LOGGER.error("Couldn't add {} relationship for 'platform-name': {} ...", relationship.getRelatedTo(), platformName); - return RequestErrorResponseUtils.getRequestErrorResponseEntity(request); + return getRequestErrorResponseEntity(request, PLATFORM); } } diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/LinesOfBusinessCacheServiceProvider.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/LinesOfBusinessCacheServiceProvider.java new file mode 100644 index 00000000..d4eb634c --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/LinesOfBusinessCacheServiceProvider.java @@ -0,0 +1,36 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aaisimulator.service.providers; + +import java.util.Optional; +import org.onap.aai.domain.yang.LineOfBusiness; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +public interface LinesOfBusinessCacheServiceProvider extends Clearable { + + void putLineOfBusiness(final String lineOfBusinessName, final LineOfBusiness lineOfBusiness); + + Optional getLineOfBusiness(final String lineOfBusinessName); + + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/LinesOfBusinessCacheServiceProviderImpl.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/LinesOfBusinessCacheServiceProviderImpl.java new file mode 100644 index 00000000..e2255760 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/service/providers/LinesOfBusinessCacheServiceProviderImpl.java @@ -0,0 +1,73 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aaisimulator.service.providers; + +import static org.onap.so.aaisimulator.utils.CacheName.LINES_OF_BUSINESS_CACHE; +import java.util.Optional; +import org.onap.aai.domain.yang.LineOfBusiness; +import org.onap.so.simulator.cache.provider.AbstractCacheServiceProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.Cache; +import org.springframework.cache.CacheManager; +import org.springframework.stereotype.Service; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@Service +public class LinesOfBusinessCacheServiceProviderImpl extends AbstractCacheServiceProvider + implements LinesOfBusinessCacheServiceProvider { + + private static final Logger LOGGER = LoggerFactory.getLogger(LinesOfBusinessCacheServiceProviderImpl.class); + + @Autowired + public LinesOfBusinessCacheServiceProviderImpl(final CacheManager cacheManager) { + super(cacheManager); + } + + @Override + public void putLineOfBusiness(final String lineOfBusinessName, final LineOfBusiness lineOfBusiness) { + LOGGER.info("Adding LineOfBusiness to cache with key: {} ...", lineOfBusinessName); + final Cache cache = getCache(LINES_OF_BUSINESS_CACHE.getName()); + cache.put(lineOfBusinessName, lineOfBusiness); + + } + + @Override + public Optional getLineOfBusiness(final String lineOfBusinessName) { + LOGGER.info("getting LineOfBusiness from cache using key: {}", lineOfBusinessName); + final Cache cache = getCache(LINES_OF_BUSINESS_CACHE.getName()); + final LineOfBusiness value = cache.get(lineOfBusinessName, LineOfBusiness.class); + if (value != null) { + return Optional.of(value); + } + LOGGER.error("Unable to find LineOfBusiness in cache using key:{} ", lineOfBusinessName); + return Optional.empty(); + } + + @Override + public void clearAll() { + clearCahce(LINES_OF_BUSINESS_CACHE.getName()); + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/CacheName.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/CacheName.java index c39daa0b..7f804c9d 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/CacheName.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/CacheName.java @@ -30,7 +30,8 @@ public enum CacheName { NODES_CACHE("nodes-cache"), GENERIC_VNF_CACHE("generic-vnf-cache"), OWNING_ENTITY_CACHE("owning-entity-cache"), - PLATFORM_CACHE("platform-cache"); + PLATFORM_CACHE("platform-cache"), + LINES_OF_BUSINESS_CACHE("lines-of-business-cache"); private String name; diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/Constants.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/Constants.java index 94bee270..daaef336 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/Constants.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/main/java/org/onap/so/aaisimulator/utils/Constants.java @@ -35,7 +35,7 @@ public class Constants { public static final String PROJECT_URL = BUSINESS_URL + "/projects/project/"; - public static final String OWNING_ENTITY_URL = BUSINESS_URL + "/owning-entities/owning-entity"; + public static final String OWNING_ENTITY_URL = BUSINESS_URL + "/owning-entities/owning-entity/"; public static final String PLATFORMS_URL = BUSINESS_URL + "/platforms/platform/"; @@ -45,6 +45,8 @@ public class Constants { public static final String RELATIONSHIP_LIST_RELATIONSHIP_URL = "/relationship-list/relationship"; + public static final String LINES_OF_BUSINESS_URL = BUSINESS_URL + "/lines-of-business/line-of-business/"; + public static final String HEALTHY = "healthy"; public static final String PROJECT = "project"; @@ -76,7 +78,7 @@ public class Constants { public static final String PLATFORM = "platform"; public static final String USES = "org.onap.relationships.inventory.Uses"; - + public static final String PLATFORM_PLATFORM_NAME = "platform.platform-name"; public static final String SERVICE_SUBSCRIPTION = "service-subscription"; diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/GenericVnfsControllerTest.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/GenericVnfsControllerTest.java index abff7499..c219d3b8 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/GenericVnfsControllerTest.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/GenericVnfsControllerTest.java @@ -51,17 +51,13 @@ import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider; import org.onap.so.aaisimulator.service.providers.GenericVnfCacheServiceProvider; import org.onap.so.aaisimulator.service.providers.PlatformCacheServiceProvider; import org.onap.so.aaisimulator.utils.Constants; +import org.onap.so.aaisimulator.utils.TestRestTemplateService; import org.onap.so.aaisimulator.utils.TestUtils; -import org.onap.so.simulator.model.UserCredentials; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.ActiveProfiles; @@ -81,10 +77,7 @@ public class GenericVnfsControllerTest { private int port; @Autowired - private TestRestTemplate restTemplate; - - @Autowired - private UserCredentials userCredentials; + private TestRestTemplateService testRestTemplateService; @Autowired private CustomerCacheServiceProvider customerCacheServiceProvider; @@ -106,10 +99,12 @@ public class GenericVnfsControllerTest { public void test_putGenericVnf_successfullyAddedToCache() throws Exception { final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID); - final ResponseEntity genericVnfResponse = invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf()); + final ResponseEntity genericVnfResponse = + testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class); assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode()); - final ResponseEntity response = invokeHttpGet(genericVnfUrl, GenericVnf.class); + final ResponseEntity response = + testRestTemplateService.invokeHttpGet(genericVnfUrl, GenericVnf.class); assertEquals(HttpStatus.OK, response.getStatusCode()); assertTrue(response.hasBody()); @@ -126,8 +121,8 @@ public class GenericVnfsControllerTest { addCustomerServiceAndGenericVnf(); final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_URL); - final ResponseEntity genericVnfRelationShipResponse = - invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getRelationShip()); + final ResponseEntity genericVnfRelationShipResponse = testRestTemplateService + .invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getRelationShip(), Void.class); assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode()); @@ -195,12 +190,13 @@ public class GenericVnfsControllerTest { addCustomerServiceAndGenericVnf(); final String platformUrl = getUrl(Constants.PLATFORMS_URL, PLATFORM_NAME); - final ResponseEntity platformResponse = invokeHttpPut(platformUrl, TestUtils.getPlatform()); + final ResponseEntity platformResponse = + testRestTemplateService.invokeHttpPut(platformUrl, TestUtils.getPlatform(), Void.class); assertEquals(HttpStatus.ACCEPTED, platformResponse.getStatusCode()); final String genericVnfRelationShipUrl = getUrl(GENERIC_VNF_URL, VNF_ID, RELATIONSHIP_URL); - final ResponseEntity genericVnfRelationShipResponse = - invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getPlatformRelatedLink()); + final ResponseEntity genericVnfRelationShipResponse = testRestTemplateService + .invokeHttpPut(genericVnfRelationShipUrl, TestUtils.getPlatformRelatedLink(), Void.class); assertEquals(HttpStatus.ACCEPTED, genericVnfRelationShipResponse.getStatusCode()); @@ -227,16 +223,18 @@ public class GenericVnfsControllerTest { } private void addCustomerServiceAndGenericVnf() throws Exception, IOException { - final ResponseEntity customerResponse = invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer()); + final ResponseEntity customerResponse = + testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class); assertEquals(HttpStatus.ACCEPTED, customerResponse.getStatusCode()); final String serviceInstanceUrl = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL); final ResponseEntity serviceInstanceResponse = - invokeHttpPut(serviceInstanceUrl, TestUtils.getServiceInstance()); + testRestTemplateService.invokeHttpPut(serviceInstanceUrl, TestUtils.getServiceInstance(), Void.class); assertEquals(HttpStatus.ACCEPTED, serviceInstanceResponse.getStatusCode()); final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID); - final ResponseEntity genericVnfResponse = invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf()); + final ResponseEntity genericVnfResponse = + testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class); assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode()); } @@ -245,22 +243,6 @@ public class GenericVnfsControllerTest { return relationshipData.stream().filter(data -> data.getRelationshipKey().equals(key)).findFirst().orElse(null); } - private ResponseEntity invokeHttpPut(final String url, final Object obj) { - final HttpEntity httpEntity = getHttpEntity(obj); - return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class); - } - - private ResponseEntity invokeHttpGet(final String url, final Class clazz) { - return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), clazz); - } - - private HttpEntity getHttpEntity(final Object obj) { - return new HttpEntity<>(obj, getHttpHeaders()); - } - - private HttpHeaders getHttpHeaders() { - return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername()); - } private String getUrl(final String... urls) { return TestUtils.getUrl(port, urls); diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/LinesOfBusinessControllerTest.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/LinesOfBusinessControllerTest.java new file mode 100644 index 00000000..ba92b02d --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/LinesOfBusinessControllerTest.java @@ -0,0 +1,89 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aaisimulator.controller; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.onap.so.aaisimulator.utils.TestConstants.LINE_OF_BUSINESS_NAME; +import org.junit.After; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.aai.domain.yang.LineOfBusiness; +import org.onap.so.aaisimulator.service.providers.LinesOfBusinessCacheServiceProvider; +import org.onap.so.aaisimulator.utils.Constants; +import org.onap.so.aaisimulator.utils.TestRestTemplateService; +import org.onap.so.aaisimulator.utils.TestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ActiveProfiles("test") +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@Configuration +public class LinesOfBusinessControllerTest { + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplateService testRestTemplateService; + + @Autowired + private LinesOfBusinessCacheServiceProvider linesOfBusinessCacheServiceProvider; + + @After + public void after() { + linesOfBusinessCacheServiceProvider.clearAll(); + } + + @Test + public void test_putLineOfBusiness_successfullyAddedToCache() throws Exception { + + final String url = getUrl(Constants.LINES_OF_BUSINESS_URL, LINE_OF_BUSINESS_NAME); + final ResponseEntity lineOfBusinessResponse = + testRestTemplateService.invokeHttpPut(url, TestUtils.getLineOfBusiness(), Void.class); + assertEquals(HttpStatus.ACCEPTED, lineOfBusinessResponse.getStatusCode()); + + final ResponseEntity response = testRestTemplateService.invokeHttpGet(url, LineOfBusiness.class); + assertEquals(HttpStatus.OK, response.getStatusCode()); + + assertTrue(response.hasBody()); + + final LineOfBusiness actualLineOfBusiness = response.getBody(); + assertEquals(LINE_OF_BUSINESS_NAME, actualLineOfBusiness.getLineOfBusinessName()); + assertNotNull("resource version should not be null", actualLineOfBusiness.getResourceVersion()); + + } + + private String getUrl(final String... urls) { + return TestUtils.getUrl(port, urls); + } +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/NodesControllerTest.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/NodesControllerTest.java index fb9a1148..07094e55 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/NodesControllerTest.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/NodesControllerTest.java @@ -49,17 +49,13 @@ import org.onap.so.aaisimulator.models.Results; import org.onap.so.aaisimulator.service.providers.CustomerCacheServiceProvider; import org.onap.so.aaisimulator.service.providers.NodesCacheServiceProvider; import org.onap.so.aaisimulator.utils.Constants; +import org.onap.so.aaisimulator.utils.TestRestTemplateService; import org.onap.so.aaisimulator.utils.TestUtils; -import org.onap.so.simulator.model.UserCredentials; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.ActiveProfiles; @@ -79,10 +75,7 @@ public class NodesControllerTest { private int port; @Autowired - private TestRestTemplate restTemplate; - - @Autowired - private UserCredentials userCredentials; + private TestRestTemplateService testRestTemplateService; @Autowired private NodesCacheServiceProvider nodesCacheServiceProvider; @@ -102,9 +95,8 @@ public class NodesControllerTest { invokeCustomerandServiceInstanceUrls(); - final ResponseEntity actual = - restTemplate.exchange(getUrl(Constants.NODES_URL, SERVICE_INSTANCE_URL), HttpMethod.GET, - new HttpEntity<>(getHttpHeaders()), ServiceInstance.class); + final ResponseEntity actual = testRestTemplateService + .invokeHttpGet(getUrl(Constants.NODES_URL, SERVICE_INSTANCE_URL), ServiceInstance.class); assertEquals(HttpStatus.OK, actual.getStatusCode()); assertTrue(actual.hasBody()); @@ -122,9 +114,9 @@ public class NodesControllerTest { invokeCustomerandServiceInstanceUrls(); - final ResponseEntity actual = restTemplate.exchange( + final ResponseEntity actual = testRestTemplateService.invokeHttpGet( getUrl(Constants.NODES_URL, SERVICE_INSTANCE_URL) + "?format=" + Format.PATHED.getValue(), - HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), Results.class); + Results.class); assertEquals(HttpStatus.OK, actual.getStatusCode()); assertTrue(actual.hasBody()); @@ -145,12 +137,14 @@ public class NodesControllerTest { invokeCustomerandServiceInstanceUrls(); final String genericVnfUrl = getUrl(GENERIC_VNF_URL, VNF_ID); - final ResponseEntity genericVnfResponse = invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf()); + final ResponseEntity genericVnfResponse = + testRestTemplateService.invokeHttpPut(genericVnfUrl, TestUtils.getGenericVnf(), Void.class); assertEquals(HttpStatus.ACCEPTED, genericVnfResponse.getStatusCode()); final String nodeGenericVnfsUrl = getUrl(NODES_URL, GENERIC_VNFS_URL) + "?vnf-name=" + GENERIC_VNF_NAME; - final ResponseEntity actual = restTemplate.exchange(nodeGenericVnfsUrl, HttpMethod.GET, - new HttpEntity<>(getHttpHeaders()), GenericVnfs.class); + + final ResponseEntity actual = + testRestTemplateService.invokeHttpGet(nodeGenericVnfsUrl, GenericVnfs.class); assertEquals(HttpStatus.OK, actual.getStatusCode()); assertTrue(actual.hasBody()); @@ -167,11 +161,13 @@ public class NodesControllerTest { private void invokeCustomerandServiceInstanceUrls() throws Exception, IOException { final String url = getUrl(CUSTOMERS_URL, SERVICE_SUBSCRIPTIONS_URL, SERVICE_INSTANCE_URL); - final ResponseEntity response = invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer()); + final ResponseEntity response = + testRestTemplateService.invokeHttpPut(getUrl(CUSTOMERS_URL), TestUtils.getCustomer(), Void.class); assertEquals(HttpStatus.ACCEPTED, response.getStatusCode()); - final ResponseEntity response2 = invokeHttpPut(url, TestUtils.getServiceInstance()); + final ResponseEntity response2 = + testRestTemplateService.invokeHttpPut(url, TestUtils.getServiceInstance(), Void.class); assertEquals(HttpStatus.ACCEPTED, response2.getStatusCode()); } @@ -179,16 +175,4 @@ public class NodesControllerTest { return TestUtils.getUrl(port, urls); } - private ResponseEntity invokeHttpPut(final String url, final Object obj) { - final HttpEntity httpEntity = getHttpEntity(obj); - return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class); - } - - private HttpEntity getHttpEntity(final Object obj) { - return new HttpEntity<>(obj, getHttpHeaders()); - } - - private HttpHeaders getHttpHeaders() { - return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername()); - } } diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/OwningEntityControllerTest.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/OwningEntityControllerTest.java index 7aabc665..8eda2013 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/OwningEntityControllerTest.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/OwningEntityControllerTest.java @@ -24,11 +24,6 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.onap.so.aaisimulator.utils.TestConstants.RELATIONSHIP_URL; -import static org.onap.so.aaisimulator.utils.TestUtils.getFile; -import static org.onap.so.aaisimulator.utils.TestUtils.getHttpHeaders; -import static org.onap.so.aaisimulator.utils.TestUtils.getJsonString; -import java.io.IOException; -import java.nio.file.Files; import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; @@ -37,16 +32,13 @@ import org.onap.so.aaisimulator.models.Format; import org.onap.so.aaisimulator.models.Results; import org.onap.so.aaisimulator.service.providers.OwnEntityCacheServiceProvider; import org.onap.so.aaisimulator.utils.Constants; +import org.onap.so.aaisimulator.utils.TestRestTemplateService; import org.onap.so.aaisimulator.utils.TestUtils; -import org.onap.so.simulator.model.UserCredentials; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.ActiveProfiles; @@ -61,21 +53,15 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @Configuration public class OwningEntityControllerTest { - private static final String OWNING_ENTITY_JSON_FILE = "test-data/owning-entity.json"; private static final String OWN_ENTITY_ID_VALUE = "oe_1"; private static final String OWN_ENTITY_NAME_VALUE = "oe_2"; - private static final String OWNING_ENTITY_RELATION_SHIP_JSON_FILE = "test-data/owning-entity-relation-ship.json"; - @LocalServerPort private int port; @Autowired - private TestRestTemplate restTemplate; - - @Autowired - private UserCredentials userCredentials; + private TestRestTemplateService testRestTemplateService; @Autowired private OwnEntityCacheServiceProvider cacheServiceProvider; @@ -87,13 +73,14 @@ public class OwningEntityControllerTest { @Test public void test_putOwningEntity_successfullyAddedToCache() throws Exception { - final String url = getOwningEntityEndPointUrl() + "/" + OWN_ENTITY_ID_VALUE; - final String body = new String(Files.readAllBytes(getFile(OWNING_ENTITY_JSON_FILE).toPath())); - final ResponseEntity actual = invokeHttpPut(url, body); + final String url = getUrl(Constants.OWNING_ENTITY_URL, OWN_ENTITY_ID_VALUE); + final ResponseEntity actual = + testRestTemplateService.invokeHttpPut(url, TestUtils.getOwningEntity(), Void.class); assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode()); - final ResponseEntity actualResponse = invokeHttpGet(url, OwningEntity.class); + final ResponseEntity actualResponse = + testRestTemplateService.invokeHttpGet(url, OwningEntity.class); assertEquals(HttpStatus.OK, actualResponse.getStatusCode()); assertTrue(actualResponse.hasBody()); @@ -106,14 +93,14 @@ public class OwningEntityControllerTest { @Test public void test_getOwningEntityCount_correctResult() throws Exception { - final String url = getOwningEntityEndPointUrl() + "/" + OWN_ENTITY_ID_VALUE; - final String body = new String(Files.readAllBytes(getFile(OWNING_ENTITY_JSON_FILE).toPath())); - final ResponseEntity actual = invokeHttpPut(url, body); + final String url = getUrl(Constants.OWNING_ENTITY_URL, OWN_ENTITY_ID_VALUE); + final ResponseEntity actual = + testRestTemplateService.invokeHttpPut(url, TestUtils.getOwningEntity(), Void.class); assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode()); - final ResponseEntity actualResponse = - invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=" + Format.COUNT.getValue(), Results.class); + final ResponseEntity actualResponse = testRestTemplateService + .invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=" + Format.COUNT.getValue(), Results.class); assertEquals(HttpStatus.OK, actualResponse.getStatusCode()); assertTrue(actualResponse.hasBody()); @@ -125,17 +112,20 @@ public class OwningEntityControllerTest { @Test public void test_putOwningEntityRelationShip_successfullyAddedToCache() throws Exception { - final String url = getOwningEntityEndPointUrl() + "/" + OWN_ENTITY_ID_VALUE; - final ResponseEntity actual = invokeHttpPut(url, getJsonString(OWNING_ENTITY_JSON_FILE)); + final String url = getUrl(Constants.OWNING_ENTITY_URL, OWN_ENTITY_ID_VALUE); + final ResponseEntity actual = + testRestTemplateService.invokeHttpPut(url, TestUtils.getOwningEntity(), Void.class); assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode()); final String owningEntityRelationshipUrl = url + RELATIONSHIP_URL; - final ResponseEntity putResponse = invokeHttpPut(owningEntityRelationshipUrl, getRelationship()); + final ResponseEntity putResponse = testRestTemplateService.invokeHttpPut(owningEntityRelationshipUrl, + TestUtils.getOwningEntityRelationship(), Void.class); assertEquals(HttpStatus.ACCEPTED, putResponse.getStatusCode()); - final ResponseEntity actualResponse = invokeHttpGet(url, OwningEntity.class); + final ResponseEntity actualResponse = + testRestTemplateService.invokeHttpGet(url, OwningEntity.class); assertEquals(HttpStatus.OK, actualResponse.getStatusCode()); assertTrue(actualResponse.hasBody()); @@ -148,29 +138,9 @@ public class OwningEntityControllerTest { } - private String getRelationship() throws IOException { - return TestUtils.getJsonString(OWNING_ENTITY_RELATION_SHIP_JSON_FILE); - } - - private ResponseEntity invokeHttpGet(final String url, final Class clazz) { - return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders(getUsername())), clazz); - } - - private String getUsername() { - return userCredentials.getUsers().iterator().next().getUsername(); - } - - private ResponseEntity invokeHttpPut(final String url, final Object obj) { - final HttpEntity httpEntity = getHttpEntity(obj); - return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class); + private String getUrl(final String... urls) { + return TestUtils.getUrl(port, urls); } - private HttpEntity getHttpEntity(final Object obj) { - return new HttpEntity<>(obj, getHttpHeaders(getUsername())); - } - - private String getOwningEntityEndPointUrl() { - return TestUtils.getBaseUrl(port) + Constants.OWNING_ENTITY_URL; - } } diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/PlatformControllerTest.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/PlatformControllerTest.java index cb704795..e6cf399f 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/PlatformControllerTest.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/PlatformControllerTest.java @@ -37,17 +37,13 @@ import org.onap.aai.domain.yang.RelationshipData; import org.onap.so.aaisimulator.service.providers.PlatformCacheServiceProvider; import org.onap.so.aaisimulator.utils.Constants; import org.onap.so.aaisimulator.utils.TestConstants; +import org.onap.so.aaisimulator.utils.TestRestTemplateService; import org.onap.so.aaisimulator.utils.TestUtils; -import org.onap.so.simulator.model.UserCredentials; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.ActiveProfiles; @@ -67,15 +63,11 @@ public class PlatformControllerTest { private int port; @Autowired - private TestRestTemplate restTemplate; - - @Autowired - private UserCredentials userCredentials; + private TestRestTemplateService testRestTemplateService; @Autowired private PlatformCacheServiceProvider platformCacheServiceProvider; - @After public void after() { platformCacheServiceProvider.clearAll(); @@ -85,10 +77,11 @@ public class PlatformControllerTest { public void test_putPlatform_successfullyAddedToCache() throws Exception { final String platformUrl = getUrl(Constants.PLATFORMS_URL, PLATFORM_NAME); - final ResponseEntity platformResponse = invokeHttpPut(platformUrl, TestUtils.getPlatform(), Void.class); + final ResponseEntity platformResponse = + testRestTemplateService.invokeHttpPut(platformUrl, TestUtils.getPlatform(), Void.class); assertEquals(HttpStatus.ACCEPTED, platformResponse.getStatusCode()); - final ResponseEntity response = invokeHttpGet(platformUrl, Platform.class); + final ResponseEntity response = testRestTemplateService.invokeHttpGet(platformUrl, Platform.class); assertEquals(HttpStatus.OK, response.getStatusCode()); assertTrue(response.hasBody()); @@ -103,13 +96,14 @@ public class PlatformControllerTest { public void test_putGenericVnfRelationShipToPlatform_successfullyAddedToCache() throws Exception { final String platformUrl = getUrl(Constants.PLATFORMS_URL, PLATFORM_NAME); - final ResponseEntity platformResponse = invokeHttpPut(platformUrl, TestUtils.getPlatform(), Void.class); + final ResponseEntity platformResponse = + testRestTemplateService.invokeHttpPut(platformUrl, TestUtils.getPlatform(), Void.class); assertEquals(HttpStatus.ACCEPTED, platformResponse.getStatusCode()); final String platformRelationShipUrl = getUrl(Constants.PLATFORMS_URL, PLATFORM_NAME, RELATIONSHIP_URL); - final ResponseEntity responseEntity = - invokeHttpPut(platformRelationShipUrl, TestUtils.getPlatformRelationShip(), Relationship.class); + final ResponseEntity responseEntity = testRestTemplateService + .invokeHttpPut(platformRelationShipUrl, TestUtils.getPlatformRelationShip(), Relationship.class); assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode()); final Optional optional = platformCacheServiceProvider.getPlatform(PLATFORM_NAME); @@ -135,23 +129,6 @@ public class PlatformControllerTest { } - private ResponseEntity invokeHttpGet(final String url, final Class clazz) { - return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), clazz); - } - - private ResponseEntity invokeHttpPut(final String url, final Object obj, final Class clazz) { - final HttpEntity httpEntity = getHttpEntity(obj); - return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, clazz); - } - - private HttpEntity getHttpEntity(final Object obj) { - return new HttpEntity<>(obj, getHttpHeaders()); - } - - private HttpHeaders getHttpHeaders() { - return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername()); - } - private String getUrl(final String... urls) { return TestUtils.getUrl(port, urls); } diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/ProjectControllerTest.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/ProjectControllerTest.java index d4f686db..66299a48 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/ProjectControllerTest.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/controller/ProjectControllerTest.java @@ -23,11 +23,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.onap.so.aaisimulator.utils.TestUtils.getFile; -import static org.onap.so.aaisimulator.utils.TestUtils.getHttpHeaders; -import static org.onap.so.aaisimulator.utils.TestUtils.getJsonString; -import java.io.IOException; -import java.nio.file.Files; +import static org.onap.so.aaisimulator.utils.TestConstants.RELATIONSHIP_URL; import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; @@ -35,16 +31,13 @@ import org.onap.aai.domain.yang.Project; import org.onap.so.aaisimulator.models.Results; import org.onap.so.aaisimulator.service.providers.ProjectCacheServiceProvider; import org.onap.so.aaisimulator.utils.Constants; +import org.onap.so.aaisimulator.utils.TestRestTemplateService; import org.onap.so.aaisimulator.utils.TestUtils; -import org.onap.so.simulator.model.UserCredentials; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.ActiveProfiles; @@ -60,22 +53,13 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @Configuration public class ProjectControllerTest { - private static final String RELATIONSHIP_URL = "/relationship-list/relationship"; - - private static final String BUSINESS_PROJECT_JSON_FILE = "test-data/business-project.json"; - - private static final String PROJECT_RELATION_SHIP_JSON_FILE = "test-data/business-project-relation-ship.json"; - private static final String PROJECT_NAME_VALUE = "PROJECT_NAME_VALUE"; @LocalServerPort private int port; @Autowired - private TestRestTemplate restTemplate; - - @Autowired - private UserCredentials userCredentials; + private TestRestTemplateService testRestTemplateService; @Autowired private ProjectCacheServiceProvider cacheServiceProvider; @@ -87,13 +71,13 @@ public class ProjectControllerTest { @Test public void test_putProject_successfullyAddedToCache() throws Exception { - final String url = getProjectEndPointUrl() + "/" + PROJECT_NAME_VALUE; - final String body = new String(Files.readAllBytes(getFile(BUSINESS_PROJECT_JSON_FILE).toPath())); - final ResponseEntity actual = invokeHttpPut(url, body); + final String url = getUrl(Constants.PROJECT_URL, PROJECT_NAME_VALUE); + final ResponseEntity actual = + testRestTemplateService.invokeHttpPut(url, TestUtils.getBusinessProject(), Void.class); assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode()); - final ResponseEntity actualResponse = invokeHttpGet(url, Project.class); + final ResponseEntity actualResponse = testRestTemplateService.invokeHttpGet(url, Project.class); assertEquals(HttpStatus.OK, actualResponse.getStatusCode()); assertTrue(actualResponse.hasBody()); @@ -105,17 +89,19 @@ public class ProjectControllerTest { @Test public void test_putProjectRelationShip_successfullyAddedToCache() throws Exception { - final String url = getProjectEndPointUrl() + "/" + PROJECT_NAME_VALUE; - final ResponseEntity actual = invokeHttpPut(url, getJsonString(BUSINESS_PROJECT_JSON_FILE)); + final String url = getUrl(Constants.PROJECT_URL, PROJECT_NAME_VALUE); + final ResponseEntity actual = + testRestTemplateService.invokeHttpPut(url, TestUtils.getBusinessProject(), Void.class); assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode()); - final String projectRelationshipUrl = url + RELATIONSHIP_URL; + final String projectRelationshipUrl = getUrl(Constants.PROJECT_URL, PROJECT_NAME_VALUE, RELATIONSHIP_URL); - final ResponseEntity putResponse = invokeHttpPut(projectRelationshipUrl, getRelationship()); + final ResponseEntity putResponse = testRestTemplateService.invokeHttpPut(projectRelationshipUrl, + TestUtils.getBusinessProjectRelationship(), Void.class); assertEquals(HttpStatus.ACCEPTED, putResponse.getStatusCode()); - final ResponseEntity actualResponse = invokeHttpGet(url, Project.class); + final ResponseEntity actualResponse = testRestTemplateService.invokeHttpGet(url, Project.class); assertEquals(HttpStatus.OK, actualResponse.getStatusCode()); assertTrue(actualResponse.hasBody()); @@ -129,14 +115,14 @@ public class ProjectControllerTest { @Test public void test_getProjectCount_correctResult() throws Exception { - final String url = getProjectEndPointUrl() + "/" + PROJECT_NAME_VALUE; - final String body = new String(Files.readAllBytes(getFile(BUSINESS_PROJECT_JSON_FILE).toPath())); - final ResponseEntity actual = invokeHttpPut(url, body); + final String url = getUrl(Constants.PROJECT_URL, PROJECT_NAME_VALUE); + final ResponseEntity actual = + testRestTemplateService.invokeHttpPut(url, TestUtils.getBusinessProject(), Void.class); assertEquals(HttpStatus.ACCEPTED, actual.getStatusCode()); final ResponseEntity actualResponse = - invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=count", Results.class); + testRestTemplateService.invokeHttpGet(url + "?resultIndex=0&resultSize=1&format=count", Results.class); assertEquals(HttpStatus.OK, actualResponse.getStatusCode()); assertTrue(actualResponse.hasBody()); @@ -146,28 +132,9 @@ public class ProjectControllerTest { assertEquals(1, result.getValues().get(0).get(Constants.PROJECT)); } - private ResponseEntity invokeHttpGet(final String url, final Class clazz) { - return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders(getUsername())), clazz); - } - private ResponseEntity invokeHttpPut(final String url, final Object obj) { - final HttpEntity httpEntity = getHttpEntity(obj); - return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, Void.class); + private String getUrl(final String... urls) { + return TestUtils.getUrl(port, urls); } - private HttpEntity getHttpEntity(final Object obj) { - return new HttpEntity<>(obj, getHttpHeaders(getUsername())); - } - - private String getUsername() { - return userCredentials.getUsers().iterator().next().getUsername(); - } - - private String getProjectEndPointUrl() { - return TestUtils.getBaseUrl(port) + Constants.PROJECT_URL; - } - - private String getRelationship() throws IOException, Exception { - return TestUtils.getJsonString(PROJECT_RELATION_SHIP_JSON_FILE); - } } diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/utils/TestConstants.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/utils/TestConstants.java index 5bb759fc..339fccc9 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/utils/TestConstants.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/utils/TestConstants.java @@ -58,6 +58,8 @@ public class TestConstants { public static final String RELATED_TO_URL = "/related-to" + GENERIC_VNFS_URL; public static final String PLATFORM_NAME = "PLATFORM_APP_ID_1"; + + public static final String LINE_OF_BUSINESS_NAME = "LINE_OF_BUSINESS_1"; private TestConstants() {} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/utils/TestRestTemplateService.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/utils/TestRestTemplateService.java new file mode 100644 index 00000000..698473ff --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/utils/TestRestTemplateService.java @@ -0,0 +1,63 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.aaisimulator.utils; + +import org.onap.so.simulator.model.UserCredentials; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ + +@Service +public class TestRestTemplateService { + + @Autowired + private TestRestTemplate restTemplate; + + @Autowired + private UserCredentials userCredentials; + + + public ResponseEntity invokeHttpGet(final String url, final Class clazz) { + return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), clazz); + } + + public ResponseEntity invokeHttpPut(final String url, final Object obj, final Class clazz) { + final HttpEntity httpEntity = getHttpEntity(obj); + return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, clazz); + } + + private HttpEntity getHttpEntity(final Object obj) { + return new HttpEntity<>(obj, getHttpHeaders()); + } + + private HttpHeaders getHttpHeaders() { + return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername()); + } + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/utils/TestUtils.java b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/utils/TestUtils.java index 880e7995..c94baac4 100644 --- a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/utils/TestUtils.java +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/java/org/onap/so/aaisimulator/utils/TestUtils.java @@ -96,6 +96,27 @@ public class TestUtils { return getJsonString("test-data/platform-relationship.json"); } + public static String getLineOfBusiness() throws IOException { + return getJsonString("test-data/line-of-business.json"); + } + + public static String getBusinessProject() throws IOException { + return getJsonString("test-data/business-project.json"); + } + + public static String getBusinessProjectRelationship() throws IOException { + return getJsonString("test-data/business-project-relation-ship.json"); + } + + public static String getOwningEntityRelationship() throws IOException { + return getJsonString("test-data/owning-entity-relation-ship.json"); + } + + public static String getOwningEntity() throws IOException { + return getJsonString("test-data/owning-entity.json"); + + } + public static String getUrl(final int port, final String... urls) { final UriComponentsBuilder baseUri = UriComponentsBuilder.fromUriString("https://localhost:" + port); for (final String url : urls) { diff --git a/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/line-of-business.json b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/line-of-business.json new file mode 100644 index 00000000..61746a24 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/aai-simulator/src/test/resources/test-data/line-of-business.json @@ -0,0 +1,3 @@ +{ + "line-of-business-name": "LINE_OF_BUSINESS_1" +} -- 2.16.6