diff options
author | k.kedron <k.kedron@partner.samsung.com> | 2019-04-30 16:37:54 +0200 |
---|---|---|
committer | k.kedron <k.kedron@partner.samsung.com> | 2019-06-04 11:50:03 +0200 |
commit | 456cf4f6af9c22449dd4a39b1d13900fc38481c1 (patch) | |
tree | 089617e97606dc4ecbc8529f0c1d4ee5546bb1d0 /src/test/java | |
parent | edae4aaca1bc65fa39d398e5a386c918ab7dde7a (diff) |
New unit tests and sonar fixes
Assertion arguments should be passed in the correct order in:
- CldsReferencePropertiesItCase
- LoggingUtils
Remove of duplicate code in LoggingUtils.
Typo in README file.
Add LoggingUtilsTest unit test.
Change-Id: Iddc61877659e9d4308479451a7eb4901b3fc5ea8
Issue-ID: CLAMP-355
Signed-off-by: Krystian Kedron <k.kedron@partner.samsung.com>
Diffstat (limited to 'src/test/java')
-rw-r--r-- | src/test/java/org/onap/clamp/clds/it/config/CldsReferencePropertiesItCase.java | 18 | ||||
-rw-r--r-- | src/test/java/org/onap/clamp/clds/util/LoggingUtilsTest.java | 148 |
2 files changed, 157 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); + } +} |