2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2021 Nordix Foundation.
 
   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.parameters;
 
  23 import static org.assertj.core.api.Assertions.assertThat;
 
  25 import java.util.HashMap;
 
  27 import org.junit.Test;
 
  28 import org.onap.policy.distribution.forwarding.parameters.PolicyForwarderParameters;
 
  31  * Class for unit testing ReceptionHandlerParameters class.
 
  33  * @author Adheli Tavares (adheli.tavares@est.tech)
 
  36 public class TestReceptionHandlerParameters {
 
  39     public void testValidate_ClassNotFound() {
 
  40         final String className = "org.onap.policy.distribution.reception.testclasses.NotExistent";
 
  42         ReceptionHandlerParameters sutParams = getReceptionHandlerParameters(className);
 
  43         sutParams.setName(className);
 
  45         assertThat(sutParams.validate().getResult()).contains("reception handler class not found in classpath");
 
  49     public void testValidate_ReceptionHandlerTypeNullEmpty() {
 
  50         final String className = "org.onap.policy.distribution.reception.handling.DummyReceptionHandler";
 
  51         final PluginHandlerParameters pHParameters =
 
  52                 new PluginHandlerParameters(getPolicyDecoders(), getPolicyForwarders());
 
  54         ReceptionHandlerParameters nullType = new ReceptionHandlerParameters(null, className, className, pHParameters);
 
  56         assertThat(nullType.validate().getResult()).contains("field \"receptionHandlerType\" type \"java.lang.String\""
 
  57                 + " value \"null\" INVALID, must be a non-blank string");
 
  59         ReceptionHandlerParameters emptyType = new ReceptionHandlerParameters("", className, className, pHParameters);
 
  61         assertThat(emptyType.validate().getResult()).contains("field \"receptionHandlerType\" type \"java.lang.String\""
 
  62                 + " value \"\" INVALID, must be a non-blank string");
 
  63         assertThat(emptyType.validate().getResult()).doesNotContain("reception handler class not found in classpath");
 
  67     public void testValidate_ReceptionHandlerClassNameNullEmpty() {
 
  68         final PluginHandlerParameters pHParameters =
 
  69                 new PluginHandlerParameters(getPolicyDecoders(), getPolicyForwarders());
 
  71         ReceptionHandlerParameters nullType = new ReceptionHandlerParameters("DummyReceptionHandler", null,
 
  72                 "dummyReceptionHandlerConfiguration", pHParameters);
 
  74         assertThat(nullType.validate().getResult())
 
  75                 .contains("field \"receptionHandlerClassName\" type \"java.lang.String\" value "
 
  76                         + "\"null\" INVALID, must be a non-blank string containing full class name "
 
  77                         + "of the reception handler");
 
  79         ReceptionHandlerParameters emptyType = new ReceptionHandlerParameters("DummyReceptionHandler", "",
 
  80                 "dummyReceptionHandlerConfiguration", pHParameters);
 
  82         assertThat(emptyType.validate().getResult())
 
  83                 .contains("field \"receptionHandlerClassName\" type \"java.lang.String\" value "
 
  84                         + "\"\" INVALID, must be a non-blank string containing full class name of "
 
  85                         + "the reception handler");
 
  89     public void testValidate_PluginHandlerParametersNull() {
 
  90         final String className = "org.onap.policy.distribution.reception.testclasses.DummyReceptionHandler";
 
  92         ReceptionHandlerParameters sutParams = new ReceptionHandlerParameters("DummyReceptionHandler", className,
 
  93                 "dummyReceptionHandlerConfiguration", null);
 
  95         assertThat(sutParams.validate().getResult())
 
  96                 .contains("parameter group \"UNDEFINED\" INVALID, must have a plugin handler");
 
  99     private ReceptionHandlerParameters getReceptionHandlerParameters(String className) {
 
 100         final Map<String, PolicyDecoderParameters> policyDecoders = getPolicyDecoders();
 
 101         final Map<String, PolicyForwarderParameters> policyForwarders = getPolicyForwarders();
 
 102         final PluginHandlerParameters pHParameters = new PluginHandlerParameters(policyDecoders, policyForwarders);
 
 103         final ReceptionHandlerParameters rhParameters = new ReceptionHandlerParameters("DummyReceptionHandler",
 
 104                 className, "dummyReceptionHandlerConfiguration", pHParameters);
 
 108     private Map<String, PolicyDecoderParameters> getPolicyDecoders() {
 
 109         final Map<String, PolicyDecoderParameters> policyDecoders = new HashMap<>();
 
 111         final PolicyDecoderParameters pDParameters =
 
 112                 new PolicyDecoderParameters("DummyDecoder", "DummyDecoder", "dummyDecoderConfiguration");
 
 113         policyDecoders.put("DummyDecoder", pDParameters);
 
 115         return policyDecoders;
 
 118     private Map<String, PolicyForwarderParameters> getPolicyForwarders() {
 
 119         final Map<String, PolicyForwarderParameters> policyForwarders = new HashMap<>();
 
 121         final PolicyForwarderParameters pFParameters =
 
 122                 new PolicyForwarderParameters("DummyForwarder", "DummyForwarder", "dummyForwarderConfiguration");
 
 123         policyForwarders.put("DummyForwarder", pFParameters);
 
 125         return policyForwarders;