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.reception.handling;
 
  23 import static org.junit.Assert.assertEquals;
 
  24 import static org.junit.Assert.assertTrue;
 
  26 import java.lang.reflect.Field;
 
  27 import java.util.ArrayList;
 
  28 import java.util.Collection;
 
  29 import java.util.Collections;
 
  30 import java.util.HashMap;
 
  32 import org.junit.Test;
 
  33 import org.onap.policy.common.parameters.ParameterService;
 
  34 import org.onap.policy.distribution.forwarding.PolicyForwarder;
 
  35 import org.onap.policy.distribution.forwarding.parameters.PolicyForwarderParameters;
 
  36 import org.onap.policy.distribution.model.PolicyInput;
 
  37 import org.onap.policy.distribution.reception.decoding.PluginInitializationException;
 
  38 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
 
  39 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
 
  40 import org.onap.policy.distribution.reception.parameters.PluginHandlerParameters;
 
  41 import org.onap.policy.distribution.reception.parameters.PolicyDecoderParameters;
 
  42 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
 
  45  * Class to perform unit test of AbstractReceptionHandler.
 
  47  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
 
  49 public class AbstractReceptionHandlerTest {
 
  51     private static final String DISTRIBUTION_GROUP = "DummyDistributionGroup";
 
  52     private static final String DECODER_TYPE = "DummyDecoder";
 
  53     private static final String DECODER_CLASS_NAME = "org.onap.policy.distribution.reception.handling.DummyDecoder";
 
  54     private static final String DECODER_KEY = "DummyDecoderKey";
 
  55     private static final String FORWARDER_KEY = "DummyForwarderKey";
 
  56     private static final String FORWARDER_TYPE = "DummyForwarder";
 
  57     private static final String FORWARDER_CLASS_NAME =
 
  58             "org.onap.policy.distribution.reception.handling.DummyPolicyForwarder";
 
  59     private static final String FORWARDER_CONFIGURATION_PARAMETERS = "DummyConfiguration";
 
  60     private static final String DECODER_CONFIGURATION_PARAMETERS = "DummyDecoderConfiguration";
 
  63     public void testInputReceived() throws PolicyDecodingException, NoSuchFieldException, SecurityException,
 
  64             IllegalArgumentException, IllegalAccessException, PluginInitializationException {
 
  65         final AbstractReceptionHandler handler = new DummyReceptionHandler();
 
  67         final ToscaEntity generatedPolicy1 = new DummyPolicy1();
 
  68         final ToscaEntity generatedPolicy2 = new DummyPolicy2();
 
  70         final PolicyDecoder<PolicyInput, ToscaEntity> policyDecoder1 =
 
  71                 new DummyDecoder(true, Collections.singletonList(generatedPolicy1));
 
  72         final PolicyDecoder<PolicyInput, ToscaEntity> policyDecoder2 =
 
  73                 new DummyDecoder(true, Collections.singletonList(generatedPolicy2));
 
  75         final Collection<PolicyDecoder<PolicyInput, ToscaEntity>> policyDecoders = new ArrayList<>();
 
  76         policyDecoders.add(policyDecoder1);
 
  77         policyDecoders.add(policyDecoder2);
 
  79         final DummyPolicyForwarder policyForwarder1 = new DummyPolicyForwarder();
 
  80         final DummyPolicyForwarder policyForwarder2 = new DummyPolicyForwarder();
 
  82         final Collection<PolicyForwarder> policyForwarders = new ArrayList<>();
 
  83         policyForwarders.add(policyForwarder1);
 
  84         policyForwarders.add(policyForwarder2);
 
  86         setUpPlugins(handler, policyDecoders, policyForwarders);
 
  88         handler.inputReceived(new DummyPolicyInput());
 
  90         assertEquals(2, policyForwarder1.getNumberOfPoliciesReceived());
 
  91         assertTrue(policyForwarder1.receivedPolicy(generatedPolicy1));
 
  92         assertTrue(policyForwarder1.receivedPolicy(generatedPolicy2));
 
  93         assertEquals(2, policyForwarder2.getNumberOfPoliciesReceived());
 
  94         assertTrue(policyForwarder2.receivedPolicy(generatedPolicy1));
 
  95         assertTrue(policyForwarder2.receivedPolicy(generatedPolicy2));
 
  98     @Test(expected = PolicyDecodingException.class)
 
  99     public void testInputReceivedNoSupportingDecoder() throws PolicyDecodingException, NoSuchFieldException,
 
 100             SecurityException, IllegalArgumentException, IllegalAccessException, PluginInitializationException {
 
 101         final AbstractReceptionHandler handler = new DummyReceptionHandler();
 
 103         final PolicyDecoder<PolicyInput, ToscaEntity> policyDecoder = new DummyDecoder(false, Collections.emptyList());
 
 104         final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder();
 
 105         setUpPlugins(handler, Collections.singleton(policyDecoder), Collections.singleton(policyForwarder));
 
 107         handler.inputReceived(new DummyPolicyInput());
 
 110     @Test(expected = PolicyDecodingException.class)
 
 111     public void testInputReceivedNoDecoder() throws PolicyDecodingException, NoSuchFieldException, SecurityException,
 
 112             IllegalArgumentException, IllegalAccessException, PluginInitializationException {
 
 113         final AbstractReceptionHandler handler = new DummyReceptionHandler();
 
 115         final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder();
 
 116         setUpPlugins(handler, Collections.emptySet(), Collections.singleton(policyForwarder));
 
 118         handler.inputReceived(new DummyPolicyInput());
 
 121     class DummyPolicyInput implements PolicyInput {
 
 124     class DummyPolicy1 extends ToscaEntity {
 
 127         public String getName() {
 
 132     class DummyPolicy2 extends ToscaEntity {
 
 135         public String getName() {
 
 140     private void setUpPlugins(final AbstractReceptionHandler receptionHandler,
 
 141             final Collection<PolicyDecoder<PolicyInput, ToscaEntity>> decoders,
 
 142             final Collection<PolicyForwarder> forwarders) throws NoSuchFieldException, SecurityException,
 
 143             IllegalArgumentException, IllegalAccessException, PluginInitializationException {
 
 144         final PluginHandlerParameters pluginParameters = getPluginHandlerParameters();
 
 145         pluginParameters.setName(DISTRIBUTION_GROUP);
 
 146         ParameterService.register(pluginParameters);
 
 147         final PluginHandler pluginHandler = new PluginHandler(pluginParameters.getName());
 
 149         final Field decodersField = pluginHandler.getClass().getDeclaredField("policyDecoders");
 
 150         decodersField.setAccessible(true);
 
 151         decodersField.set(pluginHandler, decoders);
 
 153         final Field forwardersField = pluginHandler.getClass().getDeclaredField("policyForwarders");
 
 154         forwardersField.setAccessible(true);
 
 155         forwardersField.set(pluginHandler, forwarders);
 
 157         final Field pluginHandlerField = AbstractReceptionHandler.class.getDeclaredField("pluginHandler");
 
 158         pluginHandlerField.setAccessible(true);
 
 159         pluginHandlerField.set(receptionHandler, pluginHandler);
 
 160         ParameterService.deregister(pluginParameters.getName());
 
 163     private Map<String, PolicyDecoderParameters> getPolicyDecoders() {
 
 164         final Map<String, PolicyDecoderParameters> policyDecoders = new HashMap<String, PolicyDecoderParameters>();
 
 165         final PolicyDecoderParameters pDParameters =
 
 166                 new PolicyDecoderParameters(DECODER_TYPE, DECODER_CLASS_NAME, DECODER_CONFIGURATION_PARAMETERS);
 
 167         policyDecoders.put(DECODER_KEY, pDParameters);
 
 168         return policyDecoders;
 
 171     private Map<String, PolicyForwarderParameters> getPolicyForwarders() {
 
 172         final Map<String, PolicyForwarderParameters> policyForwarders =
 
 173                 new HashMap<String, PolicyForwarderParameters>();
 
 174         final PolicyForwarderParameters pFParameters =
 
 175                 new PolicyForwarderParameters(FORWARDER_TYPE, FORWARDER_CLASS_NAME, FORWARDER_CONFIGURATION_PARAMETERS);
 
 176         policyForwarders.put(FORWARDER_KEY, pFParameters);
 
 177         return policyForwarders;
 
 180     private PluginHandlerParameters getPluginHandlerParameters() {
 
 181         final Map<String, PolicyDecoderParameters> policyDecoders = getPolicyDecoders();
 
 182         final Map<String, PolicyForwarderParameters> policyForwarders = getPolicyForwarders();
 
 183         final PluginHandlerParameters pluginHandlerParameters =
 
 184                 new PluginHandlerParameters(policyDecoders, policyForwarders);
 
 185         return pluginHandlerParameters;