046aaa66a7c98e7bff5150cacd808313b9962654
[policy/xacml-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.xacml.pdp.application.optimization;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26
27 import java.io.File;
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;
33
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;
52
53 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
54 public class OptimizationPdpApplicationTest {
55
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;
62
63     @ClassRule
64     public static final TemporaryFolder policyFolder = new TemporaryFolder();
65
66     /**
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.
70      */
71     @BeforeClass
72     public static void setUp() throws Exception {
73         //
74         // Load Single Decision Request
75         //
76         requestAffinity = gson.decode(
77                 TextFileUtils
78                     .getTextFileAsString(
79                             "../../main/src/test/resources/decisions/decision.optimization.affinity.input.json"),
80                     DecisionRequest.class);
81         //
82         // Setup our temporary folder
83         //
84         XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
85         propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
86                 properties, myCreator);
87         //
88         // Load service
89         //
90         ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
91                 ServiceLoader.load(XacmlApplicationServiceProvider.class);
92         //
93         // Iterate through Xacml application services and find
94         // the optimization service. Save it for use throughout
95         // all the Junit tests.
96         //
97         StringBuilder strDump = new StringBuilder("Loaded applications:" + System.lineSeparator());
98         Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
99         while (iterator.hasNext()) {
100             XacmlApplicationServiceProvider application = iterator.next();
101             //
102             // Is it our service?
103             //
104             if (application instanceof OptimizationPdpApplication) {
105                 //
106                 // Should be the first and only one
107                 //
108                 assertThat(service).isNull();
109                 service = application;
110             }
111             strDump.append(application.applicationName());
112             strDump.append(" supports ");
113             strDump.append(application.supportedPolicyTypes());
114             strDump.append(System.lineSeparator());
115         }
116         LOGGER.debug("{}", strDump);
117         //
118         // Tell it to initialize based on the properties file
119         // we just built for it.
120         //
121         service.initialize(propertiesFile.toPath().getParent());
122     }
123
124     @Test
125     public void test1Basics() {
126         //
127         // Make sure there's an application name
128         //
129         assertThat(service.applicationName()).isNotEmpty();
130         //
131         // Decisions
132         //
133         assertThat(service.actionDecisionsSupported().size()).isEqualTo(1);
134         assertThat(service.actionDecisionsSupported()).contains("optimize");
135         //
136         // Ensure it has the supported policy types and
137         // can support the correct policy types.
138         //
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();
143     }
144
145     @Test
146     public void test2NoPolicies() {
147         //
148         // Ask for a decision
149         //
150         DecisionResponse response = service.makeDecision(requestAffinity);
151         LOGGER.info("Decision {}", response);
152
153         assertThat(response).isNotNull();
154         assertThat(response.getPolicies().size()).isEqualTo(0);
155     }
156
157     @Test
158     public void test3AddOptimizationPolicies() throws CoderException, FileNotFoundException, IOException,
159         XacmlApplicationException {
160         //
161         // Now load the optimization policies
162         //
163         TestUtils.loadPolicies("src/test/resources/vCPE.policies.optimization.input.tosca.yaml", service);
164         //
165         // Ask for a decision
166         //
167         DecisionResponse response = service.makeDecision(requestAffinity);
168         LOGGER.info("Decision {}", response);
169
170         assertThat(response).isNotNull();
171         assertThat(response.getPolicies().size()).isEqualTo(1);
172         //
173         // Dump it out as Json
174         //
175         LOGGER.info(gson.encode(response));
176     }
177 }