Merge "Allow storing database password in environment variables"
[policy/models.git] / models-interactions / model-actors / actor.test / src / test / java / org / onap / policy / controlloop / actor / test / BasicHttpOperationTest.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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.when;
29
30 import javax.ws.rs.client.InvocationCallback;
31 import javax.ws.rs.core.Response;
32 import org.junit.Before;
33 import org.junit.Test;
34
35 public class BasicHttpOperationTest {
36     private static final String ACTOR = "my-actor";
37     private static final String OPERATION = "my-operation";
38
39     private BasicHttpOperation<String> oper;
40
41
42     @Before
43     public void setUp() throws Exception {
44         oper = new BasicHttpOperation<>(ACTOR, OPERATION);
45         oper.setUp();
46     }
47
48     @Test
49     public void testBasicHttpOperation() {
50         oper = new BasicHttpOperation<>();
51         assertEquals(BasicHttpOperation.DEFAULT_ACTOR, oper.actorName);
52         assertEquals(BasicHttpOperation.DEFAULT_OPERATION, oper.operationName);
53     }
54
55     @Test
56     public void testBasicHttpOperationStringString() {
57         assertEquals(ACTOR, oper.actorName);
58         assertEquals(OPERATION, oper.operationName);
59     }
60
61     @Test
62     public void testSetUp() throws Exception {
63         assertNotNull(oper.client);
64         assertSame(oper.client, oper.factory.get(BasicHttpOperation.MY_CLIENT));
65         assertEquals(200, oper.rawResponse.getStatus());
66         assertNotNull(oper.future);
67         assertEquals(BasicHttpOperation.BASE_URI, oper.client.getBaseUrl());
68         assertNotNull(oper.context);
69         assertNotNull(oper.outcome);
70         assertNotNull(oper.executor);
71         assertTrue(oper.operator.isAlive());
72     }
73
74     @Test
75     public void testMakeContext() {
76         oper.makeContext();
77
78         assertTrue(oper.enrichment.isEmpty());
79
80         assertSame(BasicHttpOperation.REQ_ID, oper.event.getRequestId());
81         assertSame(oper.enrichment, oper.event.getAai());
82
83         assertSame(oper.event, oper.context.getEvent());
84
85         assertSame(oper.context, oper.params.getContext());
86         assertSame(oper.service, oper.params.getActorService());
87         assertSame(oper.executor, oper.params.getExecutor());
88         assertEquals(ACTOR, oper.params.getActor());
89         assertEquals(OPERATION, oper.params.getOperation());
90         assertEquals(BasicHttpOperation.TARGET_ENTITY, oper.params.getTargetEntity());
91     }
92
93     @Test
94     public void testInitOperator() throws Exception {
95         oper.initOperator();
96
97         assertTrue(oper.operator.isAlive());
98         assertEquals(ACTOR + "." + OPERATION, oper.operator.getFullName());
99         assertEquals(ACTOR, oper.operator.getActorName());
100         assertEquals(OPERATION, oper.operator.getName());
101         assertSame(oper.client, oper.operator.getClient());
102         assertEquals(BasicHttpOperation.PATH, oper.operator.getPath());
103     }
104
105     @Test
106     public void testMakeEnrichment() {
107         assertTrue(oper.makeEnrichment().isEmpty());
108     }
109
110     @Test
111     public void testProvideResponse() throws Exception {
112         InvocationCallback<Response> cb = new InvocationCallback<>() {
113             @Override
114             public void completed(Response response) {
115                 // do nothing
116             }
117
118             @Override
119             public void failed(Throwable throwable) {
120                 // do nothing
121             }
122         };
123
124
125         when(oper.client.get(any(), any(), any())).thenAnswer(oper.provideResponse(oper.rawResponse));
126
127         assertSame(oper.rawResponse, oper.client.get(cb, null, null).get());
128     }
129 }