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.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
27 import java.util.Collections;
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;
35 public class BidirectionalTopicActorParamsTest {
36 private static final String CONTAINER = "my-container";
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;
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;
47 // oper2 uses some default values
48 private static final String OPER2_NAME = "oper B";
49 private static final String OPER2_SOURCE = "source B";
51 // oper3 uses default values for everything
52 private static final String OPER3_NAME = "oper C";
54 private Map<String, Map<String, Object>> operMap;
55 private BidirectionalTopicActorParams params;
63 BidirectionalTopicParams oper1 = BidirectionalTopicParams.builder().sourceTopic(OPER1_SOURCE)
64 .sinkTopic(OPER1_SINK).timeoutSec(OPER1_TIMEOUT).build();
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);
71 params = makeBidirectionalTopicActorParams();
75 public void testValidate() {
76 assertTrue(params.validate(CONTAINER).isValid());
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());
83 testValidateField("operation", "null", params2 -> params2.setOperation(null));
84 testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1));
87 params.setTimeoutSec(0);
88 assertFalse(params.validate(CONTAINER).isValid());
90 params.setTimeoutSec(1);
91 assertTrue(params.validate(CONTAINER).isValid());
94 private void testValidateField(String fieldName, String expected,
95 Consumer<BidirectionalTopicActorParams> makeInvalid) {
97 // original params should be valid
98 ValidationResult result = params.validate(CONTAINER);
99 assertTrue(fieldName, result.isValid());
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);
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);