2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2018 Ericsson. All rights reserved.
 
   4  * ================================================================================
 
   5  * Licensed under the Apache License, Version 2.0 (the "License");
 
   6  * you may not use this file except in compliance with the License.
 
   7  * You may obtain a copy of the License at
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  11  * Unless required by applicable law or agreed to in writing, software
 
  12  * distributed under the License is distributed on an "AS IS" BASIS,
 
  13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  * See the License for the specific language governing permissions and
 
  15  * limitations under the License.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.distribution.forwarding.apex.pdp;
 
  23 import static org.junit.Assert.assertTrue;
 
  24 import static org.junit.Assert.fail;
 
  25 import static org.mockito.Matchers.anyObject;
 
  26 import static org.mockito.Matchers.eq;
 
  27 import static org.mockito.Mockito.times;
 
  28 import static org.mockito.Mockito.verify;
 
  30 import java.io.FileNotFoundException;
 
  31 import java.io.IOException;
 
  32 import java.lang.reflect.Field;
 
  33 import java.util.ArrayList;
 
  34 import java.util.Collection;
 
  36 import org.junit.AfterClass;
 
  37 import org.junit.BeforeClass;
 
  38 import org.junit.Test;
 
  39 import org.junit.runner.RunWith;
 
  40 import org.mockito.Mock;
 
  41 import org.mockito.Mockito;
 
  42 import org.mockito.runners.MockitoJUnitRunner;
 
  43 import org.onap.policy.apex.core.deployment.EngineServiceFacade;
 
  44 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
 
  45 import org.onap.policy.common.parameters.ParameterGroup;
 
  46 import org.onap.policy.common.parameters.ParameterService;
 
  47 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
 
  48 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
 
  49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 
  52  * Class to perform unit test of {@link ApexPdpPolicyForwarder}.
 
  54  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
 
  56 @RunWith(MockitoJUnitRunner.class)
 
  57 public class ApexPdpPolicyForwarderTest {
 
  59     private static final String HOST_NAME = "10.10.10.10";
 
  60     private static final int PORT = 1234;
 
  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 ApexPdpPolicyForwarderParameterBuilder builder = new ApexPdpPolicyForwarderParameterBuilder();
 
  74         builder.setHostname(HOST_NAME).setPort(PORT).setIgnoreConflicts(IGNORE_CONFLICTS).setForceUpdate(FORCE_UPDATE);
 
  75         final ParameterGroup parameterGroup = new ApexPdpPolicyForwarderParameterGroup(builder);
 
  76         parameterGroup.setName(GROUP_NAME);
 
  77         ParameterService.register(parameterGroup);
 
  84     public static void tearDown() {
 
  85         ParameterService.deregister(GROUP_NAME);
 
  89     public void testForwardPolicy() throws ApexException, FileNotFoundException, IOException, PolicyForwardingException,
 
  90             NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
 
  92         final Collection<ToscaEntity> policies = new ArrayList<>();
 
  93         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
 
  94         forwarder.configure(GROUP_NAME);
 
  96         final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
 
  97         forwarderField.setAccessible(true);
 
  98         forwarderField.set(forwarder, engineServiceFacade);
 
 100         createPolicy(policies, "policy", "APEX", "Sample Policy of apex");
 
 103             forwarder.forward(policies);
 
 104             verify(engineServiceFacade, times(1)).init();
 
 105             verify(engineServiceFacade, times(1)).deployModel(eq("policy"), anyObject(), eq(IGNORE_CONFLICTS),
 
 107         } catch (final Exception exp) {
 
 108             fail("Test must not throw an exception");
 
 113     public void testForwardPolicyError()
 
 114             throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
 
 115             SecurityException, IllegalArgumentException, IllegalAccessException {
 
 117         final Collection<ToscaEntity> policies = new ArrayList<>();
 
 118         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
 
 119         forwarder.configure(GROUP_NAME);
 
 121         Mockito.doThrow(new ApexException("Failed")).when(engineServiceFacade).deployModel(eq("policy1"), anyObject(),
 
 122                 eq(IGNORE_CONFLICTS), eq(FORCE_UPDATE));
 
 124         final Field decodersField = forwarder.getClass().getDeclaredField("engineServiceFacade");
 
 125         decodersField.setAccessible(true);
 
 126         decodersField.set(forwarder, engineServiceFacade);
 
 128         createPolicy(policies, "policy1", "APEX", "Sample Policy of apex");
 
 131             forwarder.forward(policies);
 
 132             fail("Test must throw an exception");
 
 133         } catch (final Exception exp) {
 
 134             assertTrue(exp.getMessage().contains("Error sending policy to apex-pdp engine"));
 
 140     public void testForwardMoreThanOnePolicy()
 
 141             throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
 
 142             SecurityException, IllegalArgumentException, IllegalAccessException {
 
 144         final Collection<ToscaEntity> policies = new ArrayList<>();
 
 145         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
 
 146         forwarder.configure(GROUP_NAME);
 
 148         final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
 
 149         forwarderField.setAccessible(true);
 
 150         forwarderField.set(forwarder, engineServiceFacade);
 
 152         createPolicy(policies, "policy1", "APEX", "Sample Policy of apex");
 
 153         createPolicy(policies, "policy2", "APEX", "Sample Policy of apex");
 
 156             forwarder.forward(policies);
 
 157             fail("Test must throw an exception");
 
 158         } catch (final Exception exp) {
 
 159             assertTrue(exp.getMessage().contains("More than one apex policy cannot be forwarded to an apex engine"));
 
 164     public void testForwardUnsupportedPolicy()
 
 165             throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
 
 166             SecurityException, IllegalArgumentException, IllegalAccessException {
 
 168         final Collection<ToscaEntity> policies = new ArrayList<>();
 
 169         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
 
 170         forwarder.configure(GROUP_NAME);
 
 172         final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
 
 173         forwarderField.setAccessible(true);
 
 174         forwarderField.set(forwarder, engineServiceFacade);
 
 176         final ToscaEntity policy = new UnsupportedPolicy();
 
 177         policies.add(policy);
 
 180             forwarder.forward(policies);
 
 181             fail("Test must throw an exception");
 
 182         } catch (final Exception exp) {
 
 183             assertTrue(exp.getMessage().contains("Ignoring the policy as it is not of type ToscaPolicy"));
 
 187     class UnsupportedPolicy extends ToscaEntity {
 
 190         public String getName() {
 
 191             return "unsupported";
 
 195     private void createPolicy(final Collection<ToscaEntity> policies, final String name, final String type,
 
 196             final String description) {
 
 197         final ToscaPolicy policy = new ToscaPolicy();
 
 198         policy.setName(name);
 
 199         policy.setType(type);
 
 200         policy.setDescription(description);
 
 201         policies.add(policy);