4322c5f395e5afd4938d89d7dcf8566edbedfb63
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / parameters / TopicPairActorParamsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actorserviceprovider.parameters;
22
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;
28
29 import java.util.Collections;
30 import java.util.Map;
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;
36
37 public class TopicPairActorParamsTest {
38     private static final String MY_NAME = "my-name";
39
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;
43
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;
48
49     // oper2 uses some default values
50     private static final String OPER2_NAME = "oper B";
51     private static final String OPER2_SOURCE = "source B";
52
53     // oper3 uses default values for everything
54     private static final String OPER3_NAME = "oper C";
55
56     private TopicPairParams defaults;
57     private Map<String, Map<String, Object>> operMap;
58     private TopicPairActorParams params;
59
60
61     /**
62      * Sets up.
63      */
64     @Before
65     public void setUp() {
66         defaults = TopicPairParams.builder().source(DFLT_SOURCE).target(DFLT_TARGET).timeoutSec(DFLT_TIMEOUT).build();
67
68         TopicPairParams oper1 = TopicPairParams.builder().source(OPER1_SOURCE).target(OPER1_TARGET)
69                         .timeoutSec(OPER1_TIMEOUT).build();
70
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);
75
76         params = TopicPairActorParams.builder().defaults(defaults).operation(operMap).build();
77
78     }
79
80     @Test
81     public void testTopicPairActorParams() {
82         assertSame(defaults, params.getDefaults());
83         assertSame(operMap, params.getOperation());
84     }
85
86     @Test
87     public void testDoValidation() {
88         assertSame(params, params.doValidation(MY_NAME));
89
90         // test with invalid parameters
91         defaults.setTimeoutSec(-1);
92         assertThatThrownBy(() -> params.doValidation(MY_NAME)).isInstanceOf(ParameterValidationRuntimeException.class);
93     }
94
95     @Test
96     public void testValidate() {
97         ValidationResult result;
98
99         // null defaults
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);
105
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);
112
113         // null map
114         params.setOperation(null);
115         result = params.validate(MY_NAME);
116         assertFalse(result.isValid());
117         assertThat(result.getResult()).contains("operation");
118         params.setOperation(operMap);
119
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);
128
129         // test success case
130         assertTrue(params.validate(MY_NAME).isValid());
131     }
132 }