Merge "More changes to actor code"
[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 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.assertNull;
27 import static org.junit.Assert.assertSame;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.verify;
31
32 import java.util.function.BiConsumer;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.onap.policy.common.utils.coder.StandardCoderObject;
39
40 public class BasicBidirectionalTopicOperationTest {
41     private static final String ACTOR = "my-actor";
42     private static final String OPERATION = "my-operation";
43
44     @Mock
45     private BiConsumer<String, StandardCoderObject> listener;
46
47     private BasicBidirectionalTopicOperation oper;
48
49
50     /**
51      * Sets up.
52      */
53     @Before
54     public void setUp() throws Exception {
55         MockitoAnnotations.initMocks(this);
56
57         oper = new BasicBidirectionalTopicOperation(ACTOR, OPERATION);
58         oper.setUp();
59     }
60
61     @Test
62     public void testBasicBidirectionalTopicOperation() {
63         oper = new BasicBidirectionalTopicOperation();
64         assertEquals(BasicHttpOperation.DEFAULT_ACTOR, oper.actorName);
65         assertEquals(BasicHttpOperation.DEFAULT_OPERATION, oper.operationName);
66     }
67
68     @Test
69     public void testBasicBidirectionalTopicOperationStringString() {
70         assertEquals(ACTOR, oper.actorName);
71         assertEquals(OPERATION, oper.operationName);
72     }
73
74     @Test
75     public void testSetUp() {
76         assertNotNull(oper.topicParams);
77         assertNotNull(oper.context);
78         assertNotNull(oper.outcome);
79         assertNotNull(oper.executor);
80         assertTrue(oper.operator.isAlive());
81     }
82
83     @Test
84     public void testMakeContext() {
85         oper.makeContext();
86
87         assertTrue(oper.enrichment.isEmpty());
88
89         assertSame(BasicBidirectionalTopicOperation.REQ_ID, oper.event.getRequestId());
90         assertSame(oper.enrichment, oper.event.getAai());
91
92         assertSame(oper.event, oper.context.getEvent());
93
94         assertSame(oper.context, oper.params.getContext());
95         assertSame(oper.service, oper.params.getActorService());
96         assertSame(oper.executor, oper.params.getExecutor());
97         assertEquals(ACTOR, oper.params.getActor());
98         assertEquals(OPERATION, oper.params.getOperation());
99         assertEquals(BasicBidirectionalTopicOperation.TARGET_ENTITY, oper.params.getTargetEntity());
100     }
101
102     @Test
103     public void testMakePayload() {
104         assertNull(oper.makePayload());
105     }
106
107     @Test
108     public void testInitOperator() {
109         oper.initOperator();
110
111         assertTrue(oper.operator.isAlive());
112         assertEquals(ACTOR + "." + OPERATION, oper.operator.getFullName());
113         assertEquals(ACTOR, oper.operator.getActorName());
114         assertEquals(OPERATION, oper.operator.getName());
115         assertSame(oper.topicHandler, oper.operator.getTopicHandler());
116         assertSame(oper.forwarder, oper.operator.getForwarder());
117         assertSame(oper.topicParams, oper.operator.getParams());
118     }
119
120     @Test
121     public void testMakeEnrichment() {
122         assertTrue(oper.makeEnrichment().isEmpty());
123     }
124
125     @Test
126     public void testProvideResponse() {
127         String response = "{\"input\": 10}";
128
129         oper.provideResponse(listener, response);
130
131         ArgumentCaptor<StandardCoderObject> scoCaptor = ArgumentCaptor.forClass(StandardCoderObject.class);
132         verify(listener).accept(eq(response), scoCaptor.capture());
133
134         assertEquals("10", scoCaptor.getValue().getString("input"));
135
136         // try with an invalid response
137         assertThatIllegalArgumentException().isThrownBy(() -> oper.provideResponse(listener, "{invalid json"))
138                         .withMessage("response is not a Map");
139     }
140 }