diff options
author | waynedunican <wayne.dunican@est.tech> | 2024-08-20 08:27:23 +0100 |
---|---|---|
committer | waynedunican <wayne.dunican@est.tech> | 2024-09-03 16:03:15 +0100 |
commit | c780793359e31dac7d99731360cf1e575485f638 (patch) | |
tree | 897f61ba970728a80a75c35cac23033f68f941b4 /utils | |
parent | 460b684f934776e9c4eefab00d1febb398e87bb1 (diff) |
Increase coverage in common
Issue-ID: POLICY-5106
Change-Id: I54dfcc2ab1100e959ee786ce7a9791ef3465686f
Signed-off-by: waynedunican <wayne.dunican@est.tech>
Diffstat (limited to 'utils')
6 files changed, 296 insertions, 1 deletions
diff --git a/utils/src/test/java/org/onap/policy/common/utils/logging/LoggerMarkerFilterTest.java b/utils/src/test/java/org/onap/policy/common/utils/logging/LoggerMarkerFilterTest.java new file mode 100644 index 00000000..64278a93 --- /dev/null +++ b/utils/src/test/java/org/onap/policy/common/utils/logging/LoggerMarkerFilterTest.java @@ -0,0 +1,155 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation + * ================================================================================ + * 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.policy.common.utils.logging; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.spi.FilterReply; +import java.util.Collections; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onap.policy.common.utils.logging.LoggerMarkerFilter.AuditLoggerMarkerFilter; +import org.onap.policy.common.utils.logging.LoggerMarkerFilter.MetricLoggerMarkerFilter; +import org.onap.policy.common.utils.logging.LoggerMarkerFilter.SecurityLoggerMarkerFilter; +import org.onap.policy.common.utils.logging.LoggerMarkerFilter.TransactionLoggerMarkerFilter; +import org.slf4j.Marker; + +class LoggerMarkerFilterTest { + + private ILoggingEvent mockEvent; + private Marker mockMarker; + + @BeforeEach + void setUp() { + mockEvent = mock(ILoggingEvent.class); + mockMarker = mock(Marker.class); + } + + @Test + void testDecideAcceptWithMetricMarker() { + MetricLoggerMarkerFilter filter = new MetricLoggerMarkerFilter(); + when(mockEvent.getMarkerList()).thenReturn(Collections.singletonList(LoggerUtils.METRIC_LOG_MARKER)); + filter.start(); + + FilterReply reply = filter.decide(mockEvent); + assertEquals(FilterReply.ACCEPT, reply, "The filter should accept the event with the METRIC_LOG_MARKER."); + } + + @Test + void testDecideDenyWithoutMetricMarker() { + MetricLoggerMarkerFilter filter = new MetricLoggerMarkerFilter(); + when(mockEvent.getMarkerList()).thenReturn(Collections.singletonList(mockMarker)); + filter.start(); + + FilterReply reply = filter.decide(mockEvent); + assertEquals(FilterReply.DENY, reply, "The filter should deny the event without the METRIC_LOG_MARKER."); + } + + @Test + void testDecideAcceptWithSecurityMarker() { + SecurityLoggerMarkerFilter filter = new SecurityLoggerMarkerFilter(); + when(mockEvent.getMarkerList()).thenReturn(Collections.singletonList(LoggerUtils.SECURITY_LOG_MARKER)); + filter.start(); + + FilterReply reply = filter.decide(mockEvent); + assertEquals(FilterReply.ACCEPT, reply, "The filter should accept the event with the SECURITY_LOG_MARKER."); + } + + @Test + void testDecideDenyWithoutSecurityMarker() { + SecurityLoggerMarkerFilter filter = new SecurityLoggerMarkerFilter(); + when(mockEvent.getMarkerList()).thenReturn(Collections.singletonList(mockMarker)); + filter.start(); + + FilterReply reply = filter.decide(mockEvent); + assertEquals(FilterReply.DENY, reply, "The filter should deny the event without the SECURITY_LOG_MARKER."); + } + + @Test + void testDecideAcceptWithAuditMarker() { + AuditLoggerMarkerFilter filter = new AuditLoggerMarkerFilter(); + when(mockEvent.getMarkerList()).thenReturn(Collections.singletonList(LoggerUtils.AUDIT_LOG_MARKER)); + filter.start(); + + FilterReply reply = filter.decide(mockEvent); + assertEquals(FilterReply.ACCEPT, reply, "The filter should accept the event with the AUDIT_LOG_MARKER."); + } + + @Test + void testDecideDenyWithoutAuditMarker() { + AuditLoggerMarkerFilter filter = new AuditLoggerMarkerFilter(); + when(mockEvent.getMarkerList()).thenReturn(Collections.singletonList(mockMarker)); + filter.start(); + + FilterReply reply = filter.decide(mockEvent); + assertEquals(FilterReply.DENY, reply, "The filter should deny the event without the AUDIT_LOG_MARKER."); + } + + @Test + void testDecideAcceptWithTransactionMarker() { + TransactionLoggerMarkerFilter filter = new TransactionLoggerMarkerFilter(); + when(mockEvent.getMarkerList()).thenReturn(Collections.singletonList(LoggerUtils.TRANSACTION_LOG_MARKER)); + filter.start(); + + FilterReply reply = filter.decide(mockEvent); + assertEquals(FilterReply.ACCEPT, reply, "The filter should accept the event with the TRANSACTION_LOG_MARKER."); + } + + @Test + void testDecideDenyWithoutTransactionMarker() { + TransactionLoggerMarkerFilter filter = new TransactionLoggerMarkerFilter(); + when(mockEvent.getMarkerList()).thenReturn(Collections.singletonList(mockMarker)); + filter.start(); + + FilterReply reply = filter.decide(mockEvent); + assertEquals(FilterReply.DENY, reply, "The filter should deny the event without the TRANSACTION_LOG_MARKER."); + } + + @Test + void testDecideDenyWhenNotStarted() { + MetricLoggerMarkerFilter filter = new MetricLoggerMarkerFilter(); + when(mockEvent.getMarkerList()).thenReturn(Collections.singletonList(LoggerUtils.METRIC_LOG_MARKER)); + // Filter is not started + + FilterReply reply = filter.decide(mockEvent); + assertEquals(FilterReply.DENY, reply, "The filter should deny the event if the filter is not started."); + } + + @Test + void testDecideDenyWithNullEvent() { + MetricLoggerMarkerFilter filter = new MetricLoggerMarkerFilter(); + filter.start(); + + FilterReply reply = filter.decide(null); + assertEquals(FilterReply.DENY, reply, "The filter should deny if the event is null."); + } + + @Test + void testDecideDenyWithNullMarkerList() { + MetricLoggerMarkerFilter filter = new MetricLoggerMarkerFilter(); + when(mockEvent.getMarkerList()).thenReturn(null); + filter.start(); + + FilterReply reply = filter.decide(mockEvent); + assertEquals(FilterReply.DENY, reply, "The filter should deny if the marker list is null."); + } +} diff --git a/utils/src/test/java/org/onap/policy/common/utils/properties/PropertyObjectUtilsTest.java b/utils/src/test/java/org/onap/policy/common/utils/properties/PropertyObjectUtilsTest.java index b62fd1e4..06b4f74f 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/properties/PropertyObjectUtilsTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/properties/PropertyObjectUtilsTest.java @@ -21,7 +21,6 @@ package org.onap.policy.common.utils.properties; import static org.junit.jupiter.api.Assertions.assertEquals; -import java.io.IOException; import java.util.AbstractSet; import java.util.Arrays; import java.util.Iterator; diff --git a/utils/src/test/java/org/onap/policy/common/utils/resources/PrometheusUtilsTest.java b/utils/src/test/java/org/onap/policy/common/utils/resources/PrometheusUtilsTest.java new file mode 100644 index 00000000..a60b599e --- /dev/null +++ b/utils/src/test/java/org/onap/policy/common/utils/resources/PrometheusUtilsTest.java @@ -0,0 +1,37 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation. + * ================================================================================ + * 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.policy.common.utils.resources; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class PrometheusUtilsTest { + + @Test + void test() { + PrometheusUtils.PdpType pdpa = PrometheusUtils.PdpType.PDPA; + PrometheusUtils.PdpType pdpd = PrometheusUtils.PdpType.PDPD; + PrometheusUtils.PdpType pdpx = PrometheusUtils.PdpType.PDPX; + + assertEquals("pdpa", pdpa.getNamespace(), "pdpa constructor"); + assertEquals("pdpd", pdpd.getNamespace(), "pdpd constructor"); + assertEquals("pdpx", pdpx.getNamespace(), "pdpx constructor"); + } +} diff --git a/utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java b/utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java index 5f8b80ea..5eec8a74 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java @@ -21,10 +21,14 @@ package org.onap.policy.common.utils.security; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import javax.crypto.spec.SecretKeySpec; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,6 +46,12 @@ class CryptoUtilsTest { private static final String ENCRYPTED_MSG = "original value : {} encrypted value: {}"; @Test + void testConstructor() { + SecretKeySpec skspecMock = mock(SecretKeySpec.class); + assertThatCode(() -> new CryptoUtils(skspecMock)).doesNotThrowAnyException(); + } + + @Test void testEncrypt() { logger.info("testEncrypt:"); CryptoCoder cryptoUtils = new CryptoUtils(SECRET_KEY); @@ -49,6 +59,8 @@ class CryptoUtilsTest { logger.info(ENCRYPTED_MSG, PASS, encryptedValue); assertTrue(encryptedValue.startsWith("enc:")); + assertTrue(CryptoUtils.isEncrypted(encryptedValue)); + String decryptedValue = cryptoUtils.decrypt(encryptedValue); logger.info(DECRYPTED_MSG, encryptedValue, decryptedValue); assertEquals(PASS, decryptedValue); @@ -61,6 +73,7 @@ class CryptoUtilsTest { String decryptedValue = cryptoUtils.decrypt(ENCRYPTED_PASS); logger.info(DECRYPTED_MSG, ENCRYPTED_PASS, decryptedValue); assertEquals(PASS, decryptedValue); + assertFalse(CryptoUtils.isEncrypted(decryptedValue)); } @Test @@ -120,4 +133,20 @@ class CryptoUtilsTest { String decryptedAgain = CryptoUtils.decrypt(decryptedValue, SECRET_KEY); assertEquals(decryptedValue, decryptedAgain); } + + @Test + void testMain() { + SecretKeySpec skspecMock = mock(SecretKeySpec.class); + new CryptoUtils(skspecMock); + + String[] stringsForTesting = new String[1]; + stringsForTesting[0] = "dec"; + assertThatCode(() -> CryptoUtils.main(stringsForTesting)).doesNotThrowAnyException(); + + stringsForTesting[0] = "enc"; + assertThatCode(() -> CryptoUtils.main(stringsForTesting)).doesNotThrowAnyException(); + + stringsForTesting[0] = "abc"; + assertThatCode(() -> CryptoUtils.main(stringsForTesting)).doesNotThrowAnyException(); + } } diff --git a/utils/src/test/java/org/onap/policy/common/utils/services/OrderedServiceTest.java b/utils/src/test/java/org/onap/policy/common/utils/services/OrderedServiceTest.java new file mode 100644 index 00000000..adac7cb0 --- /dev/null +++ b/utils/src/test/java/org/onap/policy/common/utils/services/OrderedServiceTest.java @@ -0,0 +1,62 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation. + * ================================================================================ + * 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.policy.common.utils.services; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class OrderedServiceTest { + + @Test + void testGetSequenceNumber() { + // Anonymous class implementation for testing + OrderedService service = () -> 5; // Returns 5 as the sequence number + + // Test getSequenceNumber + assertEquals(5, service.getSequenceNumber(), "The sequence number should be 5"); + } + + @Test + void testGetName() { + // Anonymous class implementation for testing + OrderedService service = () -> 5; + + // Test getName + assertEquals(service.getClass().getName(), service.getName(), "The name should match the class name"); + } + + @Test + void testGetNameWithCustomImplementation() { + // Custom implementation of OrderedService + class CustomOrderedService implements OrderedService { + @Override + public int getSequenceNumber() { + return 10; + } + } + + OrderedService service = new CustomOrderedService(); + + // Test getName for custom implementation + assertEquals(service.getClass().getName(), service.getName(), + "The name should match the custom implementation class name"); + } +} + diff --git a/utils/src/test/java/org/onap/policy/common/utils/validation/VersionTest.java b/utils/src/test/java/org/onap/policy/common/utils/validation/VersionTest.java index d7dc44fb..19023124 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/validation/VersionTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/validation/VersionTest.java @@ -52,6 +52,19 @@ class VersionTest { } @Test + void testConstructor() { + Version versionTest = new Version("1.0.2"); + assertEquals(1, versionTest.getMajor()); + assertEquals(0, versionTest.getMinor()); + assertEquals(2, versionTest.getPatch()); + + versionTest = new Version("null"); + assertEquals(0, versionTest.getMajor()); + assertEquals(0, versionTest.getMinor()); + assertEquals(0, versionTest.getPatch()); + } + + @Test void testMakeVersion() { assertEquals("9.8.7", Version.makeVersion(TYPE, NAME, "9.8.7").toString()); assertEquals("9.0.0", Version.makeVersion(TYPE, NAME, "9").toString()); |