diff options
author | Xue Gao <xg353y@intl.att.com> | 2019-06-18 12:10:14 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-06-18 12:10:14 +0000 |
commit | eb216ac66f47c2b9d2b69a6e080d3969f75d78af (patch) | |
tree | 080b7839e8f755e6542d719bcdb7f6568bfa6a49 /src/test | |
parent | 60b5b25fa4687f8d64f1c5c48515db735a0e5141 (diff) | |
parent | 456cf4f6af9c22449dd4a39b1d13900fc38481c1 (diff) |
Merge "New unit tests and sonar fixes"
Diffstat (limited to 'src/test')
3 files changed, 159 insertions, 9 deletions
diff --git a/src/test/java/org/onap/clamp/clds/it/config/CldsReferencePropertiesItCase.java b/src/test/java/org/onap/clamp/clds/it/config/CldsReferencePropertiesItCase.java index 65aa9b44f..0a05d65dc 100644 --- a/src/test/java/org/onap/clamp/clds/it/config/CldsReferencePropertiesItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/config/CldsReferencePropertiesItCase.java @@ -55,10 +55,10 @@ public class CldsReferencePropertiesItCase { */ @Test public void testGetStringValue() { - assertEquals(refProp.getStringValue("policy.onap.name"), "DCAE"); - assertEquals(refProp.getStringValue("policy.ms.policyNamePrefix", ""), "Config_MS_"); - assertEquals(refProp.getStringValue("policy.ms.policyNamePrefix", "testos"), "Config_MS_"); - assertEquals(refProp.getStringValue("policy.ms", "policyNamePrefix"), "Config_MS_"); + assertEquals("DCAE", refProp.getStringValue("policy.onap.name")); + assertEquals("Config_MS_", refProp.getStringValue("policy.ms.policyNamePrefix", "")); + assertEquals("Config_MS_", refProp.getStringValue("policy.ms.policyNamePrefix", "testos")); + assertEquals("Config_MS_", refProp.getStringValue("policy.ms", "policyNamePrefix")); assertNull(refProp.getStringValue("does.not.exist")); } @@ -70,7 +70,7 @@ public class CldsReferencePropertiesItCase { //then assertNotNull(root); assertTrue(root.isJsonObject()); - assertEquals(root.getAsJsonObject().get("DC1").getAsString(), "Data Center 1"); + assertEquals("Data Center 1", root.getAsJsonObject().get("DC1").getAsString()); } @Test @@ -81,7 +81,7 @@ public class CldsReferencePropertiesItCase { //then assertNotNull(root); assertTrue(root.isJsonObject()); - assertEquals(root.getAsJsonObject().get("DC1").getAsString(), "Data Center 1"); + assertEquals("Data Center 1", root.getAsJsonObject().get("DC1").getAsString()); } @Test @@ -112,9 +112,9 @@ public class CldsReferencePropertiesItCase { @Test public void testGetStringList() { List<String> profileList = refProp.getStringList("policy.pdpUrl1", ","); - assertTrue(profileList.size() == 3); + assertEquals(3, profileList.size()); assertTrue(profileList.get(0).trim().startsWith("http://localhost:")); - assertTrue(profileList.get(1).trim().equals("testpdp")); - assertTrue(profileList.get(2).trim().equals("alpha123")); + assertEquals("testpdp", profileList.get(1).trim()); + assertEquals("alpha123", profileList.get(2).trim()); } } diff --git a/src/test/java/org/onap/clamp/clds/util/LoggingUtilsTest.java b/src/test/java/org/onap/clamp/clds/util/LoggingUtilsTest.java new file mode 100644 index 000000000..7e2da4d1e --- /dev/null +++ b/src/test/java/org/onap/clamp/clds/util/LoggingUtilsTest.java @@ -0,0 +1,148 @@ +/*- +* ============LICENSE_START======================================================= +* ONAP CLAMP +* Copyright (C) 2019 Samsung. 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.onap.clamp.clds.util; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Arrays; +import java.util.Map; + +import javax.net.ssl.HttpsURLConnection; +import javax.servlet.http.HttpServletRequest; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.slf4j.MDC; +import org.slf4j.event.Level; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; + +/** + * Test Logging Utils. + */ +@RunWith(MockitoJUnitRunner.class) +public class LoggingUtilsTest { + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(LoggingUtilsTest.class); + + private static final String SERVICE_NAME = "LogginUtilsTest: Test Entering method"; + + private LoggingUtils util; + + @Before + public void setup() { + this.util = new LoggingUtils(logger); + } + + @Test + public void testEnteringLoggingUtils() { + // given + final String userName = "test"; + + HttpServletRequest request = Mockito.mock(HttpServletRequest.class); + + UserDetails userDetails = Mockito.mock(UserDetails.class); + Mockito.when(userDetails.getUsername()).thenReturn(userName); + + Authentication localAuth = Mockito.mock(Authentication.class); + Mockito.when(localAuth.getPrincipal()).thenReturn(userDetails); + + SecurityContext securityContext = Mockito.mock(SecurityContext.class); + Mockito.when(securityContext.getAuthentication()).thenReturn(localAuth); + SecurityContextHolder.setContext(securityContext); + + // when + util.entering(request, SERVICE_NAME); + + // then + String[] keys = { ONAPLogConstants.MDCs.PARTNER_NAME, + ONAPLogConstants.MDCs.ENTRY_TIMESTAMP, + ONAPLogConstants.MDCs.REQUEST_ID, + ONAPLogConstants.MDCs.INVOCATION_ID, + ONAPLogConstants.MDCs.CLIENT_IP_ADDRESS, + ONAPLogConstants.MDCs.SERVER_FQDN, + ONAPLogConstants.MDCs.INSTANCE_UUID, + ONAPLogConstants.MDCs.SERVICE_NAME + }; + Map<String, String> mdc = MDC.getMDCAdapter().getCopyOfContextMap(); + + assertTrue(checkMapKeys(mdc, keys)); + assertEquals(userName, + mdc.get(ONAPLogConstants.MDCs.PARTNER_NAME)); + } + + @Test + public void testExistingLoggingUtils() { + // given + MDC.put(ONAPLogConstants.MDCs.ENTRY_TIMESTAMP, + ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT)); + + // when + util.exiting("200", SERVICE_NAME, Level.INFO, ONAPLogConstants.ResponseStatus.COMPLETED); + + // then + Map<String, String> mdc = MDC.getMDCAdapter().getCopyOfContextMap(); + assertNull(mdc); + } + + @Test + public void testInvokeTestUtils() { + // given + final String targetEntity = "LoggingUtilsTest"; + final String targetServiceName = "testInvokeTestUtils"; + HttpsURLConnection secureConnection = Mockito.mock(HttpsURLConnection.class); + + // when + secureConnection = util.invokeHttps(secureConnection, targetEntity, targetServiceName); + + // then + assertNotNull(secureConnection); + String[] keys = {ONAPLogConstants.MDCs.TARGET_ENTITY, + ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, + ONAPLogConstants.MDCs.INVOCATIONID_OUT, + ONAPLogConstants.MDCs.INVOKE_TIMESTAMP + }; + Map<String, String> mdc = MDC.getMDCAdapter().getCopyOfContextMap(); + + assertTrue(checkMapKeys(mdc, keys)); + assertEquals(targetEntity, mdc.get(ONAPLogConstants.MDCs.TARGET_ENTITY)); + assertEquals(targetServiceName, mdc.get(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME)); + } + + private boolean checkMapKeys(Map map, String[] keys) { + return Arrays.stream(keys).allMatch(key -> map.get(key) != null); + } +} diff --git a/src/test/resources/logback.xml b/src/test/resources/logback.xml new file mode 100644 index 000000000..07e587855 --- /dev/null +++ b/src/test/resources/logback.xml @@ -0,0 +1,2 @@ +<!-- Empty Configuration to prevent creating log files by Units Tests (e.g LoggingUtilsTest) ! --> +<configuration /> |