Fixed functionality of Workflow API 07/55207/2
authoravigaffa <avi.gaffa@amdocs.com>
Thu, 21 Jun 2018 14:32:00 +0000 (17:32 +0300)
committeravigaffa <avi.gaffa@amdocs.com>
Thu, 21 Jun 2018 14:42:17 +0000 (17:42 +0300)
renaming Exceptions package to lower case

Change-Id: Ib1395c15012d393be913d6588454b3b4b7c4b075
Issue-ID: SDC-1396
Signed-off-by: avigaffa <avi.gaffa@amdocs.com>
workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/WorkflowController.java
workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/exceptionshandlers/CustomizedResponseEntityExceptionHandler.java [new file with mode: 0644]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/impl/WorkflowControllerImpl.java
workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/UniqueValueService.java
workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/errors/WorkflowNotFoundException.java [deleted file]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/UniqueValueViolationException.java [moved from workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/errors/UniqueValueViolationException.java with 60% similarity]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/WorkflowNotFoundException.java [new file with mode: 0644]
workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/impl/WorkflowManagerImpl.java
workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerTest.java
workflow-designer-be/src/test/java/org/onap/sdc/workflow/services/UniqueValueServiceTest.java

index 9065770..95f11cd 100644 (file)
@@ -6,6 +6,8 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.onap.sdc.workflow.api.types.CollectionWrapper;
 import org.onap.sdc.workflow.persistence.types.Workflow;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
@@ -18,19 +20,19 @@ import org.springframework.web.bind.annotation.RequestMapping;
 @Api("Workflows")
 public interface WorkflowController {
 
-    @GetMapping
+    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
     @ApiOperation("List workflows")
     CollectionWrapper<Workflow> list(@RequestHeader(USER_ID_HEADER_PARAM) String user);
 
-    @PostMapping
+    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
     @ApiOperation("Create workflow")
-    Workflow create(@RequestBody Workflow workflow, @RequestHeader(USER_ID_HEADER_PARAM) String user);
+    ResponseEntity<?> create(@RequestBody Workflow workflow, @RequestHeader(USER_ID_HEADER_PARAM) String user);
 
-    @GetMapping("/{id}")
+    @GetMapping(path = "/{id}")
     @ApiOperation("Get workflow")
     Workflow get(@PathVariable("id") String id, @RequestHeader(USER_ID_HEADER_PARAM) String user);
 
-    @PutMapping("/{id}")
+    @PutMapping(path = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
     @ApiOperation("Update workflow")
     Workflow update(@RequestBody Workflow workflow, @PathVariable("id") String id,
             @RequestHeader(USER_ID_HEADER_PARAM) String user);
diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/exceptionshandlers/CustomizedResponseEntityExceptionHandler.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/api/exceptionshandlers/CustomizedResponseEntityExceptionHandler.java
new file mode 100644 (file)
index 0000000..c29d2ab
--- /dev/null
@@ -0,0 +1,29 @@
+package org.onap.sdc.workflow.api.exceptionshandlers;
+
+import static org.springframework.http.HttpStatus.NOT_FOUND;
+import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY;
+
+import org.onap.sdc.workflow.services.exceptions.UniqueValueViolationException;
+import org.onap.sdc.workflow.services.exceptions.WorkflowNotFoundException;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
+
+@ControllerAdvice
+@RestController
+public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
+
+    @ExceptionHandler(UniqueValueViolationException.class)
+    public final ResponseEntity<String> handleUniqueValueViolationException(
+            UniqueValueViolationException exception) {
+        return new ResponseEntity<>(exception.getMessage(), UNPROCESSABLE_ENTITY);
+    }
+
+    @ExceptionHandler(WorkflowNotFoundException.class)
+    public final ResponseEntity<String> handleWorkflowNotFoundException(
+            WorkflowNotFoundException exception) {
+        return new ResponseEntity<>(exception.getMessage(), NOT_FOUND);
+    }
+}
index b7a122b..f0b9f3e 100644 (file)
@@ -8,6 +8,8 @@ import org.onap.sdc.workflow.persistence.types.Workflow;
 import org.onap.sdc.workflow.services.WorkflowManager;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestHeader;
@@ -29,9 +31,9 @@ public class WorkflowControllerImpl implements WorkflowController {
     }
 
     @Override
-    public Workflow create(@RequestBody Workflow workflow, @RequestHeader(USER_ID_HEADER_PARAM) String user) {
+    public ResponseEntity<?> create(@RequestBody Workflow workflow, @RequestHeader(USER_ID_HEADER_PARAM) String user) {
         workflowManager.create(workflow);
-        return workflow;
+        return new ResponseEntity<>(workflow, HttpStatus.CREATED);
     }
 
     @Override
index 9a8eee2..a9acb81 100644 (file)
@@ -1,11 +1,10 @@
 package org.onap.sdc.workflow.services;
 
-import com.google.common.annotations.VisibleForTesting;
 import java.util.Optional;
 import org.apache.commons.lang.ArrayUtils;
 import org.onap.sdc.workflow.persistence.UniqueValueRepository;
 import org.onap.sdc.workflow.persistence.types.UniqueValueEntity;
-import org.onap.sdc.workflow.services.errors.UniqueValueViolationException;
+import org.onap.sdc.workflow.services.exceptions.UniqueValueViolationException;
 import org.openecomp.core.utilities.CommonMethods; // todo get rid of
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/errors/WorkflowNotFoundException.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/errors/WorkflowNotFoundException.java
deleted file mode 100644 (file)
index 46a6d5e..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-package org.onap.sdc.workflow.services.errors;
-
-import static org.springframework.http.HttpStatus.NOT_FOUND;
-
-import org.springframework.web.bind.annotation.ResponseStatus;
-
-@ResponseStatus(NOT_FOUND)
-public class WorkflowNotFoundException extends RuntimeException {
-
-    public WorkflowNotFoundException(String workflowId) {
-        super(String.format("Workflow with id %s does not exist", workflowId));
-    }
-}
@@ -1,11 +1,6 @@
-package org.onap.sdc.workflow.services.errors;
+package org.onap.sdc.workflow.services.exceptions;
 
 
-import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY;
-
-import org.springframework.web.bind.annotation.ResponseStatus;
-
-@ResponseStatus(UNPROCESSABLE_ENTITY)
 public class UniqueValueViolationException extends RuntimeException {
 
     private static final String UNIQUE_VALUE_VIOLATION_MSG = "%s with the value '%s' already exists.";
diff --git a/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/WorkflowNotFoundException.java b/workflow-designer-be/src/main/java/org/onap/sdc/workflow/services/exceptions/WorkflowNotFoundException.java
new file mode 100644 (file)
index 0000000..13c8021
--- /dev/null
@@ -0,0 +1,8 @@
+package org.onap.sdc.workflow.services.exceptions;
+
+public class WorkflowNotFoundException extends RuntimeException {
+
+    public WorkflowNotFoundException(String workflowId) {
+        super(String.format("Workflow with id '%s' does not exist", workflowId));
+    }
+}
index 2afaaa0..d5d9851 100644 (file)
@@ -6,7 +6,7 @@ import org.onap.sdc.workflow.services.mappers.WorkflowMapper;
 import org.onap.sdc.workflow.persistence.types.Workflow;
 import org.onap.sdc.workflow.services.UniqueValueService;
 import org.onap.sdc.workflow.services.WorkflowManager;
-import org.onap.sdc.workflow.services.errors.WorkflowNotFoundException;
+import org.onap.sdc.workflow.services.exceptions.WorkflowNotFoundException;
 import org.openecomp.sdc.versioning.ItemManager;
 import org.openecomp.sdc.versioning.types.Item;
 import org.openecomp.sdc.versioning.types.ItemStatus;
@@ -17,7 +17,7 @@ import org.springframework.stereotype.Service;
 @Service("workflowManager")
 public class WorkflowManagerImpl implements WorkflowManager {
 
-    public static final String WORKFLOW_TYPE = "WORKFLOW";
+    private static final String WORKFLOW_TYPE = "WORKFLOW";
     private static final String WORKFLOW_NAME_UNIQUE_TYPE = "WORKFLOW_NAME";
     private final ItemManager itemManager;
     private final UniqueValueService uniqueValueService;
index 81ec82e..0885d47 100644 (file)
@@ -103,7 +103,7 @@ public class WorkflowControllerTest {
         Workflow reqWorkflow = createWorkflow(1, false);
         mockMvc.perform(
                 post(WORKFLOWS_URL).header(RestConstants.USER_ID_HEADER_PARAM, USER_ID).contentType(APPLICATION_JSON)
-                                   .content(GSON.toJson(reqWorkflow))).andDo(print()).andExpect(status().isOk())
+                                   .content(GSON.toJson(reqWorkflow))).andDo(print()).andExpect(status().isCreated())
                .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE));
         verify(workflowManagerMock, times(1)).create(reqWorkflow);
     }
index 50a1965..4911060 100644 (file)
@@ -18,7 +18,7 @@ import org.mockito.MockitoAnnotations;
 import org.mockito.Spy;
 import org.onap.sdc.workflow.persistence.UniqueValueRepository;
 import org.onap.sdc.workflow.persistence.types.UniqueValueEntity;
-import org.onap.sdc.workflow.services.errors.UniqueValueViolationException;
+import org.onap.sdc.workflow.services.exceptions.UniqueValueViolationException;
 
 public class UniqueValueServiceTest {