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;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.util.Iterator;
31 import java.util.Properties;
32 import java.util.ServiceLoader;
34 import org.junit.BeforeClass;
35 import org.junit.ClassRule;
36 import org.junit.FixMethodOrder;
37 import org.junit.Test;
38 import org.junit.rules.TemporaryFolder;
39 import org.junit.runners.MethodSorters;
40 import org.onap.policy.common.utils.coder.CoderException;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.common.utils.resources.TextFileUtils;
43 import org.onap.policy.models.decisions.concepts.DecisionRequest;
44 import org.onap.policy.models.decisions.concepts.DecisionResponse;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
46 import org.onap.policy.pdp.xacml.application.common.TestUtils;
47 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
48 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
49 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
54 public class OptimizationPdpApplicationTest {
56 private static final Logger LOGGER = LoggerFactory.getLogger(OptimizationPdpApplicationTest.class);
57 private static Properties properties = new Properties();
58 private static File propertiesFile;
59 private static XacmlApplicationServiceProvider service;
60 private static StandardCoder gson = new StandardCoder();
61 private static DecisionRequest requestAffinity;
64 public static final TemporaryFolder policyFolder = new TemporaryFolder();
67 * Copies the xacml.properties and policies files into
68 * temporary folder and loads the service provider saving
69 * instance of provider off for other tests to use.
72 public static void setUp() throws Exception {
74 // Load Single Decision Request
76 requestAffinity = gson.decode(
79 "../../main/src/test/resources/decisions/decision.optimization.affinity.input.json"),
80 DecisionRequest.class);
82 // Setup our temporary folder
84 XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
85 propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
86 properties, myCreator);
90 ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
91 ServiceLoader.load(XacmlApplicationServiceProvider.class);
93 // Iterate through Xacml application services and find
94 // the optimization service. Save it for use throughout
95 // all the Junit tests.
97 StringBuilder strDump = new StringBuilder("Loaded applications:" + System.lineSeparator());
98 Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
99 while (iterator.hasNext()) {
100 XacmlApplicationServiceProvider application = iterator.next();
102 // Is it our service?
104 if (application instanceof OptimizationPdpApplication) {
106 // Should be the first and only one
108 assertThat(service).isNull();
109 service = application;
111 strDump.append(application.applicationName());
112 strDump.append(" supports ");
113 strDump.append(application.supportedPolicyTypes());
114 strDump.append(System.lineSeparator());
116 LOGGER.debug("{}", strDump);
118 // Tell it to initialize based on the properties file
119 // we just built for it.
121 service.initialize(propertiesFile.toPath().getParent());
125 public void test1Basics() {
127 // Make sure there's an application name
129 assertThat(service.applicationName()).isNotEmpty();
133 assertThat(service.actionDecisionsSupported().size()).isEqualTo(1);
134 assertThat(service.actionDecisionsSupported()).contains("optimize");
136 // Ensure it has the supported policy types and
137 // can support the correct policy types.
139 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
140 "onap.policies.optimization.AffinityPolicy", "1.0.0"))).isTrue();
141 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
142 "onap.foobar", "1.0.0"))).isFalse();
146 public void test2NoPolicies() {
148 // Ask for a decision
150 DecisionResponse response = service.makeDecision(requestAffinity);
151 LOGGER.info("Decision {}", response);
153 assertThat(response).isNotNull();
154 assertThat(response.getPolicies().size()).isEqualTo(0);
158 public void test3AddOptimizationPolicies() throws CoderException, FileNotFoundException, IOException,
159 XacmlApplicationException {
161 // Now load the optimization policies
163 TestUtils.loadPolicies("src/test/resources/vCPE.policies.optimization.input.tosca.yaml", service);
165 // Ask for a decision
167 DecisionResponse response = service.makeDecision(requestAffinity);
168 LOGGER.info("Decision {}", response);
170 assertThat(response).isNotNull();
171 assertThat(response.getPolicies().size()).isEqualTo(1);
173 // Dump it out as Json
175 LOGGER.info(gson.encode(response));