Validate request content of various actors
[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.assertSame;
27 import static org.junit.Assert.assertTrue;
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.Before;
33 import org.junit.Test;
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.policy.common.utils.coder.StandardCoderObject;
38
39 public class BasicBidirectionalTopicOperationTest {
40     private static final String ACTOR = "my-actor";
41     private static final String OPERATION = "my-operation";
42
43     @Mock
44     private BiConsumer<String, StandardCoderObject> listener;
45
46     private BasicBidirectionalTopicOperation oper;
47
48
49     /**
50      * Sets up.
51      */
52     @Before
53     public void setUp() throws Exception {
54         MockitoAnnotations.initMocks(this);
55
56         oper = new BasicBidirectionalTopicOperation(ACTOR, OPERATION);
57         oper.setUp();
58     }
59
60     @Test
61     public void testBasicBidirectionalTopicOperation() {
62         oper = new BasicBidirectionalTopicOperation();
63         assertEquals(BasicHttpOperation.DEFAULT_ACTOR, oper.actorName);
64         assertEquals(BasicHttpOperation.DEFAULT_OPERATION, oper.operationName);
65     }
66
67     @Test
68     public void testBasicBidirectionalTopicOperationStringString() {
69         assertEquals(ACTOR, oper.actorName);
70         assertEquals(OPERATION, oper.operationName);
71     }
72
73     @Test
74     public void testSetUp() {
75         assertNotNull(oper.topicParams);
76         assertNotNull(oper.context);
77         assertNotNull(oper.outcome);
78         assertNotNull(oper.executor);
79         assertTrue(oper.operator.isAlive());
80     }
81
82     @Test
83     public void testInitOperator() {
84         oper.initOperator();
85
86         assertTrue(oper.operator.isAlive());
87         assertEquals(ACTOR + "." + OPERATION, oper.operator.getFullName());
88         assertEquals(ACTOR, oper.operator.getActorName());
89         assertEquals(OPERATION, oper.operator.getName());
90         assertSame(oper.topicHandler, oper.operator.getTopicHandler());
91         assertSame(oper.forwarder, oper.operator.getForwarder());
92         assertSame(oper.topicParams, oper.operator.getParams());
93     }
94
95     @Test
96     public void testProvideResponse() {
97         String response = "{\"input\": 10}";
98
99         oper.provideResponse(listener, response);
100
101         ArgumentCaptor<StandardCoderObject> scoCaptor = ArgumentCaptor.forClass(StandardCoderObject.class);
102         verify(listener).accept(eq(response), scoCaptor.capture());
103
104         assertEquals("10", scoCaptor.getValue().getString("input"));
105
106         // try with an invalid response
107         assertThatIllegalArgumentException().isThrownBy(() -> oper.provideResponse(listener, "{invalid json"))
108                         .withMessage("response is not a Map");
109     }
110 }