Use BidirectionalTopicClient from policy-common
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / parameters / BidirectionalTopicParamsTest.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.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27
28 import java.util.function.Function;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.common.parameters.ValidationResult;
32 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams.BidirectionalTopicParamsBuilder;
33
34 public class BidirectionalTopicParamsTest {
35
36     private static final String CONTAINER = "my-container";
37     private static final String SINK = "my-sink";
38     private static final String SOURCE = "my-source";
39     private static final int TIMEOUT = 10;
40
41     private BidirectionalTopicParams params;
42
43     @Before
44     public void setUp() {
45         params = BidirectionalTopicParams.builder().sinkTopic(SINK).sourceTopic(SOURCE).timeoutSec(TIMEOUT).build();
46     }
47
48     @Test
49     public void testValidate() {
50         assertTrue(params.validate(CONTAINER).isValid());
51
52         testValidateField("sink", "null", bldr -> bldr.sinkTopic(null));
53         testValidateField("source", "null", bldr -> bldr.sourceTopic(null));
54         testValidateField("timeoutSec", "minimum", bldr -> bldr.timeoutSec(-1));
55
56         // check edge cases
57         assertFalse(params.toBuilder().timeoutSec(0).build().validate(CONTAINER).isValid());
58         assertTrue(params.toBuilder().timeoutSec(1).build().validate(CONTAINER).isValid());
59     }
60
61     @Test
62     public void testBuilder_testToBuilder() {
63         assertEquals(SINK, params.getSinkTopic());
64         assertEquals(SOURCE, params.getSourceTopic());
65         assertEquals(TIMEOUT, params.getTimeoutSec());
66
67         assertEquals(params, params.toBuilder().build());
68     }
69
70     private void testValidateField(String fieldName, String expected,
71                     Function<BidirectionalTopicParamsBuilder, BidirectionalTopicParamsBuilder> makeInvalid) {
72
73         // original params should be valid
74         ValidationResult result = params.validate(CONTAINER);
75         assertTrue(fieldName, result.isValid());
76
77         // make invalid params
78         result = makeInvalid.apply(params.toBuilder()).build().validate(CONTAINER);
79         assertFalse(fieldName, result.isValid());
80         assertThat(result.getResult()).contains(fieldName).contains(expected);
81     }
82 }