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.guard;
 
  25 import static org.assertj.core.api.Assertions.assertThat;
 
  26 import static org.assertj.core.api.Assertions.assertThatCode;
 
  28 import com.att.research.xacml.util.XACMLProperties;
 
  29 import com.google.common.io.Files;
 
  30 import com.google.gson.Gson;
 
  33 import java.io.FileInputStream;
 
  34 import java.io.FileOutputStream;
 
  35 import java.io.InputStream;
 
  36 import java.io.OutputStream;
 
  37 import java.nio.file.Path;
 
  38 import java.nio.file.Paths;
 
  39 import java.util.Iterator;
 
  40 import java.util.Properties;
 
  41 import java.util.ServiceLoader;
 
  43 import org.junit.Before;
 
  44 import org.junit.ClassRule;
 
  45 import org.junit.Test;
 
  46 import org.junit.rules.TemporaryFolder;
 
  47 import org.onap.policy.common.utils.resources.TextFileUtils;
 
  48 import org.onap.policy.models.decisions.concepts.DecisionRequest;
 
  49 import org.onap.policy.models.decisions.serialization.DecisionRequestMessageBodyHandler;
 
  50 import org.onap.policy.models.decisions.serialization.DecisionResponseMessageBodyHandler;
 
  51 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
 
  52 import org.slf4j.Logger;
 
  53 import org.slf4j.LoggerFactory;
 
  55 public class GuardPdpApplicationTest {
 
  57     private static final Logger LOGGER = LoggerFactory.getLogger(GuardPdpApplicationTest.class);
 
  58     private static Properties properties = new Properties();
 
  59     private static File propertiesFile;
 
  60     private static XacmlApplicationServiceProvider service;
 
  61     private static DecisionRequest requestSinglePolicy;
 
  63     private static Gson gsonDecisionRequest;
 
  64     private static Gson gsonDecisionResponse;
 
  67     public static final TemporaryFolder policyFolder = new TemporaryFolder();
 
  70     public void setUp() throws Exception {
 
  75     public void testBasics() {
 
  76         assertThatCode(() -> {
 
  78             // Create our Gson builder
 
  80             gsonDecisionRequest = new DecisionRequestMessageBodyHandler().getGson();
 
  81             gsonDecisionResponse = new DecisionResponseMessageBodyHandler().getGson();
 
  83             // Load Single Decision Request
 
  85             requestSinglePolicy = gsonDecisionRequest.fromJson(
 
  87                         .getTextFileAsString("../../main/src/test/resources/decisions/decision.single.input.json"),
 
  88                         DecisionRequest.class);
 
  90             // Copy all the properties and root policies to the temporary folder
 
  92             try (InputStream is = new FileInputStream("src/test/resources/xacml.properties")) {
 
  97                 propertiesFile = policyFolder.newFile("xacml.properties");
 
  99                 // Copy the root policies
 
 101                 for (String root : XACMLProperties.getRootPolicyIDs(properties)) {
 
 105                     Path rootPath = Paths.get(properties.getProperty(root + ".file"));
 
 106                     LOGGER.debug("Root file {} {}", rootPath, rootPath.getFileName());
 
 108                     // Construct new file name
 
 110                     File newRootPath = policyFolder.newFile(rootPath.getFileName().toString());
 
 114                     Files.copy(rootPath.toFile(), newRootPath);
 
 115                     assertThat(newRootPath).exists();
 
 117                     // Point to where the new policy is in the temp dir
 
 119                     properties.setProperty(root + ".file", newRootPath.getAbsolutePath());
 
 121                 try (OutputStream os = new FileOutputStream(propertiesFile.getAbsolutePath())) {
 
 122                     properties.store(os, "");
 
 123                     assertThat(propertiesFile).exists();
 
 129             ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
 
 130                     ServiceLoader.load(XacmlApplicationServiceProvider.class);
 
 132             // Iterate through them - I could store the object as
 
 133             // XacmlApplicationServiceProvider pointer.
 
 137             StringBuilder strDump = new StringBuilder("Loaded applications:" + System.lineSeparator());
 
 138             Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
 
 139             while (iterator.hasNext()) {
 
 140                 XacmlApplicationServiceProvider application = iterator.next();
 
 142                 // Is it our service?
 
 144                 if (application instanceof GuardPdpApplication) {
 
 146                     // Should be the first and only one
 
 148                     assertThat(service).isNull();
 
 149                     service = application;
 
 151                 strDump.append(application.applicationName());
 
 152                 strDump.append(" supports ");
 
 153                 strDump.append(application.supportedPolicyTypes());
 
 154                 strDump.append(System.lineSeparator());
 
 156             LOGGER.debug("{}", strDump);
 
 158             // Tell it to initialize based on the properties file
 
 159             // we just built for it.
 
 161             service.initialize(propertiesFile.toPath().getParent());
 
 163             // Make sure there's an application name
 
 165             assertThat(service.applicationName()).isNotEmpty();
 
 169             assertThat(service.actionDecisionsSupported().size()).isEqualTo(1);
 
 170             assertThat(service.actionDecisionsSupported()).contains("guard");
 
 172             // Ensure it has the supported policy types and
 
 173             // can support the correct policy types.
 
 175             assertThat(service.supportedPolicyTypes()).isNotEmpty();
 
 176             assertThat(service.supportedPolicyTypes().size()).isEqualTo(2);
 
 177             assertThat(service.canSupportPolicyType("onap.policies.controlloop.guard.FrequencyLimiter", "1.0.0"))
 
 179             assertThat(service.canSupportPolicyType("onap.policies.controlloop.guard.FrequencyLimiter", "1.0.1"))
 
 181             assertThat(service.canSupportPolicyType("onap.policies.controlloop.guard.MinMax", "1.0.0")).isTrue();
 
 182             assertThat(service.canSupportPolicyType("onap.policies.controlloop.guard.MinMax", "1.0.1")).isFalse();
 
 183             assertThat(service.canSupportPolicyType("onap.foo", "1.0.1")).isFalse();
 
 185             // Ensure it supports decisions
 
 187             assertThat(service.actionDecisionsSupported()).contains("guard");
 
 188         }).doesNotThrowAnyException();