c795f9cc50a8738a5ce65dd94e1bddd87528b948
[policy/models.git] /
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.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.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.jupiter.api.AfterAll;
33 import org.junit.jupiter.api.AfterEach;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.TestInstance;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.mockito.ArgumentCaptor;
40 import org.mockito.InjectMocks;
41 import org.mockito.Mock;
42 import org.mockito.junit.jupiter.MockitoExtension;
43 import org.onap.policy.common.endpoints.event.comm.TopicSink;
44 import org.onap.policy.common.endpoints.event.comm.TopicSource;
45 import org.onap.policy.common.utils.coder.StandardCoderObject;
46 import org.onap.policy.simulators.TopicServer;
47
48 @TestInstance(TestInstance.Lifecycle.PER_CLASS)
49 @ExtendWith(MockitoExtension.class)
50 class BasicBidirectionalTopicOperationTest {
51     private static final String ACTOR = "my-actor";
52     private static final String OPERATION = "my-operation";
53
54     @Mock
55     private BiConsumer<String, StandardCoderObject> listener;
56
57     private BasicBidirectionalTopicOperation<String> oper = new MyOperation(ACTOR, OPERATION);
58
59     @BeforeAll
60     void setUpBeforeClass() throws Exception {
61         BasicBidirectionalTopicOperation.initBeforeClass(BasicBidirectionalTopicOperation.MY_SINK,
62                         BasicBidirectionalTopicOperation.MY_SOURCE);
63     }
64
65     @AfterAll
66     static void tearDownAfterClass() {
67         BasicBidirectionalTopicOperation.destroyAfterClass();
68     }
69
70     /**
71      * Sets up.
72      */
73     @BeforeEach
74     void setUp() throws Exception {
75         oper.setUpBasic();
76     }
77
78     @AfterEach
79     void tearDown() {
80         oper.tearDownBasic();
81     }
82
83     @Test
84     void testTopicMgr() {
85         assertNotNull(BasicBidirectionalTopicOperation.topicMgr.getTopicHandler(
86                         BasicBidirectionalTopicOperation.MY_SINK, BasicBidirectionalTopicOperation.MY_SOURCE));
87     }
88
89     @Test
90     void testBasicBidirectionalTopicOperation() {
91         oper.tearDownBasic();
92
93         oper = new MyOperation();
94         oper.setUpBasic();
95
96         assertEquals(BasicOperation.DEFAULT_ACTOR, oper.actorName);
97         assertEquals(BasicOperation.DEFAULT_OPERATION, oper.operationName);
98     }
99
100     @Test
101     void testSetUp() {
102         assertNotNull(oper.config);
103         assertNotNull(oper.outcome);
104         assertNotNull(oper.executor);
105     }
106
107     @Test
108     void testInitOperator() {
109         oper.initConfig();
110
111         assertSame(oper.topicHandler, oper.config.getTopicHandler());
112         assertSame(oper.forwarder, oper.config.getForwarder());
113         assertEquals(BasicBidirectionalTopicOperation.TIMEOUT_MS, oper.config.getTimeoutMs());
114     }
115
116     @Test
117     void testProvideResponse() {
118         String response = "{\"input\": 10}";
119
120         oper.provideResponse(listener, response);
121
122         ArgumentCaptor<StandardCoderObject> scoCaptor = ArgumentCaptor.forClass(StandardCoderObject.class);
123         verify(listener).accept(eq(response), scoCaptor.capture());
124
125         assertEquals("10", scoCaptor.getValue().getString("input"));
126
127         // try with an invalid response
128         assertThatIllegalArgumentException().isThrownBy(() -> oper.provideResponse(listener, "{invalid json"))
129                         .withMessage("response is not a Map");
130     }
131
132     private static class MyOperation extends BasicBidirectionalTopicOperation<String> {
133         public MyOperation() {
134             super();
135         }
136
137         /**
138          * Constructs the object.
139          *
140          * @param actor actor name
141          * @param operation operation name
142          */
143         public MyOperation(String actor, String operation) {
144             super(actor, operation);
145         }
146
147         @Override
148         protected TopicServer<String> makeServer(TopicSink sink, TopicSource source) {
149             return new TopicServer<>(sink, source, null, String.class) {
150                 @Override
151                 protected String process(String request) {
152                     return null;
153                 }
154             };
155         }
156     }
157 }