2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.xacml.pdp.application.optimization;
25 import static org.assertj.core.api.Assertions.assertThat;
27 import com.att.research.xacml.api.Response;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.util.Iterator;
33 import java.util.Properties;
34 import java.util.ServiceLoader;
36 import org.apache.commons.lang3.tuple.Pair;
37 import org.junit.BeforeClass;
38 import org.junit.ClassRule;
39 import org.junit.FixMethodOrder;
40 import org.junit.Test;
41 import org.junit.rules.TemporaryFolder;
42 import org.junit.runners.MethodSorters;
43 import org.onap.policy.common.utils.coder.CoderException;
44 import org.onap.policy.common.utils.coder.StandardCoder;
45 import org.onap.policy.common.utils.resources.TextFileUtils;
46 import org.onap.policy.models.decisions.concepts.DecisionRequest;
47 import org.onap.policy.models.decisions.concepts.DecisionResponse;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
49 import org.onap.policy.pdp.xacml.application.common.TestUtils;
50 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
51 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
52 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
57 public class OptimizationPdpApplicationTest {
59 private static final Logger LOGGER = LoggerFactory.getLogger(OptimizationPdpApplicationTest.class);
60 private static Properties properties = new Properties();
61 private static File propertiesFile;
62 private static XacmlApplicationServiceProvider service;
63 private static StandardCoder gson = new StandardCoder();
64 private static DecisionRequest requestAffinity;
67 public static final TemporaryFolder policyFolder = new TemporaryFolder();
70 * Copies the xacml.properties and policies files into
71 * temporary folder and loads the service provider saving
72 * instance of provider off for other tests to use.
75 public static void setUp() throws Exception {
77 // Load Single Decision Request
79 requestAffinity = gson.decode(
82 "../../main/src/test/resources/decisions/decision.optimization.affinity.input.json"),
83 DecisionRequest.class);
85 // Setup our temporary folder
87 XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
88 propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
89 properties, myCreator);
93 ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
94 ServiceLoader.load(XacmlApplicationServiceProvider.class);
96 // Iterate through Xacml application services and find
97 // the optimization service. Save it for use throughout
98 // all the Junit tests.
100 StringBuilder strDump = new StringBuilder("Loaded applications:" + System.lineSeparator());
101 Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
102 while (iterator.hasNext()) {
103 XacmlApplicationServiceProvider application = iterator.next();
105 // Is it our service?
107 if (application instanceof OptimizationPdpApplication) {
109 // Should be the first and only one
111 assertThat(service).isNull();
112 service = application;
114 strDump.append(application.applicationName());
115 strDump.append(" supports ");
116 strDump.append(application.supportedPolicyTypes());
117 strDump.append(System.lineSeparator());
119 LOGGER.debug("{}", strDump);
121 // Tell it to initialize based on the properties file
122 // we just built for it.
124 service.initialize(propertiesFile.toPath().getParent());
128 public void test1Basics() {
130 // Make sure there's an application name
132 assertThat(service.applicationName()).isNotEmpty();
136 assertThat(service.actionDecisionsSupported().size()).isEqualTo(1);
137 assertThat(service.actionDecisionsSupported()).contains("optimize");
139 // Ensure it has the supported policy types and
140 // can support the correct policy types.
142 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
143 "onap.policies.optimization.AffinityPolicy", "1.0.0"))).isTrue();
144 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
145 "onap.foobar", "1.0.0"))).isFalse();
149 public void test2NoPolicies() {
151 // Ask for a decision
153 Pair<DecisionResponse, Response> decision = service.makeDecision(requestAffinity);
154 LOGGER.info("Decision {}", decision.getKey());
156 assertThat(decision.getKey()).isNotNull();
157 assertThat(decision.getKey().getPolicies().size()).isEqualTo(0);
161 public void test3AddOptimizationPolicies() throws CoderException, FileNotFoundException, IOException,
162 XacmlApplicationException {
164 // Now load the optimization policies
166 TestUtils.loadPolicies("src/test/resources/vCPE.policies.optimization.input.tosca.yaml", service);
168 // Ask for a decision
170 Pair<DecisionResponse, Response> decision = service.makeDecision(requestAffinity);
171 LOGGER.info("Decision {}", decision.getKey());
173 assertThat(decision.getKey()).isNotNull();
174 assertThat(decision.getKey().getPolicies().size()).isEqualTo(1);
176 // Dump it out as Json
178 LOGGER.info(gson.encode(decision.getKey()));