From: Bilal A Date: Thu, 25 Jul 2019 22:10:07 +0000 (-0400) Subject: Policy API more JUnit Tests X-Git-Tag: 2.1.1~5 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;ds=sidebyside;h=466090990a3fa02a431ab23dc5261553028042a1;p=policy%2Fapi.git Policy API more JUnit Tests Issue-ID: POLICY-1771 Signed-off-by: Bilal A Change-Id: I8882d9aa6e1e56e9425224b9fdfba01552b5e993 --- diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestCommonRestController.java b/main/src/test/java/org/onap/policy/api/main/rest/TestCommonRestController.java new file mode 100644 index 00000000..f2999975 --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestCommonRestController.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy API + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.api.main.rest; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.UUID; + +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.ResponseBuilder; + +import org.junit.Test; +/* + * Class to perform unit testing of CommonRestController. + * + * + */ +public class TestCommonRestController { + private CommonRestController crc = new CommonRestController(); + + @Test + public void testAddLoggingHeaders() { + UUID requestId = UUID.randomUUID(); + ResponseBuilder rb = + crc.addLoggingHeaders( + crc.addVersionControlHeaders(Response.status(Response.Status.OK)), requestId); + assertTrue(rb.equals(rb.header("X-ONAP-RequestID", requestId))); + } + + /* + * Tests null response for null object + */ + @Test + public void testToJsonNull() { + assertNull(crc.toJson(null)); + } +} diff --git a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java index 947221a7..3954106a 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/provider/TestPolicyProvider.java @@ -24,18 +24,34 @@ package org.onap.policy.api.main.rest.provider; import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; +import java.util.ArrayList; import java.util.Base64; -import org.junit.AfterClass; -import org.junit.BeforeClass; +import java.util.List; + +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.onap.policy.api.main.parameters.ApiParameterGroup; import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.pdp.concepts.Pdp; +import org.onap.policy.models.pdp.concepts.PdpGroup; +import org.onap.policy.models.pdp.concepts.PdpGroupFilter; +import org.onap.policy.models.pdp.concepts.PdpSubGroup; +import org.onap.policy.models.pdp.enums.PdpHealthStatus; +import org.onap.policy.models.pdp.enums.PdpState; +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.ToscaPolicyIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; /** @@ -62,8 +78,8 @@ public class TestPolicyProvider { * * @throws PfModelException the PfModel parsing exception */ - @BeforeClass - public static void setupParameters() throws PfModelException { + @Before + public void setupParameters() throws PfModelException { standardCoder = new StandardCoder(); providerParams = new PolicyModelsProviderParameters(); @@ -83,8 +99,8 @@ public class TestPolicyProvider { * * @throws PfModelException the PfModel parsing exception */ - @AfterClass - public static void tearDown() throws PfModelException { + @After + public void tearDown() throws PfModelException { policyTypeProvider.close(); policyProvider.close(); @@ -115,12 +131,108 @@ public class TestPolicyProvider { }).hasMessage("policy with ID dummy:null and type dummy:dummy does not exist"); } + @Test public void testFetchDeployedPolicies() { + String policyId = "onap.restart.tca"; + String policyVersion = "1.0.0"; + String policyTypeVersion = "1.0.0"; + String policyTypeId = "onap.policies.monitoring.cdap.tca.hi.lo.app"; + //Basic Exception Throw assertThatThrownBy(() -> { policyProvider.fetchDeployedPolicies("dummy", "dummy", "dummy"); }).hasMessage("could not find policy with ID dummy and type dummy:dummy deployed in any pdp group"); + + try (PolicyModelsProvider databaseProvider = + new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParams)) { + assertEquals(0, databaseProvider.getPdpGroups("name").size()); + assertEquals(0, databaseProvider.getFilteredPdpGroups(PdpGroupFilter.builder().build()).size()); + + assertNotNull(databaseProvider.createPdpGroups(new ArrayList<>())); + assertNotNull(databaseProvider.updatePdpGroups(new ArrayList<>())); + + PdpGroup pdpGroup = new PdpGroup(); + pdpGroup.setName("group"); + pdpGroup.setVersion("1.2.3"); + pdpGroup.setPdpGroupState(PdpState.ACTIVE); + pdpGroup.setPdpSubgroups(new ArrayList<>()); + List groupList = new ArrayList<>(); + groupList.add(pdpGroup); + + PdpSubGroup pdpSubGroup = new PdpSubGroup(); + pdpSubGroup.setPdpType("type"); + pdpSubGroup.setDesiredInstanceCount(123); + pdpSubGroup.setSupportedPolicyTypes(new ArrayList<>()); + pdpSubGroup.getSupportedPolicyTypes().add(new ToscaPolicyTypeIdentifier( + policyTypeId, policyTypeVersion)); + pdpGroup.getPdpSubgroups().add(pdpSubGroup); + + Pdp pdp = new Pdp(); + pdp.setInstanceId("type-0"); + pdp.setMessage("Hello"); + pdp.setPdpState(PdpState.ACTIVE); + pdp.setHealthy(PdpHealthStatus.UNKNOWN); + pdpSubGroup.setPdpInstances(new ArrayList<>()); + pdpSubGroup.getPdpInstances().add(pdp); + + // Create Pdp Groups + assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0) + .getDesiredInstanceCount()); + assertEquals(1, databaseProvider.getPdpGroups("group").size()); + + // Create Policy Type + assertThatCode(() -> { + String policyTypeString = ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE); + ToscaServiceTemplate policyTypeServiceTemplate = + standardCoder.decode(policyTypeString, ToscaServiceTemplate.class); + policyTypeProvider.createPolicyType(policyTypeServiceTemplate); + }).doesNotThrowAnyException(); + + // Create Policy + assertThatCode(() -> { + String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + ToscaServiceTemplate policyServiceTemplate = + standardCoder.decode(policyString, ToscaServiceTemplate.class); + ToscaServiceTemplate serviceTemplate = policyProvider + .createPolicy(policyTypeId, policyTypeVersion, policyServiceTemplate); + assertFalse(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).isEmpty()); + }).doesNotThrowAnyException(); + + // Test fetchDeployedPolicies (deployedPolicyMap.isEmpty())==true + assertThatThrownBy( + () -> { + policyProvider.fetchDeployedPolicies( + policyTypeId, policyTypeVersion, policyId); + }).hasMessage("could not find policy with ID " + policyId + " and type " + + policyTypeId + ":" + policyTypeVersion + " deployed in any pdp group"); + + + // Update pdpSubGroup + pdpSubGroup.setPolicies(new ArrayList<>()); + pdpSubGroup.getPolicies().add(new ToscaPolicyIdentifier(policyId, policyVersion)); + assertEquals(1, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0) + .getPolicies().size()); + + // Test fetchDeployedPolicies + assertThatCode( + () -> { + policyProvider.fetchDeployedPolicies( + policyTypeId, policyTypeVersion, policyId); + }).doesNotThrowAnyException(); + + // Test validateDeleteEligibility exception path(!pdpGroups.isEmpty()) + assertThatThrownBy( + () -> { + policyProvider.deletePolicy( + "onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", + "onap.restart.tca", "1.0.0"); + }).hasMessageContaining("policy with ID " + policyId + ":" + policyVersion + + " cannot be deleted as it is deployed in pdp groups"); + } + catch (Exception exc) { + fail("Test should not throw an exception"); + } } @Test @@ -199,4 +311,5 @@ public class TestPolicyProvider { "onap.restart.tca", "1.0.0"); }).hasMessage(exceptionMessage); } + } diff --git a/main/src/test/java/org/onap/policy/api/main/startstop/TestApiCommandLineArguments.java b/main/src/test/java/org/onap/policy/api/main/startstop/TestApiCommandLineArguments.java new file mode 100644 index 00000000..73503694 --- /dev/null +++ b/main/src/test/java/org/onap/policy/api/main/startstop/TestApiCommandLineArguments.java @@ -0,0 +1,106 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy API + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.api.main.startstop; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.attribute.FileAttribute; +import java.nio.file.attribute.PosixFilePermission; +import java.nio.file.attribute.PosixFilePermissions; +import java.util.Set; + +import org.junit.Test; +import org.onap.policy.api.main.exception.PolicyApiException; +import org.onap.policy.api.main.exception.PolicyApiRuntimeException; + +public class TestApiCommandLineArguments { + private ApiCommandLineArguments apiCmdArgs = new ApiCommandLineArguments(); + + @Test(expected = PolicyApiRuntimeException.class) + public void testApiCommandLineArgumentsStringArray() { + String [] args = {"---d"}; + ApiCommandLineArguments apiCmdArgs1 = new ApiCommandLineArguments(args); + } + + @Test + public void testNonExistentFileValidateReadableFile() { + apiCmdArgs.setConfigurationFilePath("src/test/resources/filetest/nonexist.json "); + assertThatThrownBy( + apiCmdArgs::validate + ) + .isInstanceOf(PolicyApiException.class) + .hasMessageContaining("file \"src/test/resources/filetest/nonexist.json \" does not exist"); + } + + @Test + public void testEmptyFileNameValidateReadableFile() { + apiCmdArgs.setConfigurationFilePath(""); + assertThatThrownBy( + apiCmdArgs::validate + ) + .isInstanceOf(PolicyApiException.class) + .hasMessageContaining("policy api configuration file was not specified as an argument"); + } + + @Test + public void testInvalidUrlValidateReadableFile() { + apiCmdArgs.setConfigurationFilePath("src/test\\resources/filetest\\n"); + assertThatThrownBy( + apiCmdArgs::validate + ) + .isInstanceOf(PolicyApiException.class) + .hasMessageContaining( + "policy api configuration file \"src/test\\resources/filetest\\n\" does not exist"); + } + + @Test + public void testDirectoryValidateReadableFile() { + apiCmdArgs.setConfigurationFilePath("src/test/resources/"); + assertThatThrownBy( + apiCmdArgs::validate + ) + .isInstanceOf(PolicyApiException.class) + .hasMessageContaining("file \"src/test/resources/\" is not a normal file"); + } + + @Test + public void testReadPermissionValidateReadableFile() throws IOException { + String filepath = "src/test/resources/unreadablefile.json"; + Set notReadable = PosixFilePermissions.fromString("-wx-wx-wx"); + FileAttribute permissions = PosixFilePermissions.asFileAttribute(notReadable); + Path pathObj = Paths.get(filepath); + Files.createFile(pathObj, permissions); + apiCmdArgs.setConfigurationFilePath(filepath); + assertThatThrownBy( + apiCmdArgs::validate + ) + .isInstanceOf(PolicyApiException.class) + .hasMessageContaining( + "file \"src/test/resources/unreadablefile.json\" is ureadable"); + Files.deleteIfExists(pathObj); + } +}