c708a17cd74935b04d5eace324cf05ac1b1fe0aa
[policy/models.git] / models-interactions / model-actors / actor.test / src / test / java / org / onap / policy / controlloop / actor / test / BasicBidirectionalTopicOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.actor.test;
22
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;
29
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;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class BasicBidirectionalTopicOperationTest {
47     private static final String ACTOR = "my-actor";
48     private static final String OPERATION = "my-operation";
49
50     @Mock
51     private BiConsumer<String, StandardCoderObject> listener;
52
53     private BasicBidirectionalTopicOperation<String> oper;
54
55     @BeforeClass
56     public static void setUpBeforeClass() throws Exception {
57         BasicBidirectionalTopicOperation.initBeforeClass(BasicBidirectionalTopicOperation.MY_SINK,
58                         BasicBidirectionalTopicOperation.MY_SOURCE);
59     }
60
61     @AfterClass
62     public static void tearDownAfterClass() {
63         BasicBidirectionalTopicOperation.destroyAfterClass();
64     }
65
66     /**
67      * Sets up.
68      */
69     @Before
70     public void setUp() throws Exception {
71         oper = new MyOperation(ACTOR, OPERATION);
72         oper.setUpBasic();
73     }
74
75     @After
76     public void tearDown() {
77         oper.tearDownBasic();
78     }
79
80     @Test
81     public void testTopicMgr() {
82         assertNotNull(BasicBidirectionalTopicOperation.topicMgr.getTopicHandler(
83                         BasicBidirectionalTopicOperation.MY_SINK, BasicBidirectionalTopicOperation.MY_SOURCE));
84     }
85
86     @Test
87     public void testBasicBidirectionalTopicOperation() {
88         oper.tearDownBasic();
89
90         oper = new MyOperation();
91         oper.setUpBasic();
92
93         assertEquals(BasicOperation.DEFAULT_ACTOR, oper.actorName);
94         assertEquals(BasicOperation.DEFAULT_OPERATION, oper.operationName);
95     }
96
97     @Test
98     public void testBasicBidirectionalTopicOperationStringString() {
99         assertEquals(ACTOR, oper.actorName);
100         assertEquals(OPERATION, oper.operationName);
101     }
102
103     @Test
104     public void testSetUp() {
105         assertNotNull(oper.config);
106         assertNotNull(oper.outcome);
107         assertNotNull(oper.executor);
108     }
109
110     @Test
111     public void testInitOperator() {
112         oper.initConfig();
113
114         assertSame(oper.topicHandler, oper.config.getTopicHandler());
115         assertSame(oper.forwarder, oper.config.getForwarder());
116         assertEquals(BasicBidirectionalTopicOperation.TIMEOUT_MS, oper.config.getTimeoutMs());
117     }
118
119     @Test
120     public void testProvideResponse() {
121         String response = "{\"input\": 10}";
122
123         oper.provideResponse(listener, response);
124
125         ArgumentCaptor<StandardCoderObject> scoCaptor = ArgumentCaptor.forClass(StandardCoderObject.class);
126         verify(listener).accept(eq(response), scoCaptor.capture());
127
128         assertEquals("10", scoCaptor.getValue().getString("input"));
129
130         // try with an invalid response
131         assertThatIllegalArgumentException().isThrownBy(() -> oper.provideResponse(listener, "{invalid json"))
132                         .withMessage("response is not a Map");
133     }
134
135     private static class MyOperation extends BasicBidirectionalTopicOperation<String> {
136         public MyOperation() {
137             super();
138         }
139
140         /**
141          * Constructs the object.
142          *
143          * @param actor actor name
144          * @param operation operation name
145          */
146         public MyOperation(String actor, String operation) {
147             super(actor, operation);
148         }
149
150         @Override
151         protected TopicServer<String> makeServer(TopicSink sink, TopicSource source) {
152             return new TopicServer<String>(sink, source, null, String.class) {
153                 @Override
154                 protected String process(String request) {
155                     return null;
156                 }
157             };
158         }
159     }
160 }