multiple identical hub 40/77640/1
authorromaingimbert <romain.gimbert@orange.com>
Thu, 31 Jan 2019 12:36:07 +0000 (13:36 +0100)
committerromaingimbert <romain.gimbert@orange.com>
Thu, 31 Jan 2019 12:39:01 +0000 (13:39 +0100)
-change hubresource
-add test

Change-Id: Ibb8ef363b84d9bcff83b1786cf7547a7660f957e
Issue-ID: EXTAPI-187
Signed-off-by: romaingimbert <romain.gimbert@orange.com>
src/main/java/org/onap/nbi/apis/hub/model/Subscription.java
src/main/java/org/onap/nbi/apis/hub/service/SubscriptionService.java
src/main/java/org/onap/nbi/exceptions/ApiExceptionHandler.java
src/test/resources/karatetest/data/subscriber.json
src/test/resources/karatetest/features/03--Subscriber.feature

index 53f33ab..34e23e1 100755 (executable)
@@ -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;
index 09826bc..550c185 100644 (file)
@@ -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();
     }
 
index fa2a65b..e008f09 100644 (file)
@@ -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);
     }
 }
index e7c08ad..8fa7370 100644 (file)
@@ -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"
   }
index 0d21ccb..6e4b884 100644 (file)
@@ -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]