diff options
author | NicolasLaplaud <nicolas.laplaud@orange.com> | 2018-09-07 16:18:11 +0200 |
---|---|---|
committer | MatthieuGeerebaert <matthieu.geerebaert@orange.com> | 2018-09-12 16:52:30 +0200 |
commit | d04ad9a5abd23a93e948ebac2cbe5ae1925e2099 (patch) | |
tree | 007324164767c6d5efd4d75e66d92176322e70ff | |
parent | 5bfec6934fc307aecadca43e75b3c04d647bf005 (diff) |
Add HubRessource Test
- Improve junit test coverage on HubResource method
Change-Id: I90e96ed57b4cb781a2454ac26953f5384a60c032
Issue-ID: EXTAPI-135
Signed-off-by: NicolasLaplaud <nicolas.laplaud@orange.com>
4 files changed, 182 insertions, 34 deletions
diff --git a/src/main/java/org/onap/nbi/apis/hub/HubResource.java b/src/main/java/org/onap/nbi/apis/hub/HubResource.java index 811dd51..bd98820 100755 --- a/src/main/java/org/onap/nbi/apis/hub/HubResource.java +++ b/src/main/java/org/onap/nbi/apis/hub/HubResource.java @@ -1,20 +1,21 @@ /** - * Copyright (c) 2018 Orange + * Copyright (c) 2018 Orange * - * 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 + * 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 + * 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. + * 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. */ package org.onap.nbi.apis.hub; +import java.net.URI; +import java.util.List; +import java.util.stream.Collectors; import org.onap.nbi.apis.hub.model.Subscriber; import org.onap.nbi.apis.hub.model.Subscription; import org.onap.nbi.apis.hub.service.SubscriptionService; @@ -32,12 +33,17 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.util.MultiValueMap; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; -import java.net.URI; -import java.util.List; - @RestController @RequestMapping("/hub") @EnableScheduling @@ -60,10 +66,7 @@ public class HubResource extends ResourceManagement { Subscriber subscriber = subscriptionService.createSubscription(subscription); - URI location = ServletUriComponentsBuilder - .fromCurrentRequest() - .path("/{id}") - .buildAndExpand(subscriber.getId()) + URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(subscriber.getId()) .toUri(); return ResponseEntity.created(location).build(); @@ -72,7 +75,7 @@ public class HubResource extends ResourceManagement { @GetMapping(value = "/{subscriptionId}", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Subscription> getSubscription(@PathVariable String subscriptionId) { - Subscriber subscriber = subscriptionService.findSubscriptionById(subscriptionId); + Subscriber subscriber = subscriptionService.findSubscriptionById(subscriptionId); if (subscriber == null) { return ResponseEntity.notFound().build(); } @@ -90,7 +93,10 @@ public class HubResource extends ResourceManagement { headers.add("X-Total-Count", String.valueOf(totalCount)); headers.add("X-Result-Count", String.valueOf(subscribers.size())); - return this.findResponse(subscribers, filter, headers); + List<Subscription> subscriptions = + subscribers.stream().map(Subscription::createFromSubscriber).collect(Collectors.toList()); + + return this.findResponse(subscriptions, filter, headers); } diff --git a/src/main/java/org/onap/nbi/apis/hub/service/SubscriptionService.java b/src/main/java/org/onap/nbi/apis/hub/service/SubscriptionService.java index 65eaea3..cf9ab66 100644 --- a/src/main/java/org/onap/nbi/apis/hub/service/SubscriptionService.java +++ b/src/main/java/org/onap/nbi/apis/hub/service/SubscriptionService.java @@ -40,6 +40,10 @@ public class SubscriptionService { subscriberRepository.delete(subscriptionId); } + public void deleteAll() { + subscriberRepository.deleteAll(); + } + public long countSubscription(){ return subscriberRepository.count(); } diff --git a/src/test/java/org/onap/nbi/apis/ApiTest.java b/src/test/java/org/onap/nbi/apis/ApiTest.java index 489af4e..22e6200 100644 --- a/src/test/java/org/onap/nbi/apis/ApiTest.java +++ b/src/test/java/org/onap/nbi/apis/ApiTest.java @@ -16,8 +16,6 @@ package org.onap.nbi.apis; -import static org.assertj.core.api.Assertions.assertThat; - import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.http.ResponseDefinition; import com.github.tomakehurst.wiremock.stubbing.ListStubMappingsResult; @@ -30,25 +28,22 @@ import java.util.Set; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; + +import org.junit.*; import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.onap.nbi.apis.assertions.HubAssertions; import org.onap.nbi.apis.assertions.ServiceCatalogAssertions; import org.onap.nbi.apis.assertions.ServiceInventoryAssertions; import org.onap.nbi.apis.assertions.ServiceOrderAssertions; +import org.onap.nbi.apis.hub.HubResource; +import org.onap.nbi.apis.hub.model.Subscriber; +import org.onap.nbi.apis.hub.model.Subscription; +import org.onap.nbi.apis.hub.service.SubscriptionService; import org.onap.nbi.apis.servicecatalog.ServiceSpecificationResource; import org.onap.nbi.apis.serviceinventory.ServiceInventoryResource; import org.onap.nbi.apis.serviceorder.ServiceOrderResource; -import org.onap.nbi.apis.serviceorder.model.ActionType; -import org.onap.nbi.apis.serviceorder.model.RelatedParty; -import org.onap.nbi.apis.serviceorder.model.Service; -import org.onap.nbi.apis.serviceorder.model.ServiceOrder; -import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem; -import org.onap.nbi.apis.serviceorder.model.ServiceSpecificationRef; -import org.onap.nbi.apis.serviceorder.model.StateType; +import org.onap.nbi.apis.serviceorder.model.*; import org.onap.nbi.apis.serviceorder.model.orchestrator.ExecutionTask; import org.onap.nbi.apis.serviceorder.repositories.ExecutionTaskRepository; import org.onap.nbi.apis.serviceorder.repositories.ServiceOrderRepository; @@ -57,9 +52,15 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.ResponseEntity; +import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @@ -82,6 +83,12 @@ public class ApiTest { ServiceOrderResource serviceOrderResource; @Autowired + HubResource hubResource; + + @Autowired + SubscriptionService subscriptionService; + + @Autowired ServiceOrderRepository serviceOrderRepository; @Autowired @@ -90,8 +97,17 @@ public class ApiTest { @Autowired SOTaskProcessor SoTaskProcessor; + @Mock + private RequestAttributes attrs; + static Validator validator; + @Before + public void before() { + MockHttpServletRequest request = new MockHttpServletRequest(); + RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); + } + @BeforeClass public static void setUp() throws Exception { wireMockServer.start(); @@ -109,6 +125,7 @@ public class ApiTest { public void tearsDownUpPort() throws Exception { executionTaskRepository.deleteAll(); serviceOrderRepository.deleteAll(); + subscriptionService.deleteAll(); wireMockServer.resetToDefaultMappings(); } @@ -1229,6 +1246,82 @@ public class ApiTest { } + // hub + + @Test + public void testFindWhenNoSubscriber() throws Exception { + ResponseEntity<Object> findResponseEntity = hubResource.findSubscribers(new LinkedMultiValueMap<>()); + assertThat(findResponseEntity.getStatusCodeValue()).isEqualTo(200); + ArrayList subscribers = (ArrayList) findResponseEntity.getBody(); + assertThat(subscribers.size()).isEqualTo(0); + } + + @Test + public void testSubscriberCreation() throws Exception { + ResponseEntity<Subscriber> firstCreationResponseEntity = hubResource + .createEventSubscription(HubAssertions.createServiceOrderCreationSubscription()); + assertThat(firstCreationResponseEntity.getStatusCodeValue()).isEqualTo(201); + assertThat(firstCreationResponseEntity.getHeaders().getLocation()).isNotNull(); + assertThat(subscriptionService.countSubscription()).isEqualTo(1); + } + + @Test + public void testCreationAndFindSubscriber() throws Exception { + ResponseEntity<Subscriber> firstCreationResponseEntity = hubResource + .createEventSubscription(HubAssertions.createServiceOrderCreationSubscription()); + ResponseEntity<Object> findResponseEntity = hubResource.findSubscribers(new LinkedMultiValueMap<>()); + ArrayList subscribers = (ArrayList) findResponseEntity.getBody(); + assertThat(subscribers.size()).isEqualTo(1); + } + + @Test + public void testCreationAndGetByIdSubscriber() throws Exception { + ResponseEntity<Subscriber> createResponseEntity = hubResource + .createEventSubscription(HubAssertions.createServiceOrderCreationSubscription()); + String resourceId = createResponseEntity.getHeaders().getLocation().getPath().substring(1); + ResponseEntity<Subscription> getResponseEntity = hubResource.getSubscription(resourceId); + assertThat(getResponseEntity.getStatusCodeValue()).isEqualTo(200); + assertThat(getResponseEntity.getBody()).isInstanceOf(Subscription.class); + } + + @Test + public void testMultiCreationAndFindSubscriber() throws Exception { + hubResource.createEventSubscription(HubAssertions.createServiceOrderCreationSubscription()); + hubResource.createEventSubscription(HubAssertions.createServiceOrderStateChangeSubscription()); + hubResource.createEventSubscription(HubAssertions.createServiceOrderItemStateChangeSubscription()); + + ResponseEntity<Object> findAllResponseEntity = hubResource.findSubscribers(new LinkedMultiValueMap<>()); + ArrayList subscribers = (ArrayList) findAllResponseEntity.getBody(); + assertThat(subscribers.size()).isEqualTo(3); + } + + @Test + public void testMultiCreationAndFindWithFilteringSubscriber() throws Exception { + hubResource.createEventSubscription(HubAssertions.createServiceOrderCreationSubscription()); + hubResource.createEventSubscription(HubAssertions.createServiceOrderStateChangeSubscription()); + hubResource.createEventSubscription(HubAssertions.createServiceOrderItemStateChangeSubscription()); + MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); + params.add("query.eventType", "ServiceOrderCreationNotification"); + ResponseEntity<Object> findWithFilterResponseEntity = hubResource.findSubscribers(params); + ArrayList subscribers = (ArrayList) findWithFilterResponseEntity.getBody(); + assertThat(subscribers.size()).isEqualTo(1); + } + @Test + public void testSubscriberDeletion() throws Exception { + ResponseEntity<Subscriber> createResponseEntity = hubResource + .createEventSubscription(HubAssertions.createServiceOrderCreationSubscription()); + String resourceId = createResponseEntity.getHeaders().getLocation().getPath().substring(1); + + ResponseEntity<Object> findResponseEntity = hubResource.findSubscribers(new LinkedMultiValueMap<>()); + ArrayList subscribers = (ArrayList) findResponseEntity.getBody(); + assertThat(subscribers.size()).isEqualTo(1); + + hubResource.deleteSubscription(resourceId); + + findResponseEntity = hubResource.findSubscribers(new LinkedMultiValueMap<>()); + subscribers = (ArrayList) findResponseEntity.getBody(); + assertThat(subscribers).isEmpty(); + } } diff --git a/src/test/java/org/onap/nbi/apis/assertions/HubAssertions.java b/src/test/java/org/onap/nbi/apis/assertions/HubAssertions.java new file mode 100644 index 0000000..486c4ef --- /dev/null +++ b/src/test/java/org/onap/nbi/apis/assertions/HubAssertions.java @@ -0,0 +1,45 @@ +/** + * Copyright (c) 2018 Orange + * + * 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. + */ +package org.onap.nbi.apis.assertions; + +import org.onap.nbi.apis.hub.model.Subscription; + +public class HubAssertions { + + public static Subscription createServiceOrderCreationSubscription(){ + Subscription subscription = new Subscription(); + subscription.setId("id"); + subscription.setCallback("http://localhost:8090"); + subscription.setQuery("eventType = ServiceOrderCreationNotification"); + return subscription; + } + + public static Subscription createServiceOrderStateChangeSubscription(){ + Subscription subscription = new Subscription(); + subscription.setId("id"); + subscription.setCallback("http://localhost:8090"); + subscription.setQuery("eventType = ServiceOrderStateChangeNotification"); + return subscription; + } + + public static Subscription createServiceOrderItemStateChangeSubscription(){ + Subscription subscription = new Subscription(); + subscription.setId("id"); + subscription.setCallback("http://localhost:8090"); + subscription.setQuery("eventType = ServiceOrderItemStateChangeNotification"); + return subscription; + } +} |