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