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.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.Before;
32 import org.junit.Test;
33 import org.mockito.ArgumentCaptor;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.policy.common.utils.coder.StandardCoderObject;
38 public class BasicBidirectionalTopicOperationTest {
39 private static final String ACTOR = "my-actor";
40 private static final String OPERATION = "my-operation";
43 private BiConsumer<String, StandardCoderObject> listener;
45 private BasicBidirectionalTopicOperation oper;
52 public void setUp() throws Exception {
53 MockitoAnnotations.initMocks(this);
55 oper = new BasicBidirectionalTopicOperation(ACTOR, OPERATION);
60 public void testBasicBidirectionalTopicOperation() {
61 oper = new BasicBidirectionalTopicOperation();
62 assertEquals(BasicOperation.DEFAULT_ACTOR, oper.actorName);
63 assertEquals(BasicOperation.DEFAULT_OPERATION, oper.operationName);
67 public void testBasicBidirectionalTopicOperationStringString() {
68 assertEquals(ACTOR, oper.actorName);
69 assertEquals(OPERATION, oper.operationName);
73 public void testSetUp() {
74 assertNotNull(oper.config);
75 assertNotNull(oper.context);
76 assertNotNull(oper.outcome);
77 assertNotNull(oper.executor);
81 public void testInitOperator() {
84 assertSame(oper.topicHandler, oper.config.getTopicHandler());
85 assertSame(oper.forwarder, oper.config.getForwarder());
86 assertEquals(BasicBidirectionalTopicOperation.TIMEOUT_MS, oper.config.getTimeoutMs());
90 public void testProvideResponse() {
91 String response = "{\"input\": 10}";
93 oper.provideResponse(listener, response);
95 ArgumentCaptor<StandardCoderObject> scoCaptor = ArgumentCaptor.forClass(StandardCoderObject.class);
96 verify(listener).accept(eq(response), scoCaptor.capture());
98 assertEquals("10", scoCaptor.getValue().getString("input"));
100 // try with an invalid response
101 assertThatIllegalArgumentException().isThrownBy(() -> oper.provideResponse(listener, "{invalid json"))
102 .withMessage("response is not a Map");