Use BidirectionalTopicClient from policy-common
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / parameters / BidirectionalTopicActorParamsTest.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.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.Collections;
28 import java.util.Map;
29 import java.util.function.Consumer;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.common.parameters.ValidationResult;
33 import org.onap.policy.controlloop.actorserviceprovider.Util;
34
35 public class BidirectionalTopicActorParamsTest {
36     private static final String CONTAINER = "my-container";
37
38     private static final String DFLT_SOURCE = "default-source";
39     private static final String DFLT_SINK = "default-target";
40     private static final int DFLT_TIMEOUT = 10;
41
42     private static final String OPER1_NAME = "oper A";
43     private static final String OPER1_SOURCE = "source A";
44     private static final String OPER1_SINK = "target A";
45     private static final int OPER1_TIMEOUT = 20;
46
47     // oper2 uses some default values
48     private static final String OPER2_NAME = "oper B";
49     private static final String OPER2_SOURCE = "source B";
50
51     // oper3 uses default values for everything
52     private static final String OPER3_NAME = "oper C";
53
54     private Map<String, Map<String, Object>> operMap;
55     private BidirectionalTopicActorParams params;
56
57
58     /**
59      * Sets up.
60      */
61     @Before
62     public void setUp() {
63         BidirectionalTopicParams oper1 = BidirectionalTopicParams.builder().sourceTopic(OPER1_SOURCE)
64                         .sinkTopic(OPER1_SINK).timeoutSec(OPER1_TIMEOUT).build();
65
66         Map<String, Object> oper1Map = Util.translateToMap(OPER1_NAME, oper1);
67         Map<String, Object> oper2Map = Map.of("source", OPER2_SOURCE);
68         Map<String, Object> oper3Map = Collections.emptyMap();
69         operMap = Map.of(OPER1_NAME, oper1Map, OPER2_NAME, oper2Map, OPER3_NAME, oper3Map);
70
71         params = makeBidirectionalTopicActorParams();
72     }
73
74     @Test
75     public void testValidate() {
76         assertTrue(params.validate(CONTAINER).isValid());
77
78         // only a few fields are required
79         BidirectionalTopicActorParams sparse = Util.translate(CONTAINER, Map.of("operation", operMap, "timeoutSec", 1),
80                         BidirectionalTopicActorParams.class);
81         assertTrue(sparse.validate(CONTAINER).isValid());
82
83         testValidateField("operation", "null", params2 -> params2.setOperation(null));
84         testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1));
85
86         // check edge cases
87         params.setTimeoutSec(0);
88         assertFalse(params.validate(CONTAINER).isValid());
89
90         params.setTimeoutSec(1);
91         assertTrue(params.validate(CONTAINER).isValid());
92     }
93
94     private void testValidateField(String fieldName, String expected,
95                     Consumer<BidirectionalTopicActorParams> makeInvalid) {
96
97         // original params should be valid
98         ValidationResult result = params.validate(CONTAINER);
99         assertTrue(fieldName, result.isValid());
100
101         // make invalid params
102         BidirectionalTopicActorParams params2 = makeBidirectionalTopicActorParams();
103         makeInvalid.accept(params2);
104         result = params2.validate(CONTAINER);
105         assertFalse(fieldName, result.isValid());
106         assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected);
107     }
108
109     private BidirectionalTopicActorParams makeBidirectionalTopicActorParams() {
110         BidirectionalTopicActorParams params2 = new BidirectionalTopicActorParams();
111         params2.setSinkTopic(DFLT_SINK);
112         params2.setSourceTopic(DFLT_SOURCE);
113         params2.setTimeoutSec(DFLT_TIMEOUT);
114         params2.setOperation(operMap);
115
116         return params2;
117     }
118 }