Remove dmaap from models
[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  * Modifications Copyright (C) 2024 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.test;
23
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertSame;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.verify;
30
31 import java.util.function.BiConsumer;
32 import org.junit.After;
33 import org.junit.AfterClass;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.ArgumentCaptor;
39 import org.mockito.Mock;
40 import org.mockito.junit.MockitoJUnitRunner;
41 import org.onap.policy.common.endpoints.event.comm.TopicSink;
42 import org.onap.policy.common.endpoints.event.comm.TopicSource;
43 import org.onap.policy.common.utils.coder.StandardCoderObject;
44 import org.onap.policy.simulators.TopicServer;
45
46 @RunWith(MockitoJUnitRunner.class)
47 public class BasicBidirectionalTopicOperationTest {
48     private static final String ACTOR = "my-actor";
49     private static final String OPERATION = "my-operation";
50
51     @Mock
52     private BiConsumer<String, StandardCoderObject> listener;
53
54     private BasicBidirectionalTopicOperation<String> oper;
55
56     @BeforeClass
57     public static void setUpBeforeClass() throws Exception {
58         BasicBidirectionalTopicOperation.initBeforeClass(BasicBidirectionalTopicOperation.MY_SINK,
59                         BasicBidirectionalTopicOperation.MY_SOURCE);
60     }
61
62     @AfterClass
63     public static void tearDownAfterClass() {
64         BasicBidirectionalTopicOperation.destroyAfterClass();
65     }
66
67     /**
68      * Sets up.
69      */
70     @Before
71     public void setUp() throws Exception {
72         oper = new MyOperation(ACTOR, OPERATION);
73         oper.setUpBasic();
74     }
75
76     @After
77     public void tearDown() {
78         oper.tearDownBasic();
79     }
80
81     @Test
82     public void testTopicMgr() {
83         assertNotNull(BasicBidirectionalTopicOperation.topicMgr.getTopicHandler(
84                         BasicBidirectionalTopicOperation.MY_SINK, BasicBidirectionalTopicOperation.MY_SOURCE));
85     }
86
87     @Test
88     public void testBasicBidirectionalTopicOperation() {
89         oper.tearDownBasic();
90
91         oper = new MyOperation();
92         oper.setUpBasic();
93
94         assertEquals(BasicOperation.DEFAULT_ACTOR, oper.actorName);
95         assertEquals(BasicOperation.DEFAULT_OPERATION, oper.operationName);
96     }
97
98     @Test
99     public void testBasicBidirectionalTopicOperationStringString() {
100         assertEquals(ACTOR, oper.actorName);
101         assertEquals(OPERATION, oper.operationName);
102     }
103
104     @Test
105     public void testSetUp() {
106         assertNotNull(oper.config);
107         assertNotNull(oper.outcome);
108         assertNotNull(oper.executor);
109     }
110
111     @Test
112     public void testInitOperator() {
113         oper.initConfig();
114
115         assertSame(oper.topicHandler, oper.config.getTopicHandler());
116         assertSame(oper.forwarder, oper.config.getForwarder());
117         assertEquals(BasicBidirectionalTopicOperation.TIMEOUT_MS, oper.config.getTimeoutMs());
118     }
119
120     @Test
121     public void testProvideResponse() {
122         String response = "{\"input\": 10}";
123
124         oper.provideResponse(listener, response);
125
126         ArgumentCaptor<StandardCoderObject> scoCaptor = ArgumentCaptor.forClass(StandardCoderObject.class);
127         verify(listener).accept(eq(response), scoCaptor.capture());
128
129         assertEquals("10", scoCaptor.getValue().getString("input"));
130
131         // try with an invalid response
132         assertThatIllegalArgumentException().isThrownBy(() -> oper.provideResponse(listener, "{invalid json"))
133                         .withMessage("response is not a Map");
134     }
135
136     private static class MyOperation extends BasicBidirectionalTopicOperation<String> {
137         public MyOperation() {
138             super();
139         }
140
141         /**
142          * Constructs the object.
143          *
144          * @param actor actor name
145          * @param operation operation name
146          */
147         public MyOperation(String actor, String operation) {
148             super(actor, operation);
149         }
150
151         @Override
152         protected TopicServer<String> makeServer(TopicSink sink, TopicSource source) {
153             return new TopicServer<>(sink, source, null, String.class) {
154                 @Override
155                 protected String process(String request) {
156                     return null;
157                 }
158             };
159         }
160     }
161 }