diff options
5 files changed, 56 insertions, 21 deletions
diff --git a/src/main/java/org/onap/nbi/apis/hub/model/Subscription.java b/src/main/java/org/onap/nbi/apis/hub/model/Subscription.java index 53f33ab..34e23e1 100755 --- a/src/main/java/org/onap/nbi/apis/hub/model/Subscription.java +++ b/src/main/java/org/onap/nbi/apis/hub/model/Subscription.java @@ -16,8 +16,9 @@ package org.onap.nbi.apis.hub.model; import java.util.stream.Collectors; +import org.onap.nbi.commons.Resource; -public class Subscription { +public class Subscription implements Resource{ private String id; 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 09826bc..550c185 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 @@ -1,25 +1,28 @@ /** - * 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.service; +import com.google.common.collect.Lists; +import java.text.MessageFormat; +import java.util.List; import org.onap.nbi.apis.hub.model.Subscriber; import org.onap.nbi.apis.hub.model.Subscription; import org.onap.nbi.apis.hub.repository.SubscriberRepository; +import org.onap.nbi.exceptions.ValidationException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Example; import org.springframework.stereotype.Service; +import org.springframework.validation.ObjectError; @Service public class SubscriptionService { @@ -27,16 +30,32 @@ public class SubscriptionService { @Autowired SubscriberRepository subscriberRepository; - public Subscriber findSubscriptionById(String subscriptionId){ + public Subscriber findSubscriptionById(String subscriptionId) { return subscriberRepository.findOne(subscriptionId); } - public Subscriber createSubscription(Subscription subscription){ - Subscriber sub = Subscriber.createFromSubscription(subscription); - return subscriberRepository.save(sub); + public Subscriber createSubscription(Subscription subscription) { + subscription.setId(null); + Subscriber subscriber = Subscriber.createFromSubscription(subscription); + if (isSubscriberAlreadyExisting(subscriber)) { + String message = MessageFormat + .format("subscription with callback {0} and query {1} already exists", subscription.getCallback(), + subscription.getQuery()); + ObjectError error = new ObjectError("subscription", message); + List<ObjectError> errors = Lists.newArrayList(error); + throw new ValidationException(errors); + } else { + return subscriberRepository.save(subscriber); + } } - public void deleteSubscription(String subscriptionId){ + private boolean isSubscriberAlreadyExisting(Subscriber subscriber) { + Example<Subscriber> subscriberExample = Example.of(subscriber); + Subscriber subscriberAlreadyExisting = subscriberRepository.findOne(subscriberExample); + return subscriberAlreadyExisting != null; + } + + public void deleteSubscription(String subscriptionId) { subscriberRepository.delete(subscriptionId); } @@ -44,7 +63,7 @@ public class SubscriptionService { subscriberRepository.deleteAll(); } - public long countSubscription(){ + public long countSubscription() { return subscriberRepository.count(); } diff --git a/src/main/java/org/onap/nbi/exceptions/ApiExceptionHandler.java b/src/main/java/org/onap/nbi/exceptions/ApiExceptionHandler.java index fa2a65b..e008f09 100644 --- a/src/main/java/org/onap/nbi/exceptions/ApiExceptionHandler.java +++ b/src/main/java/org/onap/nbi/exceptions/ApiExceptionHandler.java @@ -54,6 +54,6 @@ public class ApiExceptionHandler { @ResponseBody public ResponseEntity<ApiError> validationExceptionHandler(final ValidationException exception) { ApiError apiError = new ApiError("400", HttpStatus.BAD_REQUEST.getReasonPhrase(), exception.getMessages(), ""); - return new ResponseEntity<>(apiError, HttpStatus.INTERNAL_SERVER_ERROR); + return new ResponseEntity<>(apiError, HttpStatus.BAD_REQUEST); } } diff --git a/src/test/resources/karatetest/data/subscriber.json b/src/test/resources/karatetest/data/subscriber.json index e7c08ad..8fa7370 100644 --- a/src/test/resources/karatetest/data/subscriber.json +++ b/src/test/resources/karatetest/data/subscriber.json @@ -1,16 +1,13 @@ [ { - "id": "id", "callback": "http://localhost:8080/test", "query": "eventType = ServiceOrderCreationNotification" }, { - "id": "id", "callback": "http://localhost/test", "query": "eventType=ServiceOrderStateChangeNotification" }, { - "id": "id", "callback": "http://localhost/test", "query": "eventType=ServiceOrderItemStateChangeNotification" } diff --git a/src/test/resources/karatetest/features/03--Subscriber.feature b/src/test/resources/karatetest/features/03--Subscriber.feature index 0d21ccb..6e4b884 100644 --- a/src/test/resources/karatetest/features/03--Subscriber.feature +++ b/src/test/resources/karatetest/features/03--Subscriber.feature @@ -38,6 +38,24 @@ Given url location When method delete Then status 204 +Scenario: testCreation2SameSubscribers +Given path 'hub' +And request data[0] +When method post +Then status 201 +And def location = responseHeaders['Location'][0] +Given path 'hub' +And request data[0] +When method post +Then status 400 +And match $ contains { message : 'Bad Request'} +Given path 'hub' +When method get +And match $ == '#[1]' +Given url location +When method delete +Then status 204 + Scenario: testGetByIdSubscriber Given path 'hub' And request data[0] |