2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.actorserviceprovider.parameters;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
29 import java.util.Collections;
31 import java.util.TreeMap;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.parameters.ValidationResult;
35 import org.onap.policy.controlloop.actorserviceprovider.Util;
37 public class TopicPairActorParamsTest {
38 private static final String MY_NAME = "my-name";
40 private static final String DFLT_SOURCE = "default-source";
41 private static final String DFLT_TARGET = "default-target";
42 private static final int DFLT_TIMEOUT = 10;
44 private static final String OPER1_NAME = "oper A";
45 private static final String OPER1_SOURCE = "source A";
46 private static final String OPER1_TARGET = "target A";
47 private static final int OPER1_TIMEOUT = 20;
49 // oper2 uses some default values
50 private static final String OPER2_NAME = "oper B";
51 private static final String OPER2_SOURCE = "source B";
53 // oper3 uses default values for everything
54 private static final String OPER3_NAME = "oper C";
56 private TopicPairParams defaults;
57 private Map<String, Map<String, Object>> operMap;
58 private TopicPairActorParams params;
66 defaults = TopicPairParams.builder().source(DFLT_SOURCE).target(DFLT_TARGET).timeoutSec(DFLT_TIMEOUT).build();
68 TopicPairParams oper1 = TopicPairParams.builder().source(OPER1_SOURCE).target(OPER1_TARGET)
69 .timeoutSec(OPER1_TIMEOUT).build();
71 Map<String, Object> oper1Map = Util.translateToMap(OPER1_NAME, oper1);
72 Map<String, Object> oper2Map = Map.of("source", OPER2_SOURCE);
73 Map<String, Object> oper3Map = Collections.emptyMap();
74 operMap = Map.of(OPER1_NAME, oper1Map, OPER2_NAME, oper2Map, OPER3_NAME, oper3Map);
76 params = TopicPairActorParams.builder().defaults(defaults).operation(operMap).build();
81 public void testTopicPairActorParams() {
82 assertSame(defaults, params.getDefaults());
83 assertSame(operMap, params.getOperation());
87 public void testDoValidation() {
88 assertSame(params, params.doValidation(MY_NAME));
90 // test with invalid parameters
91 defaults.setTimeoutSec(-1);
92 assertThatThrownBy(() -> params.doValidation(MY_NAME)).isInstanceOf(ParameterValidationRuntimeException.class);
96 public void testValidate() {
97 ValidationResult result;
100 params.setDefaults(null);
101 result = params.validate(MY_NAME);
102 assertFalse(result.isValid());
103 assertThat(result.getResult()).contains("defaults").contains("null");
104 params.setDefaults(defaults);
106 // invalid value in defaults
107 defaults.setTimeoutSec(-1);
108 result = params.validate(MY_NAME);
109 assertFalse(result.isValid());
110 assertThat(result.getResult()).contains("defaults").contains("timeoutSec");
111 defaults.setTimeoutSec(DFLT_TIMEOUT);
114 params.setOperation(null);
115 result = params.validate(MY_NAME);
116 assertFalse(result.isValid());
117 assertThat(result.getResult()).contains("operation");
118 params.setOperation(operMap);
120 // null entry in the map
121 Map<String, Map<String, Object>> map2 = new TreeMap<>(operMap);
122 map2.put(OPER2_NAME, null);
123 params.setOperation(map2);
124 result = params.validate(MY_NAME);
125 assertFalse(result.isValid());
126 assertThat(result.getResult()).contains("operation").contains("null");
127 params.setOperation(operMap);
130 assertTrue(params.validate(MY_NAME).isValid());