From cc78e0cd74b11e96d3f9c3e9fa60a7330c9f75b0 Mon Sep 17 00:00:00 2001 From: Tomasz Golabek Date: Wed, 21 Aug 2019 09:18:38 +0200 Subject: unit tests - sdc-common-rest Additional junit tests Change-Id: Ieeb6f02e1d5abf131882c530a0ba3a395d56c6cd Issue-ID: SDC-2326 Signed-off-by: Tomasz Golabek --- .../sdcrests/errors/ZusammenExceptionMapper.java | 10 +-- .../errors/DefaultExceptionMapperTest.java | 85 ++++++++++++++++++++++ .../errors/ZusammenExceptionMapperTest.java | 67 +++++++++++++++++ .../wrappers/GenericCollectionWrapperTest.java | 34 +++++++++ .../wrappers/StringWrapperResponseTest.java | 32 ++++++++ 5 files changed, 223 insertions(+), 5 deletions(-) create mode 100644 openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/errors/DefaultExceptionMapperTest.java create mode 100644 openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/errors/ZusammenExceptionMapperTest.java create mode 100644 openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/wrappers/GenericCollectionWrapperTest.java create mode 100644 openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/wrappers/StringWrapperResponseTest.java (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src') diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/ZusammenExceptionMapper.java b/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/ZusammenExceptionMapper.java index 4100cc5bf7..abf6024701 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/ZusammenExceptionMapper.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/java/org/openecomp/sdcrests/errors/ZusammenExceptionMapper.java @@ -33,15 +33,15 @@ import org.openecomp.sdc.logging.api.LoggerFactory; public class ZusammenExceptionMapper implements ExceptionMapper { 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 index 0000000000..8634cd3720 --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/errors/DefaultExceptionMapperTest.java @@ -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 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> 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 index 0000000000..9c3c4b2e72 --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/errors/ZusammenExceptionMapperTest.java @@ -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 index 0000000000..86d0257eeb --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/wrappers/GenericCollectionWrapperTest.java @@ -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 index 0000000000..9b33434328 --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/test/java/org/openecomp/sdcrests/wrappers/StringWrapperResponseTest.java @@ -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 -- cgit 1.2.3-korg