From 8448309e0e1aed102a34bafbb53b1fd9b9600bfe Mon Sep 17 00:00:00 2001 From: Dmitry Puzikov Date: Mon, 23 Mar 2020 15:44:19 +0100 Subject: Fix unit tests Add asserts where required, fix tiny issues. Change-Id: I764fb6ab989e6271248a68f488929356ac2069c0 Issue-ID: SDC-2848 Signed-off-by: Dmitry Puzikov --- .../openecomp/sdc/be/ecomp/EcompIntImplTest.java | 20 +++++++++++++++++--- .../converters/AssetMetadataConverterTest.java | 21 +++++++++++++++++++-- .../be/ecomp/converters/EcompRoleConverterTest.java | 16 +++++++++++----- .../be/ecomp/converters/EcompUserConverterTest.java | 4 ++++ 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/ecomp/EcompIntImplTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/ecomp/EcompIntImplTest.java index d94bdeffc0..8db8450e87 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/ecomp/EcompIntImplTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/ecomp/EcompIntImplTest.java @@ -20,6 +20,9 @@ package org.openecomp.sdc.be.ecomp; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + import org.junit.Test; import org.mockito.Mockito; import org.onap.portalsdk.core.onboarding.exception.PortalAPIException; @@ -28,6 +31,8 @@ import org.onap.portalsdk.core.restful.domain.EcompUser; import javax.servlet.http.HttpServletRequest; import java.util.List; +import org.openecomp.sdc.be.user.Role; +import org.openecomp.sdc.common.api.Constants; public class EcompIntImplTest { @@ -84,7 +89,8 @@ public class EcompIntImplTest { // default test testSubject = createTestSubject(); - result = testSubject.getAvailableRoles("Mock"); + result = testSubject.getAvailableRoles("mock-id"); + assertThat(result).hasSameSizeAs(Role.values()); } @@ -104,19 +110,27 @@ public class EcompIntImplTest { EcompIntImpl testSubject; boolean result; HttpServletRequest httpServletRequestImpl = Mockito.mock(HttpServletRequest.class); + when(httpServletRequestImpl.getHeader("username")).thenReturn("mock-user"); + when(httpServletRequestImpl.getHeader("password")).thenReturn("mock-password"); + // default test - testSubject = createTestSubject(); + testSubject = createTestSubject(); result = testSubject.isAppAuthenticated(httpServletRequestImpl, null); + assertThat(result).isFalse(); } @Test public void testGetUserId() throws Exception { EcompIntImpl testSubject; - HttpServletRequest httpServletRequestImpl = Mockito.mock(HttpServletRequest.class); + String userId = "mock-user-id"; + HttpServletRequest httpServletRequestImpl = Mockito.mock(HttpServletRequest.class); + when(httpServletRequestImpl.getHeader(Constants.USER_ID_HEADER)).thenReturn(userId); + String result; // default test testSubject = createTestSubject(); result = testSubject.getUserId(httpServletRequestImpl); + assertThat(result).isEqualTo(userId); } } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/ecomp/converters/AssetMetadataConverterTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/ecomp/converters/AssetMetadataConverterTest.java index 22c8b94190..f89d899e00 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/ecomp/converters/AssetMetadataConverterTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/ecomp/converters/AssetMetadataConverterTest.java @@ -20,6 +20,8 @@ package org.openecomp.sdc.be.ecomp.converters; +import static org.assertj.core.api.Assertions.assertThat; + import fj.data.Either; import mockit.Deencapsulation; import org.junit.Assert; @@ -66,6 +68,7 @@ public class AssetMetadataConverterTest { testSubject = createTestSubject(); componentList = null; result = testSubject.convertToAssetMetadata(componentList, serverBaseURL, detailed); + assertThat(result.isLeft()).isTrue(); } @Test @@ -80,6 +83,7 @@ public class AssetMetadataConverterTest { // default test testSubject = createTestSubject(); result = testSubject.convertToSingleAssetMetadata(component, serverBaseURL, detailed); + assertThat(result.isLeft()).isTrue(); } @Test @@ -95,6 +99,7 @@ public class AssetMetadataConverterTest { testSubject = createTestSubject(); result = Deencapsulation.invoke(testSubject, "convertToMetadata", ComponentTypeEnum.RESOURCE, serverBaseURL, detailed, curr); + assertThat(result.isLeft()).isTrue(); } @Test @@ -108,6 +113,7 @@ public class AssetMetadataConverterTest { // default test testSubject = createTestSubject(); result = Deencapsulation.invoke(testSubject, "generateResourceMeatdata", serverBaseURL, true, curr); + assertThat(result.isLeft()).isTrue(); } @Test @@ -118,6 +124,7 @@ public class AssetMetadataConverterTest { // default test testSubject = createTestSubject(); result = Deencapsulation.invoke(testSubject, "createMetadaObject", true, ComponentTypeEnum.RESOURCE); + assertThat(result).isInstanceOf(AssetMetadata.class); } @Test @@ -134,6 +141,7 @@ public class AssetMetadataConverterTest { // default test testSubject = createTestSubject(); result = Deencapsulation.invoke(testSubject, "generateServiceMetadata", serverBaseURL, detailed, curr); + assertThat(result.isLeft()).isTrue(); } @Test @@ -147,6 +155,7 @@ public class AssetMetadataConverterTest { // default test testSubject = createTestSubject(); result = Deencapsulation.invoke(testSubject, "convertToAsset", asset, component, serverBaseURL, true); + assertThat(result).isInstanceOf(ResourceAssetMetadata.class); } @Test @@ -163,6 +172,7 @@ public class AssetMetadataConverterTest { testSubject = createTestSubject(); result = Deencapsulation.invoke(testSubject, "convertToResourceMetadata", assetToPopulate, resource, serverBaseURL, true); + assertThat(result).isInstanceOf(ResourceAssetMetadata.class); } @Test @@ -180,6 +190,7 @@ public class AssetMetadataConverterTest { testSubject = createTestSubject(); result = Deencapsulation.invoke(testSubject, "convertToServiceAssetMetadata", assetToPopulate, service, serverBaseURL, true); + assertThat(result).isInstanceOf(ServiceAssetMetadata.class); } @Test @@ -194,6 +205,7 @@ public class AssetMetadataConverterTest { testSubject = createTestSubject(); result = Deencapsulation.invoke(testSubject, "convertToResourceDetailedMetadata", assetToPopulate, resource, serverBaseURL); + assertThat(result.isLeft()).isTrue(); } @Test @@ -206,6 +218,7 @@ public class AssetMetadataConverterTest { // default test testSubject = createTestSubject(); result = Deencapsulation.invoke(testSubject, "convertToServiceDetailedMetadata", assetToPopulate, service); + assertThat(result.isLeft()).isTrue(); } @Test @@ -218,7 +231,8 @@ public class AssetMetadataConverterTest { // default test testSubject = createTestSubject(); - result = Deencapsulation.invoke(testSubject, "populateResourceWithArtifacts", asset, resource, artifacts); + result = Deencapsulation.invoke(testSubject, "populateResourceWithArtifacts", asset, resource, artifacts); + assertThat(result).isInstanceOf(ResourceAssetDetailedMetadata.class); } @Test @@ -234,6 +248,7 @@ public class AssetMetadataConverterTest { testSubject = createTestSubject(); result = Deencapsulation.invoke(testSubject, "populateServiceWithArtifacts", asset, Service.class, artifacts); + assertThat(result).isInstanceOf(ServiceAssetDetailedMetadata.class); } @Test @@ -246,7 +261,7 @@ public class AssetMetadataConverterTest { // test 1 testSubject = createTestSubject(); result = Deencapsulation.invoke(testSubject, "populateAssetWithArtifacts", component, artifacts); - Assert.assertEquals(null, result); + assertThat(result).isNull(); } @Test @@ -263,6 +278,7 @@ public class AssetMetadataConverterTest { testSubject = createTestSubject(); result = Deencapsulation.invoke(testSubject, "convertToArtifactMetadata", artifact, componentType, componentUUID, resourceInstanceName); + assertThat(result).isInstanceOf(ArtifactMetadata.class); } @Test @@ -277,5 +293,6 @@ public class AssetMetadataConverterTest { testSubject = createTestSubject(); result = Deencapsulation.invoke(testSubject, "convertToResourceInstanceMetadata", new Object[] { componentInstances, componentType, componentUUID }); + assertThat(result.isLeft()).isTrue(); } } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/ecomp/converters/EcompRoleConverterTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/ecomp/converters/EcompRoleConverterTest.java index ea4b085e5a..bbe0c176c8 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/ecomp/converters/EcompRoleConverterTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/ecomp/converters/EcompRoleConverterTest.java @@ -20,7 +20,10 @@ package org.openecomp.sdc.be.ecomp.converters; +import static org.assertj.core.api.Assertions.assertThat; + import org.junit.Test; +import org.onap.portalsdk.core.onboarding.exception.PortalAPIException; import org.onap.portalsdk.core.restful.domain.EcompRole; import org.openecomp.sdc.be.user.Role; @@ -34,12 +37,15 @@ public class EcompRoleConverterTest { // test 1 for (Role iterable_element : Role.values()) { ecompRole.setName(iterable_element.name()); - EcompRoleConverter.convertEcompRoleToRole(ecompRole); + result = EcompRoleConverter.convertEcompRoleToRole(ecompRole); + assertThat(result).isEqualTo(iterable_element.name()); } - - EcompRoleConverter.convertEcompRoleToRole(null); - - ecompRole.setId(new Long(4523535)); + } + + @Test(expected = PortalAPIException.class) + public void testConvertEcompRoleToRoleFailure() throws PortalAPIException { + EcompRole ecompRole = new EcompRole(); + ecompRole.setName("mock-name"); EcompRoleConverter.convertEcompRoleToRole(ecompRole); } } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/ecomp/converters/EcompUserConverterTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/ecomp/converters/EcompUserConverterTest.java index 74fe5205a6..1284f2e09a 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/ecomp/converters/EcompUserConverterTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/ecomp/converters/EcompUserConverterTest.java @@ -20,6 +20,8 @@ package org.openecomp.sdc.be.ecomp.converters; +import static org.assertj.core.api.Assertions.assertThat; + import fj.data.Either; import org.junit.Test; import org.onap.portalsdk.core.restful.domain.EcompUser; @@ -34,6 +36,7 @@ public class EcompUserConverterTest { // test 1 result = EcompUserConverter.convertUserToEcompUser(asdcUser); + assertThat(result.isLeft()).isTrue(); } @Test @@ -43,5 +46,6 @@ public class EcompUserConverterTest { // test 1 result = EcompUserConverter.convertEcompUserToUser(ecompUser); + assertThat(result).isInstanceOf(User.class); } } -- cgit 1.2.3-korg