Fix response of fetch data type endpoint 24/131424/3
authorandre.schmid <andre.schmid@est.tech>
Tue, 11 Oct 2022 13:20:40 +0000 (14:20 +0100)
committerandre.schmid <andre.schmid@est.tech>
Wed, 12 Oct 2022 13:03:12 +0000 (14:03 +0100)
The catalog endpoint /v1/catalog/data-types/<id> is returning an
Optional instead of the Data Type itself.
Improves unit test to verify the response payload.

Change-Id: I781917fb66225abf911a043370917e7b7a183bb1
Issue-ID: SDC-4211
Signed-off-by: André Schmid <andre.schmid@est.tech>
catalog-be/src/main/java/org/openecomp/sdc/be/servlets/DataTypeServlet.java
catalog-be/src/test/java/org/openecomp/sdc/be/servlets/DataTypeServletTest.java

index 4456b83..d9846a7 100644 (file)
@@ -84,9 +84,9 @@ public class DataTypeServlet extends BeGenericServlet {
                                   @PathParam("dataTypeUid") String dataTypeUid) {
         final String url = request.getMethod() + " " + request.getRequestURI();
         log.debug("Start handle request of {} - modifier id is {}", url, userId);
-        final Optional<DataTypeDataDefinition> dataTypeByUid;
+        final Optional<DataTypeDataDefinition> dataTypeFoundOptional;
         try {
-            dataTypeByUid = dataTypeOperation.getDataTypeByUid(dataTypeUid);
+            dataTypeFoundOptional = dataTypeOperation.getDataTypeByUid(dataTypeUid);
         } catch (final BusinessException e) {
             throw e;
         } catch (final Exception ex) {
@@ -95,10 +95,8 @@ public class DataTypeServlet extends BeGenericServlet {
             log.error(EcompLoggerErrorCode.UNKNOWN_ERROR, this.getClass().getName(), errorMsg, ex);
             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
         }
-        if (dataTypeByUid.isEmpty()) {
-            throw new OperationException(ActionStatus.DATA_TYPE_NOT_FOUND, dataTypeUid);
-        }
-        return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), dataTypeByUid);
+        var dataType = dataTypeFoundOptional.orElseThrow(() -> new OperationException(ActionStatus.DATA_TYPE_NOT_FOUND, dataTypeUid));
+        return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), dataType);
     }
 
 }
index 39ba071..d916fff 100644 (file)
@@ -98,8 +98,10 @@ class DataTypeServletTest extends JerseySpringBaseTest {
 
     @Test
     void fetchDataTypeTest_Success() {
+        final DataTypeDataDefinition expectedDataType = new DataTypeDataDefinition();
+        expectedDataType.setUniqueId(DATA_TYPE_UID);
         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(new ResponseFormat(HttpStatus.SC_OK));
-        when(dataTypeOperation.getDataTypeByUid(DATA_TYPE_UID)).thenReturn(Optional.of(new DataTypeDataDefinition()));
+        when(dataTypeOperation.getDataTypeByUid(DATA_TYPE_UID)).thenReturn(Optional.of(expectedDataType));
 
         final Response response = target()
             .path(PATH)
@@ -108,6 +110,8 @@ class DataTypeServletTest extends JerseySpringBaseTest {
             .get(Response.class);
         assertNotNull(response);
         assertEquals(HttpStatus.SC_OK, response.getStatus());
+        final DataTypeDataDefinition actualDataType = response.readEntity(DataTypeDataDefinition.class);
+        assertEquals(expectedDataType.getUniqueId(), actualDataType.getUniqueId());
     }
 
     @Test