diff options
author | Pamela Dragosh <pdragosh@research.att.com> | 2020-03-10 07:54:05 -0400 |
---|---|---|
committer | Pamela Dragosh <pdragosh@research.att.com> | 2020-03-10 09:37:44 -0400 |
commit | b0a27abb7d8812d5a73f65645df0bdbf06e4d64d (patch) | |
tree | 966f321e7d7259a6816ebeac28680ca1571752b6 /applications/optimization/src/test/java | |
parent | a0ad29cdb6d6f541aac59f9e265f79d3ad085560 (diff) |
Fix optimization bug add coverage plus
Fix a NPE bug and add more code coverage.
Also is missing returning of context details for subscriber policies.
This code is a bit ad-hoc and there is a separate JIRA POLICY-2147 to
support re-factoring this codebase.
Added scanning for advice to be returned. For optimization, because of
the need for some changes in XACML github dependency, we are stuck with
a little narly code to get it to fully work. POLICY-2417 is created to
address this in Guilen.
Upgraded to released XACML artifact - this has been tested locally for
a few weeks with naming, guard and this optimzation code. It removed
Jackson in lieu of Json, cleaned up some security fixes, upgraded
dependencies, and added more code coverage.
Issue-ID: POLICY-2066
Change-Id: I3cae99de265c815200ec2ce71e471338772bdb5b
Signed-off-by: Pamela Dragosh <pdragosh@research.att.com>
Diffstat (limited to 'applications/optimization/src/test/java')
2 files changed, 139 insertions, 5 deletions
diff --git a/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTest.java b/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTest.java index e457e2f2..85541f18 100644 --- a/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTest.java +++ b/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTest.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,6 +24,7 @@ package org.onap.policy.xacml.pdp.application.optimization; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -50,11 +51,15 @@ import org.junit.runners.MethodSorters; import org.onap.policy.common.endpoints.parameters.RestServerParameters; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.coder.StandardYamlCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.common.utils.resources.TextFileUtils; import org.onap.policy.models.decisions.concepts.DecisionRequest; import org.onap.policy.models.decisions.concepts.DecisionResponse; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException; import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider; import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils; @@ -214,7 +219,10 @@ public class OptimizationPdpApplicationTest { // // Now load all the optimization policies // - TestUtils.loadPolicies("src/test/resources/vCPE.policies.optimization.input.tosca.yaml", service); + List<ToscaPolicy> loadedPolicies = TestUtils.loadPolicies("src/test/resources/test-optimization-policies.yaml", + service); + assertThat(loadedPolicies).isNotNull(); + assertThat(loadedPolicies).hasSize(14); // // Ask for a decision for available default policies // @@ -337,7 +345,6 @@ public class OptimizationPdpApplicationTest { // // Add gold as a scope // - //((List<String>)baseRequest.getResource().get("scope")).add("gold"); ((List<String>)baseRequest.getContext().get("subscriberName")).add("subscriber_a"); // // Ask for a decision for specific US vCPE vG gold @@ -345,7 +352,8 @@ public class OptimizationPdpApplicationTest { DecisionResponse response = makeDecision(); assertThat(response).isNotNull(); - assertThat(response.getPolicies().size()).isEqualTo(6); + assertThat(response.getPolicies()).hasSize(6); + assertThat(response.getAdvice()).hasSize(2); // // Validate it // @@ -369,7 +377,8 @@ public class OptimizationPdpApplicationTest { DecisionResponse response = makeDecision(); assertThat(response).isNotNull(); - assertThat(response.getPolicies().size()).isEqualTo(8); + assertThat(response.getPolicies()).hasSize(8); + assertThat(response.getAdvice()).hasSize(2); // // Validate it // @@ -448,6 +457,47 @@ public class OptimizationPdpApplicationTest { validateDecision(response, baseRequest); } + @Test + public void test999BadSubscriberPolicies() throws Exception { + final StandardYamlCoder yamlCoder = new StandardYamlCoder(); + // + // Decode it + // + String policyYaml = ResourceUtils.getResourceAsString("src/test/resources/bad-subscriber-policies.yaml"); + // + // Serialize it into a class + // + ToscaServiceTemplate serviceTemplate; + try { + serviceTemplate = yamlCoder.decode(policyYaml, ToscaServiceTemplate.class); + } catch (CoderException e) { + throw new XacmlApplicationException("Failed to decode policy from resource file", e); + } + // + // Make sure all the fields are setup properly + // + JpaToscaServiceTemplate jtst = new JpaToscaServiceTemplate(); + jtst.fromAuthorative(serviceTemplate); + ToscaServiceTemplate completedJtst = jtst.toAuthorative(); + // + // Get the policies + // + for (Map<String, ToscaPolicy> policies : completedJtst.getToscaTopologyTemplate().getPolicies()) { + for (ToscaPolicy policy : policies.values()) { + if ("missing-subscriberProperties".equals(policy.getName())) { + assertThatExceptionOfType(XacmlApplicationException.class).isThrownBy(() -> + service.loadPolicy(policy)); + } else if ("missing-subscriberName".equals(policy.getName())) { + assertThatExceptionOfType(XacmlApplicationException.class).isThrownBy(() -> + service.loadPolicy(policy)); + } else if ("missing-subscriberRole".equals(policy.getName())) { + assertThatExceptionOfType(XacmlApplicationException.class).isThrownBy(() -> + service.loadPolicy(policy)); + } + } + } + } + private DecisionResponse makeDecision() { Pair<DecisionResponse, Response> decision = service.makeDecision(baseRequest, null); LOGGER.info("Request Resources {}", baseRequest.getResource()); diff --git a/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationSubscriberRequestTest.java b/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationSubscriberRequestTest.java new file mode 100644 index 00000000..3fd9e44b --- /dev/null +++ b/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationSubscriberRequestTest.java @@ -0,0 +1,84 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2020 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.xacml.pdp.application.optimization; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.att.research.xacml.api.Request; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.TextFileUtils; +import org.onap.policy.models.decisions.concepts.DecisionRequest; + +public class OptimizationSubscriberRequestTest { + private static StandardCoder gson = new StandardCoder(); + private DecisionRequest request; + + /** + * setupLoadDecision loads the decision request object for testing. + * + * @throws Exception Exception if unable to load + */ + @Before + public void setupLoadDecision() throws Exception { + request = gson.decode( + TextFileUtils + .getTextFileAsString( + "src/test/resources/decision.optimization.input.json"), + DecisionRequest.class); + + assertThat(request).isNotNull(); + } + + @Test + public void testDecisionRequest() throws Exception { + // + // Add context + // + Map<String, Object> context = new HashMap<>(); + context.put("subscriberRole", "role1"); + request.setContext(context); + // + // Check the return object + // + Request xacml = OptimizationSubscriberRequest.createInstance(request); + + assertThat(xacml).isNotNull(); + assertThat(xacml.getRequestAttributes()).hasSize(4); + + List<String> roles = new ArrayList<>(); + roles.add("role-A"); + roles.add("role-B"); + context.put("subscriberRole", "role1"); + request.setContext(context); + + xacml = OptimizationSubscriberRequest.createInstance(request); + assertThat(xacml).isNotNull(); + assertThat(xacml.getRequestAttributes()).hasSize(4); + } +} |