diff options
author | waynedunican <wayne.dunican@est.tech> | 2024-07-23 09:23:51 +0100 |
---|---|---|
committer | waynedunican <wayne.dunican@est.tech> | 2024-08-13 08:49:10 +0100 |
commit | b7804abcf865dc58a01bed3f2be4756e731d9288 (patch) | |
tree | 7f6fc3b50622578bb8612de9fe5e5c6adddfaf28 /models-provider/src/test/java | |
parent | a029ccab07f2dd71286804da620c513da9fdfc0e (diff) |
Improve code coverage and sonar fixes
Increased code coverage to 90%
SONAR - Removed TODO comments
SONAR - Added NOSONAR where appropriate
SONAR - Replaced stream.Collect() with stream.toList() where applicable
SONAR - Made variables serializable or transient to comply with sonar rules
Issue-ID: POLICY-5069
Change-Id: Ife256eaf4e6f427fe40b138bacc6f112dc5bcea4
Signed-off-by: waynedunican <wayne.dunican@est.tech>
Diffstat (limited to 'models-provider/src/test/java')
5 files changed, 216 insertions, 1 deletions
diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/PolicyModelsProviderFactoryTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/PolicyModelsProviderFactoryTest.java index 5fa6e809c..6d7a7075f 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/PolicyModelsProviderFactoryTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/PolicyModelsProviderFactoryTest.java @@ -23,8 +23,13 @@ package org.onap.policy.models.provider; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import jakarta.persistence.EntityManager; +import lombok.Getter; +import lombok.Setter; import lombok.ToString; import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.onap.policy.models.dao.impl.ProxyDao; /** * Test the {@link PolicyModelsProviderFactory} class. @@ -34,6 +39,11 @@ import org.junit.jupiter.api.Test; @ToString class PolicyModelsProviderFactoryTest { + @Getter + @Setter + @Mock + private EntityManager mgr; + @Test void testFactory() { PolicyModelsProviderFactory factory = new PolicyModelsProviderFactory(); @@ -70,5 +80,26 @@ class PolicyModelsProviderFactoryTest { }).hasMessage("could not create an instance of PolicyModelsProvider " + "\"org.onap.policy.models.provider.impl.DummyBadProviderImpl\""); // @formatter:on + + assertThatThrownBy(() -> { + PolicyModelsProviderParameters pars = new PolicyModelsProviderParameters(); + ProxyDao dao = new ProxyDao(mgr); + factory.createPolicyModelsProvider(dao, pars); + }).hasMessageContaining("could not create an instance of PolicyModelsProvider"); + + assertThatThrownBy(() -> { + PolicyModelsProviderParameters pars = new PolicyModelsProviderParameters(); + pars.setImplementation(null); + ProxyDao dao = new ProxyDao(mgr); + factory.createPolicyModelsProvider(dao, pars); + }).hasMessage("could not find implementation of the \"PolicyModelsProvider\" interface \"null\""); + + assertThatThrownBy(() -> { + PolicyModelsProviderParameters pars = new PolicyModelsProviderParameters(); + pars.setImplementation("java.lang.String"); + ProxyDao dao = new ProxyDao(mgr); + factory.createPolicyModelsProvider(dao, pars); + }).hasMessage( + "the class \"java.lang.String\" is not an implementation of the \"PolicyModelsProvider\" interface"); } } diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/AbstractModelsProviderTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/AbstractModelsProviderTest.java new file mode 100644 index 000000000..d6aefdab0 --- /dev/null +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/AbstractModelsProviderTest.java @@ -0,0 +1,73 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Provider Models + * ================================================================================ + * 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.models.provider.impl; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.openMocks; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.provider.PolicyModelsProviderParameters; + +class AbstractModelsProviderTest { + + @Mock + private PolicyModelsProviderParameters mockParameters; + + private AbstractModelsProvider abstractModelsProviderUnderTest; + + private AutoCloseable mockitoCloseable; + + @BeforeEach + void setUp() { + mockitoCloseable = openMocks(this); + abstractModelsProviderUnderTest = new AbstractModelsProvider(mockParameters) {}; + } + + @AfterEach + void tearDown() throws Exception { + mockitoCloseable.close(); + } + + @Test + void testInitError() { + when(mockParameters.getDatabaseUrl()).thenReturn("invalidParameter"); + when(mockParameters.getDatabaseDriver()).thenReturn("invalidParameter"); + when(mockParameters.getDatabaseUser()).thenReturn("invalidParameter"); + when(mockParameters.getDatabasePassword()).thenReturn("invalidParameter"); + when(mockParameters.getPersistenceUnit()).thenReturn("invalidParameter"); + + assertThrows(PfModelException.class, () -> abstractModelsProviderUnderTest.init()); + } + + @Test + void testClose() { + when(mockParameters.getDatabaseUrl()).thenReturn("result"); + when(mockParameters.getPersistenceUnit()).thenReturn("result"); + + assertThatCode(abstractModelsProviderUnderTest::close).doesNotThrowAnyException(); + } +} diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java index 16d4ee4ac..0db2e0d3d 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java @@ -22,6 +22,7 @@ package org.onap.policy.models.provider.impl; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -44,6 +45,7 @@ import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion; import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType; @@ -488,6 +490,53 @@ class DatabasePolicyModelsProviderTest { databaseProvider.deleteToscaNodeTemplate("invalidName", "1.0.1"); }).hasMessage("node template invalidName:1.0.1 not found"); + assertThatCode(() -> databaseProvider.getServiceTemplateList(NAME, VERSION_100)) + .doesNotThrowAnyException(); + + assertThatCode(() -> databaseProvider.getFilteredServiceTemplateList(ToscaEntityFilter + .<ToscaServiceTemplate>builder().build())) + .doesNotThrowAnyException(); + + + } + + @Test + void testToscaTemplateMethods() throws PfModelException { + databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + + assertThatThrownBy(() -> databaseProvider.createServiceTemplate(new ToscaServiceTemplate())) + .hasMessageContaining("\"service template\" INVALID"); + + assertThatThrownBy(() -> databaseProvider.updateServiceTemplate(new ToscaServiceTemplate())) + .hasMessageContaining("\"service template\" INVALID"); + + assertThatThrownBy(() -> databaseProvider.deleteServiceTemplate(NAME, VERSION_100)) + .hasMessageContaining("service template not found in database"); + + assertThatThrownBy(() -> databaseProvider.getToscaNodeTemplate(NAME, VERSION_100)) + .hasMessageContaining("service template not found in database"); + + assertNotNull(databaseProvider.getAllPolicyStatus()); + + assertNotNull(databaseProvider.getAllPolicyStatus(new ToscaConceptIdentifierOptVersion())); + + assertNotNull(databaseProvider.getGroupPolicyStatus("testGroup")); + + ToscaServiceTemplate serviceTemplate = makeNodeTemplate(); + databaseProvider.createToscaNodeTemplates(serviceTemplate); + + assertThatCode(() -> databaseProvider.createServiceTemplate(new ToscaServiceTemplate())) + .doesNotThrowAnyException(); + assertThatCode(() -> databaseProvider.getToscaNodeTemplate(serviceTemplate.getName(), + serviceTemplate.getVersion())).doesNotThrowAnyException(); + + assertThatCode(() -> databaseProvider.updateServiceTemplate(new ToscaServiceTemplate())) + .doesNotThrowAnyException(); + assertThatCode(() -> databaseProvider.deleteServiceTemplate(serviceTemplate.getName(), + serviceTemplate.getVersion())).doesNotThrowAnyException(); + + + databaseProvider.close(); } diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DbPolicyModelsProviderImplTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DbPolicyModelsProviderImplTest.java new file mode 100644 index 000000000..d14ed8f2e --- /dev/null +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DbPolicyModelsProviderImplTest.java @@ -0,0 +1,63 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Provider Models + * ================================================================================ + * 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.models.provider.impl; + +import static org.assertj.core.api.Assertions.assertThatCode; +import static org.mockito.MockitoAnnotations.openMocks; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.onap.policy.models.dao.impl.ProxyDao; + +class DbPolicyModelsProviderImplTest { + + @Mock + private ProxyDao mockPfDao; + + private DbPolicyModelsProviderImpl dbPolicyModelsProviderImplUnderTest; + + private AutoCloseable mockitoCloseable; + + @BeforeEach + void setUp() { + mockitoCloseable = openMocks(this); + dbPolicyModelsProviderImplUnderTest = new DbPolicyModelsProviderImpl(mockPfDao); + } + + @AfterEach + void tearDown() throws Exception { + mockitoCloseable.close(); + } + + @Test + void testInit() { + assertThatCode(() -> dbPolicyModelsProviderImplUnderTest.init()) + .doesNotThrowAnyException(); + } + + @Test + void testClose() { + assertThatCode(() -> dbPolicyModelsProviderImplUnderTest.close()) + .doesNotThrowAnyException(); + } +} diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java index 227f1866f..e9aedad99 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/PolicyToscaPersistenceTest.java @@ -26,7 +26,6 @@ import static org.assertj.core.api.Assertions.assertThatCode; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import java.net.URISyntaxException; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; |