unit tests - sdc-common-rest 19/94019/2
authorTomasz Golabek <tomasz.golabek@nokia.com>
Wed, 21 Aug 2019 07:18:38 +0000 (09:18 +0200)
committerPiotr Darosz <piotr.darosz@nokia.com>
Wed, 21 Aug 2019 19:22:46 +0000 (19:22 +0000)
Additional junit tests

Change-Id: Ieeb6f02e1d5abf131882c530a0ba3a395d56c6cd
Issue-ID: SDC-2326
Signed-off-by: Tomasz Golabek <tomasz.golabek@nokia.com>
openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/pom.xml
openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/ZusammenExceptionMapper.java
openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/errors/DefaultExceptionMapperTest.java [new file with mode: 0644]
openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/errors/ZusammenExceptionMapperTest.java [new file with mode: 0644]
openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/wrappers/GenericCollectionWrapperTest.java [new file with mode: 0644]
openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/wrappers/StringWrapperResponseTest.java [new file with mode: 0644]

index 71552b7..c9c70ab 100644 (file)
         <version>1.5.1-SNAPSHOT</version>
     </parent>
 
+    <properties>
+        <jersey-common.version>2.22.2</jersey-common.version>
+    </properties>
+
     <dependencies>
         <dependency>
             <groupId>org.openecomp.sdc</groupId>
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.glassfish.jersey.core</groupId>
+            <artifactId>jersey-common</artifactId>
+            <version>${jersey-common.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.google.code.bean-matchers</groupId>
+            <artifactId>bean-matchers</artifactId>
+            <version>${bean-matchers.version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
 </project>
index 4100cc5..abf6024 100644 (file)
@@ -33,15 +33,15 @@ import org.openecomp.sdc.logging.api.LoggerFactory;
 
 public class ZusammenExceptionMapper implements ExceptionMapper<SdcRuntimeException> {
     private static final String ZUSAMMEN_DB_PREFIX = Module.ZDB + "-";
-    private static final String VLM_VSP_VERSION_ID_DOES_NOT_EXISTS =
+    static final String VLM_VSP_VERSION_ID_DOES_NOT_EXISTS =
             ZUSAMMEN_DB_PREFIX + com.amdocs.zusammen.datatypes.response.ErrorCode.ZU_ITEM_VERSION_NOT_EXIST;
-    private static final String VLM_VSP_ITEM_ID_DOES_NOT_EXISTS =
+    static final String VLM_VSP_ITEM_ID_DOES_NOT_EXISTS =
             ZUSAMMEN_DB_PREFIX + com.amdocs.zusammen.datatypes.response.ErrorCode.ZU_ITEM_DOES_NOT_EXIST;
-    private static final String SUB_ENTITY_ID_DOES_NOT_EXISTS =
+    static final String SUB_ENTITY_ID_DOES_NOT_EXISTS =
             ZUSAMMEN_DB_PREFIX + com.amdocs.zusammen.datatypes.response.ErrorCode.ZU_ELEMENT_GET_INFO;
-    private static final String FAILED_TO_SYNC =
+    static final String FAILED_TO_SYNC =
             ZUSAMMEN_DB_PREFIX + com.amdocs.zusammen.datatypes.response.ErrorCode.ZU_ITEM_VERSION_SYNC;
-    private static final String FAILED_TO_PUBLISH_OUT_OF_SYNC =
+    static final String FAILED_TO_PUBLISH_OUT_OF_SYNC =
             ZUSAMMEN_DB_PREFIX + com.amdocs.zusammen.datatypes.response.ErrorCode
                                               .ZU_ITEM_VERSION_PUBLISH_NOT_ALLOWED;
 
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/errors/DefaultExceptionMapperTest.java b/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/errors/DefaultExceptionMapperTest.java
new file mode 100644 (file)
index 0000000..8634cd3
--- /dev/null
@@ -0,0 +1,85 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2019 Nokia. All rights reserved.
+ * ================================================================================
+ * 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
+ *
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+package org.openecomp.sdcrests.errors;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.HashSet;
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.ConstraintViolationException;
+import javax.ws.rs.core.Response;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.hibernate.validator.internal.engine.path.PathImpl;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.openecomp.sdc.common.errors.CoreException;
+import org.openecomp.sdc.common.errors.ErrorCategory;
+import org.openecomp.sdc.common.errors.ErrorCode;
+import org.openecomp.sdc.common.errors.ErrorCode.ErrorCodeBuilder;
+
+@RunWith(MockitoJUnitRunner.class)
+public class DefaultExceptionMapperTest {
+
+    private static final String TEST_MESSAGE = "Test message";
+
+    @Mock
+    private ConstraintViolation<String> constraintViolation;
+    private PathImpl path = PathImpl.createRootPath();
+
+    @Test
+    public void shouldMapCoreExceptionToResponse() {
+        DefaultExceptionMapper defaultExceptionMapper = new DefaultExceptionMapper();
+        ErrorCode errorCode = new ErrorCodeBuilder().withId("VSP_NOT_FOUND").withCategory(ErrorCategory.APPLICATION).build();
+        CoreException exception = new CoreException(errorCode);
+        Response response = defaultExceptionMapper.toResponse(exception);
+        assertEquals(response.getStatus(), 404);
+    }
+
+    @Test
+    public void shouldMapConstraintViolationExceptionToResponse() {
+        Mockito.when(constraintViolation.getPropertyPath()).thenReturn(path);
+        DefaultExceptionMapper defaultExceptionMapper = new DefaultExceptionMapper();
+        Set<ConstraintViolation<String>> violations = new HashSet<>();
+        violations.add(constraintViolation);
+        ConstraintViolationException exception = new ConstraintViolationException(TEST_MESSAGE, violations);
+        Response response = defaultExceptionMapper.toResponse(exception);
+        assertEquals(response.getStatus(), 417);
+    }
+
+    @Test
+    public void shouldMapJsonMappingExceptionToResponse() {
+        DefaultExceptionMapper defaultExceptionMapper = new DefaultExceptionMapper();
+        JsonMappingException exception = new JsonMappingException(TEST_MESSAGE);
+        Response response = defaultExceptionMapper.toResponse(exception);
+        assertEquals(response.getStatus(), 417);
+    }
+
+    @Test
+    public void shouldMapOtherExceptionToResponse() {
+        DefaultExceptionMapper defaultExceptionMapper = new DefaultExceptionMapper();
+        Exception exception = new Exception(TEST_MESSAGE);
+        Response response = defaultExceptionMapper.toResponse(exception);
+        assertEquals(response.getStatus(), 500);
+    }
+}
\ No newline at end of file
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/errors/ZusammenExceptionMapperTest.java b/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/errors/ZusammenExceptionMapperTest.java
new file mode 100644 (file)
index 0000000..9c3c4b2
--- /dev/null
@@ -0,0 +1,67 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2019 Nokia. All rights reserved.
+ * ================================================================================
+ * 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
+ *
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+package org.openecomp.sdcrests.errors;
+
+import static org.junit.Assert.assertEquals;
+import static org.openecomp.sdcrests.errors.ZusammenExceptionMapper.FAILED_TO_PUBLISH_OUT_OF_SYNC;
+import static org.openecomp.sdcrests.errors.ZusammenExceptionMapper.FAILED_TO_SYNC;
+import static org.openecomp.sdcrests.errors.ZusammenExceptionMapper.SUB_ENTITY_ID_DOES_NOT_EXISTS;
+import static org.openecomp.sdcrests.errors.ZusammenExceptionMapper.VLM_VSP_ITEM_ID_DOES_NOT_EXISTS;
+
+import javax.ws.rs.core.Response;
+import org.junit.Test;
+import org.openecomp.sdc.common.errors.ErrorCodeAndMessage;
+import org.openecomp.sdc.common.errors.Messages;
+import org.openecomp.sdc.common.errors.SdcRuntimeException;
+
+public class ZusammenExceptionMapperTest {
+
+    @Test
+    public void shouldTransformENTITY_NOT_FOUND() {
+        ZusammenExceptionMapper zusammenExceptionMapper = new ZusammenExceptionMapper();
+        Response response = zusammenExceptionMapper
+            .toResponse(new SdcRuntimeException(VLM_VSP_ITEM_ID_DOES_NOT_EXISTS));
+        assertEquals(((ErrorCodeAndMessage)response.getEntity()).getErrorCode(), Messages.ENTITY_NOT_FOUND.name());
+    }
+
+    @Test
+    public void shouldTransformSUB_ENTITY_ID_DOES_NOT_EXISTS() {
+        ZusammenExceptionMapper zusammenExceptionMapper = new ZusammenExceptionMapper();
+        Response response = zusammenExceptionMapper
+            .toResponse(new SdcRuntimeException(SUB_ENTITY_ID_DOES_NOT_EXISTS));
+        assertEquals(((ErrorCodeAndMessage)response.getEntity()).getErrorCode(), Messages.SUB_ENTITY_NOT_FOUND.name());
+    }
+
+    @Test
+    public void shouldTransformFAILED_TO_SYNC() {
+        ZusammenExceptionMapper zusammenExceptionMapper = new ZusammenExceptionMapper();
+        Response response = zusammenExceptionMapper
+            .toResponse(new SdcRuntimeException(FAILED_TO_SYNC));
+        assertEquals(((ErrorCodeAndMessage)response.getEntity()).getErrorCode(), Messages.FAILED_TO_SYNC.name());
+    }
+
+    @Test
+    public void shouldTransformFAILED_TO_PUBLISH_OUT_OF_SYNC() {
+        ZusammenExceptionMapper zusammenExceptionMapper = new ZusammenExceptionMapper();
+        Response response = zusammenExceptionMapper
+            .toResponse(new SdcRuntimeException(FAILED_TO_PUBLISH_OUT_OF_SYNC));
+        assertEquals(((ErrorCodeAndMessage)response.getEntity()).getErrorCode(), Messages.FAILED_TO_PUBLISH_OUT_OF_SYNC.name());
+    }
+}
\ No newline at end of file
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/wrappers/GenericCollectionWrapperTest.java b/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/wrappers/GenericCollectionWrapperTest.java
new file mode 100644 (file)
index 0000000..86d0257
--- /dev/null
@@ -0,0 +1,34 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2019 Nokia. All rights reserved.
+ * ================================================================================
+ * 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
+ *
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+package org.openecomp.sdcrests.wrappers;
+
+
+import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+
+public class GenericCollectionWrapperTest {
+
+    @Test
+    public void shouldHaveValidGettersAndSetters() {
+        assertThat(GenericCollectionWrapper.class, hasValidGettersAndSetters());
+    }
+}
\ No newline at end of file
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/wrappers/StringWrapperResponseTest.java b/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/wrappers/StringWrapperResponseTest.java
new file mode 100644 (file)
index 0000000..9b33434
--- /dev/null
@@ -0,0 +1,32 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2019 Nokia. All rights reserved.
+ * ================================================================================
+ * 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
+ *
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+package org.openecomp.sdcrests.wrappers;
+
+import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+
+public class StringWrapperResponseTest {
+    @Test
+    public void shouldHaveValidGettersAndSetters() {
+        assertThat(GenericCollectionWrapper.class, hasValidGettersAndSetters());
+    }
+}
\ No newline at end of file