From 134ca5667da901a703ecb0c78d96873712a9d0d3 Mon Sep 17 00:00:00 2001 From: xuegao Date: Mon, 22 Mar 2021 11:22:47 +0100 Subject: Improve test coverage Add unit tests to improve test coverage. Issue-ID: SDC-3428 Change-Id: I9e00af2824366ae45d47d2dcecf322fd3e9a6fea Signed-off-by: xuegao --- .../sdc/common/util/GeneralUtilityTest.java | 44 +++++++++++++----- .../sdc/common/util/ThreadLocalsHolderTest.java | 53 ++++++++++++++++++++-- 2 files changed, 82 insertions(+), 15 deletions(-) (limited to 'common-app-api/src/test') diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/util/GeneralUtilityTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/util/GeneralUtilityTest.java index b5036060eb..837be1d0ca 100644 --- a/common-app-api/src/test/java/org/openecomp/sdc/common/util/GeneralUtilityTest.java +++ b/common-app-api/src/test/java/org/openecomp/sdc/common/util/GeneralUtilityTest.java @@ -23,17 +23,22 @@ package org.openecomp.sdc.common.util; import com.google.common.collect.Lists; import org.apache.commons.io.FileUtils; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import java.io.File; import java.io.IOException; import java.util.Base64; +import java.util.LinkedList; import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; public class GeneralUtilityTest { @@ -56,14 +61,12 @@ public class GeneralUtilityTest { @Test public void validateIsBase64EncodedReturnsProperResponseFromByteArray() { - final String testString = "testDataToEncode"; final byte[] testBytes = testString.getBytes(); final byte[] testEncodedBytes = Base64.getEncoder().encode(testBytes); - boolean result = GeneralUtility.isBase64Encoded(testEncodedBytes); - - assertTrue(result); + assertTrue(GeneralUtility.isBase64Encoded(testEncodedBytes)); + assertTrue(GeneralUtility.isBase64Encoded(testString)); } @Test @@ -191,7 +194,7 @@ public class GeneralUtilityTest { final String encodedString = org.apache.commons.codec.digest.DigestUtils.md5Hex(testStringToEncode.getBytes()); - assertArrayEquals(encodedString.getBytes(), Base64.getDecoder().decode(result)); + assertEquals(encodedString, new String(Base64.getDecoder().decode(result))); } @Test @@ -204,7 +207,7 @@ public class GeneralUtilityTest { final String encodedString = org.apache.commons.codec.digest.DigestUtils.md5Hex(testStringToEncode.getBytes()); - assertArrayEquals(encodedString.getBytes(), Base64.getDecoder().decode(result)); + assertEquals(encodedString, new String(Base64.getDecoder().decode(result))); } @Test @@ -243,8 +246,25 @@ public class GeneralUtilityTest { final String empty = " \ttest "; boolean result = GeneralUtility.isEmptyString(empty); - + assertTrue(GeneralUtility.isEmptyString(null)); assertFalse(result); } + @Test + public void getCategorizedComponentsTest() { + + List components = new LinkedList<>(); + ICategorizedElement componentService = Mockito.mock(ICategorizedElement.class); + when(componentService.getComponentTypeAsString()).thenReturn("SERVICE"); + + ICategorizedElement componentResource = Mockito.mock(ICategorizedElement.class); + when(componentResource.getComponentTypeAsString()).thenReturn("RESOURCE"); + + components.add(componentService); + components.add(componentResource); + Map>> result = GeneralUtility.getCategorizedComponents(components); + verify(componentService, Mockito.times(1)).getComponentTypeAsString(); + verify(componentResource, Mockito.times(2)).getComponentTypeAsString(); + assertEquals(2, result.size()); + } } diff --git a/common-app-api/src/test/java/org/openecomp/sdc/common/util/ThreadLocalsHolderTest.java b/common-app-api/src/test/java/org/openecomp/sdc/common/util/ThreadLocalsHolderTest.java index c8b2920f81..223b027c96 100644 --- a/common-app-api/src/test/java/org/openecomp/sdc/common/util/ThreadLocalsHolderTest.java +++ b/common-app-api/src/test/java/org/openecomp/sdc/common/util/ThreadLocalsHolderTest.java @@ -20,10 +20,16 @@ package org.openecomp.sdc.common.util; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.openecomp.sdc.common.api.FilterDecisionEnum; +import org.openecomp.sdc.common.datastructure.UserContext; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import java.util.HashSet; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNull; public class ThreadLocalsHolderTest { @@ -76,4 +82,45 @@ public class ThreadLocalsHolderTest { assertNull(ThreadLocalsHolder.getUuid()); assertEquals(ThreadLocalsHolder.isMdcProcessed(), false); } + + @Test + public void validateUserContext() { + UserContext userContext = new UserContext("userId", new HashSet<>(), "firstName", "lastName"); + + ThreadLocalsHolder.setUserContext(userContext); + assertEquals("userId", ThreadLocalsHolder.getUserContext().getUserId()); + assertEquals("firstName", ThreadLocalsHolder.getUserContext().getFirstName()); + assertEquals("lastName", ThreadLocalsHolder.getUserContext().getLastName()); + assertEquals(0, ThreadLocalsHolder.getUserContext().getUserRoles().size()); + } + + @Test + public void validateApiType() { + FilterDecisionEnum filterDecision = FilterDecisionEnum.INTERNAL; + + ThreadLocalsHolder.setApiType(filterDecision); + assertEquals(filterDecision, ThreadLocalsHolder.getApiType()); + } + + @Test + public void isInternalRequestTest() { + FilterDecisionEnum filterDecision = FilterDecisionEnum.INTERNAL; + ThreadLocalsHolder.setApiType(filterDecision); + assertTrue(ThreadLocalsHolder.isInternalRequest()); + + filterDecision = FilterDecisionEnum.EXTERNAL; + ThreadLocalsHolder.setApiType(filterDecision); + assertFalse(ThreadLocalsHolder.isInternalRequest()); + } + + @Test + public void isExternalRequestTest() { + FilterDecisionEnum filterDecision = FilterDecisionEnum.INTERNAL; + ThreadLocalsHolder.setApiType(filterDecision); + assertFalse(ThreadLocalsHolder.isExternalRequest()); + + filterDecision = FilterDecisionEnum.EXTERNAL; + ThreadLocalsHolder.setApiType(filterDecision); + assertTrue(ThreadLocalsHolder.isExternalRequest()); + } } -- cgit 1.2.3-korg