2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2018 Ericsson. All rights reserved.
 
   4  *  Modifications Copyright (C) 2019 Nordix Foundation.
 
   5  * ================================================================================
 
   6  * Licensed under the Apache License, Version 2.0 (the "License");
 
   7  * you may not use this file except in compliance with the License.
 
   8  * You may obtain a copy of the License at
 
  10  *      http://www.apache.org/licenses/LICENSE-2.0
 
  12  * Unless required by applicable law or agreed to in writing, software
 
  13  * distributed under the License is distributed on an "AS IS" BASIS,
 
  14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  15  * See the License for the specific language governing permissions and
 
  16  * limitations under the License.
 
  18  * SPDX-License-Identifier: Apache-2.0
 
  19  * ============LICENSE_END=========================================================
 
  22 package org.onap.policy.distribution.forwarding.apex.pdp;
 
  24 import static org.junit.Assert.assertTrue;
 
  25 import static org.junit.Assert.fail;
 
  26 import static org.mockito.Matchers.anyObject;
 
  27 import static org.mockito.Matchers.eq;
 
  28 import static org.mockito.Mockito.times;
 
  29 import static org.mockito.Mockito.verify;
 
  31 import java.io.FileNotFoundException;
 
  32 import java.io.IOException;
 
  33 import java.lang.reflect.Field;
 
  34 import java.util.ArrayList;
 
  35 import java.util.Collection;
 
  37 import org.junit.AfterClass;
 
  38 import org.junit.BeforeClass;
 
  39 import org.junit.Test;
 
  40 import org.junit.runner.RunWith;
 
  41 import org.mockito.Mock;
 
  42 import org.mockito.Mockito;
 
  43 import org.mockito.runners.MockitoJUnitRunner;
 
  44 import org.onap.policy.apex.core.deployment.EngineServiceFacade;
 
  45 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
 
  46 import org.onap.policy.common.parameters.ParameterGroup;
 
  47 import org.onap.policy.common.parameters.ParameterService;
 
  48 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
 
  49 import org.onap.policy.distribution.forwarding.xacml.pdp.testclasses.CommonTestData;
 
  50 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
 
  51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 
  54  * Class to perform unit test of {@link ApexPdpPolicyForwarder}.
 
  56  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
 
  58 @RunWith(MockitoJUnitRunner.class)
 
  59 public class ApexPdpPolicyForwarderTest {
 
  61     private static final boolean IGNORE_CONFLICTS = false;
 
  62     private static final boolean FORCE_UPDATE = true;
 
  63     private static final String GROUP_NAME = "apexPdpConfiguration";
 
  66     EngineServiceFacade engineServiceFacade;
 
  72     public static void setUp() {
 
  73         final ParameterGroup parameterGroup = CommonTestData.getPolicyForwarderParameters(
 
  74                 "src/test/resources/parameters/ApexPdpPolicyForwarderParameters.json",
 
  75                 ApexPdpPolicyForwarderParameterGroup.class);
 
  77         parameterGroup.setName(GROUP_NAME);
 
  78         ParameterService.register(parameterGroup);
 
  85     public static void tearDown() {
 
  86         ParameterService.deregister(GROUP_NAME);
 
  90     public void testForwardPolicy() throws ApexException, FileNotFoundException, IOException, PolicyForwardingException,
 
  91             NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
 
  93         final Collection<ToscaEntity> policies = new ArrayList<>();
 
  94         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
 
  95         forwarder.configure(GROUP_NAME);
 
  97         final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
 
  98         forwarderField.setAccessible(true);
 
  99         forwarderField.set(forwarder, engineServiceFacade);
 
 101         createPolicy(policies, "policy", "APEX", "Sample Policy of apex");
 
 104             forwarder.forward(policies);
 
 105             verify(engineServiceFacade, times(1)).init();
 
 106             verify(engineServiceFacade, times(1)).deployModel(eq("policy"), anyObject(), eq(IGNORE_CONFLICTS),
 
 108         } catch (final Exception exp) {
 
 109             fail("Test must not throw an exception");
 
 114     public void testForwardPolicyError()
 
 115             throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
 
 116             SecurityException, IllegalArgumentException, IllegalAccessException {
 
 118         final Collection<ToscaEntity> policies = new ArrayList<>();
 
 119         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
 
 120         forwarder.configure(GROUP_NAME);
 
 122         Mockito.doThrow(new ApexException("Failed")).when(engineServiceFacade).deployModel(eq("policy1"), anyObject(),
 
 123                 eq(IGNORE_CONFLICTS), eq(FORCE_UPDATE));
 
 125         final Field decodersField = forwarder.getClass().getDeclaredField("engineServiceFacade");
 
 126         decodersField.setAccessible(true);
 
 127         decodersField.set(forwarder, engineServiceFacade);
 
 129         createPolicy(policies, "policy1", "APEX", "Sample Policy of apex");
 
 132             forwarder.forward(policies);
 
 133             fail("Test must throw an exception");
 
 134         } catch (final Exception exp) {
 
 135             assertTrue(exp.getMessage().contains("Error sending policy to apex-pdp engine"));
 
 141     public void testForwardMoreThanOnePolicy()
 
 142             throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
 
 143             SecurityException, IllegalArgumentException, IllegalAccessException {
 
 145         final Collection<ToscaEntity> policies = new ArrayList<>();
 
 146         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
 
 147         forwarder.configure(GROUP_NAME);
 
 149         final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
 
 150         forwarderField.setAccessible(true);
 
 151         forwarderField.set(forwarder, engineServiceFacade);
 
 153         createPolicy(policies, "policy1", "APEX", "Sample Policy of apex");
 
 154         createPolicy(policies, "policy2", "APEX", "Sample Policy of apex");
 
 157             forwarder.forward(policies);
 
 158             fail("Test must throw an exception");
 
 159         } catch (final Exception exp) {
 
 160             assertTrue(exp.getMessage().contains("More than one apex policy cannot be forwarded to an apex engine"));
 
 165     public void testForwardUnsupportedPolicy()
 
 166             throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
 
 167             SecurityException, IllegalArgumentException, IllegalAccessException {
 
 169         final Collection<ToscaEntity> policies = new ArrayList<>();
 
 170         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
 
 171         forwarder.configure(GROUP_NAME);
 
 173         final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
 
 174         forwarderField.setAccessible(true);
 
 175         forwarderField.set(forwarder, engineServiceFacade);
 
 177         final ToscaEntity policy = new UnsupportedPolicy();
 
 178         policies.add(policy);
 
 181             forwarder.forward(policies);
 
 182             fail("Test must throw an exception");
 
 183         } catch (final Exception exp) {
 
 184             assertTrue(exp.getMessage().contains("Ignoring the policy as it is not of type ToscaPolicy"));
 
 188     class UnsupportedPolicy extends ToscaEntity {
 
 191         public String getName() {
 
 192             return "unsupported";
 
 196     private void createPolicy(final Collection<ToscaEntity> policies, final String name, final String type,
 
 197             final String description) {
 
 198         final ToscaPolicy policy = new ToscaPolicy();
 
 199         policy.setName(name);
 
 200         policy.setType(type);
 
 201         policy.setDescription(description);
 
 202         policies.add(policy);