diff options
Diffstat (limited to 'src/main/java')
3 files changed, 38 insertions, 18 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); } } |