2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 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.actor.test;
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertSame;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.verify;
30 import java.util.function.BiConsumer;
31 import org.junit.After;
32 import org.junit.AfterClass;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.ArgumentCaptor;
38 import org.mockito.Mock;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.policy.common.endpoints.event.comm.TopicSink;
41 import org.onap.policy.common.endpoints.event.comm.TopicSource;
42 import org.onap.policy.common.utils.coder.StandardCoderObject;
43 import org.onap.policy.simulators.TopicServer;
45 @RunWith(MockitoJUnitRunner.class)
46 public class BasicBidirectionalTopicOperationTest {
47 private static final String ACTOR = "my-actor";
48 private static final String OPERATION = "my-operation";
51 private BiConsumer<String, StandardCoderObject> listener;
53 private BasicBidirectionalTopicOperation<String> oper;
56 public static void setUpBeforeClass() throws Exception {
57 BasicBidirectionalTopicOperation.initBeforeClass(BasicBidirectionalTopicOperation.MY_SINK,
58 BasicBidirectionalTopicOperation.MY_SOURCE);
62 public static void tearDownAfterClass() {
63 BasicBidirectionalTopicOperation.destroyAfterClass();
70 public void setUp() throws Exception {
71 oper = new MyOperation(ACTOR, OPERATION);
76 public void tearDown() {
81 public void testTopicMgr() {
82 assertNotNull(BasicBidirectionalTopicOperation.topicMgr.getTopicHandler(
83 BasicBidirectionalTopicOperation.MY_SINK, BasicBidirectionalTopicOperation.MY_SOURCE));
87 public void testBasicBidirectionalTopicOperation() {
90 oper = new MyOperation();
93 assertEquals(BasicOperation.DEFAULT_ACTOR, oper.actorName);
94 assertEquals(BasicOperation.DEFAULT_OPERATION, oper.operationName);
98 public void testBasicBidirectionalTopicOperationStringString() {
99 assertEquals(ACTOR, oper.actorName);
100 assertEquals(OPERATION, oper.operationName);
104 public void testSetUp() {
105 assertNotNull(oper.config);
106 assertNotNull(oper.outcome);
107 assertNotNull(oper.executor);
111 public void testInitOperator() {
114 assertSame(oper.topicHandler, oper.config.getTopicHandler());
115 assertSame(oper.forwarder, oper.config.getForwarder());
116 assertEquals(BasicBidirectionalTopicOperation.TIMEOUT_MS, oper.config.getTimeoutMs());
120 public void testProvideResponse() {
121 String response = "{\"input\": 10}";
123 oper.provideResponse(listener, response);
125 ArgumentCaptor<StandardCoderObject> scoCaptor = ArgumentCaptor.forClass(StandardCoderObject.class);
126 verify(listener).accept(eq(response), scoCaptor.capture());
128 assertEquals("10", scoCaptor.getValue().getString("input"));
130 // try with an invalid response
131 assertThatIllegalArgumentException().isThrownBy(() -> oper.provideResponse(listener, "{invalid json"))
132 .withMessage("response is not a Map");
135 private static class MyOperation extends BasicBidirectionalTopicOperation<String> {
136 public MyOperation() {
141 * Constructs the object.
143 * @param actor actor name
144 * @param operation operation name
146 public MyOperation(String actor, String operation) {
147 super(actor, operation);
151 protected TopicServer<String> makeServer(TopicSink sink, TopicSource source) {
152 return new TopicServer<String>(sink, source, null, String.class) {
154 protected String process(String request) {