From 478ac7c72dba1c26e7a79a08d50b6fd14441bf2a Mon Sep 17 00:00:00 2001 From: "puthuparambil.aditya" Date: Fri, 17 Jul 2020 11:13:49 +0100 Subject: Fetch and Delete policy API with PolicyName and PolicyVersion Issue-ID: POLICY-2585 Signed-off-by: puthuparambil.aditya Change-Id: Icabde6463105ed382c1e32b6f7c1319490a385b2 --- .../policy/api/main/rest/TestApiRestServer.java | 92 +++++++++++++++++ .../api/main/rest/TestApiStatisticsManager.java | 19 +++- .../api/main/rest/provider/TestPolicyProvider.java | 110 +++++++++++++++++++++ 3 files changed, 220 insertions(+), 1 deletion(-) (limited to 'main/src/test/java/org/onap') diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java index 2f794fd7..01892be1 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestApiRestServer.java @@ -3,6 +3,7 @@ * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved. * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2020 Bell Canada. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,6 +48,7 @@ import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; import org.glassfish.jersey.client.ClientProperties; import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; import org.junit.AfterClass; @@ -197,6 +199,8 @@ public class TestApiRestServer { "policies/vCPE.policy.operational.legacy.input.json", "policies/vDNS.policy.operational.legacy.input.json", "policies/vFirewall.policy.operational.legacy.input.json"}; + private static final String POLICIES_VCPE_VERSION1 = "policies/onap.restart.tca/versions/1.0.0"; + private static PolicyModelsProviderParameters providerParams; private static ApiParameterGroup apiParamGroup; private static PolicyProvider policyProvider; @@ -335,6 +339,7 @@ public class TestApiRestServer { .getResourceAsString(TOSCA_POLICY_RESOURCE_NAMES[TOSCA_POLICIES_RESOURCE_NAMES.length - 1]); toscaPolicy = toscaPolicy.replaceAll("onap.policies.monitoring.cdap.tca.hi.lo.app", "IDontExist"); + toscaPolicy = toscaPolicy.replaceAll("onap.restart.tca", "onap.restart.tca.IDontExist"); TextFileUtils.putStringAsTextFile(toscaPolicy, "src/test/resources/policies/BadTestPolicy.yaml"); Response rawResponse2 = createResource(POLICIES, "src/test/resources/policies/BadTestPolicy.yaml"); @@ -836,6 +841,93 @@ public class TestApiRestServer { assertEquals("policy operational.scaleout:1.0.0 not found", errorResponse.getErrorMessage()); } + @Test + public void testGetPoliciesJson() throws Exception { + getPolicies(APP_JSON); + } + + @Test + public void testGetPoliciesYaml() throws Exception { + getPolicies(APP_YAML); + } + + private void getPolicies(String mediaType) throws Exception { + for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) { + Response rawResponse = createResource(POLICYTYPES, resrcName); + assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode()); + ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class); + assertThat(response).isNotNull(); + assertThat(response.getPolicyTypes()).isNotEmpty(); + } + for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) { + Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName); + assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode()); + } + Response rawResponse = readResource(POLICIES, mediaType); + assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode()); + ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class); + assertThat(response.getToscaTopologyTemplate().getPolicies()).isNotEmpty(); + } + + @Test + public void testGetSpecificPolicyJson() throws Exception { + getSpecificPolicy(APP_JSON); + } + + @Test + public void testGetSpecificPolicyYaml() throws Exception { + getSpecificPolicy(APP_YAML); + } + + private void getSpecificPolicy(String mediaType) throws Exception { + for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) { + Response rawResponse = createResource(POLICYTYPES, resrcName); + assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode()); + ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class); + assertThat(response).isNotNull(); + assertThat(response.getPolicyTypes()).isNotEmpty(); + } + for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) { + Response rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName); + assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode()); + } + Response rawResponse = readResource(POLICIES_VCPE_VERSION1, mediaType); + assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode()); + ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class); + assertThat(response.getToscaTopologyTemplate().getPolicies()).hasSize(1); + } + + @Test + public void testDeleteSpecificPolicy() throws Exception { + Response rawResponse; + for (String resrcName : TOSCA_POLICYTYPE_RESOURCE_NAMES) { + rawResponse = createResource(POLICYTYPES, resrcName); + assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode()); + ToscaServiceTemplate response = rawResponse.readEntity(ToscaServiceTemplate.class); + assertThat(response).isNotNull(); + assertThat(response.getPolicyTypes()).isNotEmpty(); + } + for (String resrcName : TOSCA_POLICY_RESOURCE_NAMES) { + rawResponse = createResource(POLICYTYPES_TCA_POLICIES, resrcName); + assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode()); + } + + rawResponse = readResource(POLICIES_VCPE_VERSION1, APP_JSON); + assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode()); + + // delete a particular policy + rawResponse = deleteResource(POLICIES_VCPE_VERSION1, APP_JSON); + assertThat(rawResponse.getStatus()).isEqualTo(Response.Status.OK.getStatusCode()); + + rawResponse = readResource(POLICIES_VCPE_VERSION1, APP_JSON); + assertThat(rawResponse.getStatus()).isEqualTo(Status.NOT_FOUND.getStatusCode()); + + rawResponse = deleteResource(POLICIES_VCPE_VERSION1, APP_JSON); + assertThat(rawResponse.getStatus()).isEqualTo(Status.NOT_FOUND.getStatusCode()); + + } + + private Response createResource(String endpoint, String resourceName) throws Exception { String mediaType = APP_JSON; // default media type diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java b/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java index b72ad23c..148ec1b8 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java @@ -3,6 +3,7 @@ * ONAP Policy API * ================================================================================ * Copyright (C) 2019 IBM. + * Modifications Copyright (C) 2020 Bell Canada. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,10 +37,26 @@ public class TestApiStatisticsManager { @Test public void testUpdateMethods() { + assertEquals(1, ApiStatisticsManager.updateTotalApiCallCount()); + assertEquals(1, ApiStatisticsManager.updateApiCallSuccessCount()); + assertEquals(1, ApiStatisticsManager.updateApiCallFailureCount()); + assertEquals(1, ApiStatisticsManager.updateTotalPolicyGetCount()); + assertEquals(1, ApiStatisticsManager.updateTotalPolicyPostCount()); + assertEquals(1, ApiStatisticsManager.updateTotalPolicyDeleteCount()); + assertEquals(1, ApiStatisticsManager.updateTotalPolicyTypeGetCount()); assertEquals(1, ApiStatisticsManager.updateTotalPolicyTypePostCount()); + assertEquals(1, ApiStatisticsManager.updateTotalPolicyTypeDeleteCount()); assertEquals(1, ApiStatisticsManager.updatePolicyGetSuccessCount()); + assertEquals(1, ApiStatisticsManager.updatePolicyGetFailureCount()); assertEquals(1, ApiStatisticsManager.updatePolicyPostSuccessCount()); + assertEquals(1, ApiStatisticsManager.updatePolicyPostFailureCount()); + assertEquals(1, ApiStatisticsManager.updatePolicyDeleteSuccessCount()); + assertEquals(1, ApiStatisticsManager.updatePolicyDeleteFailureCount()); + assertEquals(1, ApiStatisticsManager.updatePolicyTypeGetSuccessCount()); + assertEquals(1, ApiStatisticsManager.updatePolicyTypeGetFailureCount()); + assertEquals(1, ApiStatisticsManager.updatePolicyTypePostSuccessCount()); assertEquals(1, ApiStatisticsManager.updatePolicyTypePostFailureCount()); + assertEquals(1, ApiStatisticsManager.updatePolicyTypeDeleteSuccessCount()); + assertEquals(1, ApiStatisticsManager.updatePolicyTypeDeleteFailureCount()); } - } 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 23257e34..a86f969a 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 @@ -4,6 +4,7 @@ * ================================================================================ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2020 Bell Canada. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +24,7 @@ package org.onap.policy.api.main.rest.provider; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; @@ -140,6 +142,10 @@ public class TestPolicyProvider { assertThatThrownBy(() -> { policyProvider.fetchPolicies("dummy", "1.0.0", "dummy", "1.0.0", null); }).hasMessage("service template not found in database"); + + assertThatThrownBy(() -> { + policyProvider.fetchPolicies(null, null, "dummy", "1.0.0", null); + }).hasMessage("service template not found in database"); } @Test @@ -413,4 +419,108 @@ public class TestPolicyProvider { "1.0.0"); }).hasMessageContaining("no policies found"); } + + @Test + public void testFetchAllPolicies() throws Exception { + // Create Policy Type + ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder + .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + policyTypeProvider.createPolicyType(policyTypeServiceTemplate); + + // Create Policy + String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + ToscaServiceTemplate policyServiceTemplate = + standardCoder.decode(policyString, ToscaServiceTemplate.class); + ToscaServiceTemplate serviceTemplate = policyProvider.createPolicy( + "onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); + + assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); + + // Test fetch all policies + policyTypeServiceTemplate = policyProvider + .fetchPolicies(null, null, null, null, null); + + assertThat(policyTypeServiceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); + } + + @Test + public void testFetchSpecificPolicy_availablePolicy() throws Exception { + // Create Policy Type + ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder + .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + policyTypeProvider.createPolicyType(policyTypeServiceTemplate); + + // Create Policy + String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + ToscaServiceTemplate policyServiceTemplate = + standardCoder.decode(policyString, ToscaServiceTemplate.class); + ToscaServiceTemplate serviceTemplate = policyProvider.createPolicy( + "onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); + + assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); + + // Test fetch specific policy + assertThat(policyProvider.fetchPolicies(null, null, "onap.restart.tca", + "1.0.0", null).getToscaTopologyTemplate().getPolicies()).hasSize(1); + } + + @Test + public void testFetchSpecificPolicy_unavailablePolicy() throws Exception { + // Create Policy Type + ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder + .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + policyTypeProvider.createPolicyType(policyTypeServiceTemplate); + + // Create Policy + String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + ToscaServiceTemplate policyServiceTemplate = + standardCoder.decode(policyString, ToscaServiceTemplate.class); + ToscaServiceTemplate serviceTemplate = policyProvider.createPolicy( + "onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); + assertNotNull(serviceTemplate.getToscaTopologyTemplate().getPolicies()); + assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); + + // Test fetch specific policy + assertThatThrownBy(() -> policyProvider.fetchPolicies( + null, null, "onap.restart.tca", "2.0.0", null)) + .hasMessageContaining("policies for onap.restart.tca:2.0.0 do not exist"); + } + + @Test + public void testDeleteSpecificPolicy_availablePolicy() throws Exception { + ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder + .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + policyTypeProvider.createPolicyType(policyTypeServiceTemplate); + + String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); + ToscaServiceTemplate serviceTemplate = policyProvider.createPolicy( + "onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); + assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); + + ToscaServiceTemplate svcTemplate = policyProvider + .deletePolicy(null, null, "onap.restart.tca", "1.0.0"); + assertThat(svcTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); + } + + @Test + public void testDeleteSpecificPolicy_unavailablePolicy() throws Exception { + ToscaServiceTemplate policyTypeServiceTemplate = standardYamlCoder + .decode(ResourceUtils.getResourceAsString(POLICY_TYPE_RESOURCE), ToscaServiceTemplate.class); + policyTypeProvider.createPolicyType(policyTypeServiceTemplate); + + String policyString = ResourceUtils.getResourceAsString(POLICY_RESOURCE); + ToscaServiceTemplate policyServiceTemplate = standardCoder.decode(policyString, ToscaServiceTemplate.class); + ToscaServiceTemplate serviceTemplate = policyProvider.createPolicy( + "onap.policies.monitoring.cdap.tca.hi.lo.app", "1.0.0", policyServiceTemplate); + assertThat(serviceTemplate.getToscaTopologyTemplate().getPolicies()).hasSize(1); + + assertThatThrownBy(() -> policyProvider + .deletePolicy(null, null, "onap.restart.tca", "2.0.0")) + .hasMessageContaining("not found"); + + assertThatThrownBy(() -> policyProvider.deletePolicy( + null, null, "onap.restart.tca.unavailable", "1.0.0")) + .hasMessageContaining("not found"); + } } -- cgit 1.2.3-korg