aboutsummaryrefslogtreecommitdiffstats
path: root/common-app-api/src
diff options
context:
space:
mode:
authorxuegao <xue.gao@intl.att.com>2021-03-22 11:22:47 +0100
committerxuegao <xue.gao@intl.att.com>2021-03-24 08:25:00 +0100
commit134ca5667da901a703ecb0c78d96873712a9d0d3 (patch)
tree38738cddb2798a54d1f81c22d005b9ff1e0c79f0 /common-app-api/src
parenteb57d1efc197968a4073b331b2dc8f40d8109847 (diff)
Improve test coverage
Add unit tests to improve test coverage. Issue-ID: SDC-3428 Change-Id: I9e00af2824366ae45d47d2dcecf322fd3e9a6fea Signed-off-by: xuegao <xue.gao@intl.att.com>
Diffstat (limited to 'common-app-api/src')
-rw-r--r--common-app-api/src/main/java/org/openecomp/sdc/common/http/config/ClientCertificate.java35
-rw-r--r--common-app-api/src/test/java/org/openecomp/sdc/common/util/GeneralUtilityTest.java44
-rw-r--r--common-app-api/src/test/java/org/openecomp/sdc/common/util/ThreadLocalsHolderTest.java53
3 files changed, 84 insertions, 48 deletions
diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/ClientCertificate.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/ClientCertificate.java
index 1f6c070094..c318a3b514 100644
--- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/ClientCertificate.java
+++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/ClientCertificate.java
@@ -21,9 +21,11 @@
package org.openecomp.sdc.common.http.config;
import fj.data.Either;
+import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.onap.sdc.security.SecurityUtil;
+@EqualsAndHashCode
public class ClientCertificate {
private String keyStore;
private String keyStorePassword;
@@ -68,39 +70,6 @@ public class ClientCertificate {
public String getKeyStorePassword() {
return keyStorePassword;
}
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((keyStore == null) ? 0 : keyStore.hashCode());
- result = prime * result + ((keyStorePassword == null) ? 0 : keyStorePassword.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- ClientCertificate other = (ClientCertificate) obj;
- if (keyStore == null) {
- if (other.keyStore != null)
- return false;
- }
- else if (!keyStore.equals(other.keyStore))
- return false;
- if (keyStorePassword == null) {
- if (other.keyStorePassword != null)
- return false;
- }
- else if (!keyStorePassword.equals(other.keyStorePassword))
- return false;
- return true;
- }
@Override
public String toString() {
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<ICategorizedElement> 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<String, Map<String, List<ICategorizedElement>>> 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());
+ }
}