c0351d12c28844cba82c7ecf017dba5dc211fb8d
[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  * Modifications Copyright (C) 2023 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.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertSame;
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 import org.junit.runner.RunWith;
35 import org.mockito.junit.MockitoJUnitRunner;
36
37 @RunWith(MockitoJUnitRunner.class)
38 public class BasicHttpOperationTest {
39     private static final String ACTOR = "my-actor";
40     private static final String OPERATION = "my-operation";
41
42     private BasicHttpOperation oper;
43
44
45     @Before
46     public void setUp() throws Exception {
47         oper = new BasicHttpOperation(ACTOR, OPERATION);
48         oper.setUpBasic();
49     }
50
51     @Test
52     public void testBasicHttpOperation() {
53         oper = new BasicHttpOperation();
54         assertEquals(BasicOperation.DEFAULT_ACTOR, oper.actorName);
55         assertEquals(BasicOperation.DEFAULT_OPERATION, oper.operationName);
56     }
57
58     @Test
59     public void testBasicHttpOperationStringString() {
60         assertEquals(ACTOR, oper.actorName);
61         assertEquals(OPERATION, oper.operationName);
62     }
63
64     @Test
65     public void testSetUp() throws Exception {
66         assertNotNull(oper.client);
67         assertSame(oper.client, oper.factory.get(BasicHttpOperation.MY_CLIENT));
68         assertEquals(200, oper.rawResponse.getStatus());
69         assertNotNull(oper.future);
70         assertEquals(BasicHttpOperation.BASE_URI, oper.client.getBaseUrl());
71         assertNotNull(oper.outcome);
72         assertNotNull(oper.executor);
73     }
74
75     @Test
76     public void testInitOperator() throws Exception {
77         oper.initConfig();
78
79         assertSame(oper.client, oper.config.getClient());
80         assertEquals(BasicHttpOperation.PATH, oper.config.getPath());
81     }
82
83     @Test
84     public void testProvideResponse() throws Exception {
85         InvocationCallback<Response> cb = new InvocationCallback<>() {
86             @Override
87             public void completed(Response response) {
88                 // do nothing
89             }
90
91             @Override
92             public void failed(Throwable throwable) {
93                 // do nothing
94             }
95         };
96
97
98         when(oper.client.get(any(), any(), any())).thenAnswer(oper.provideResponse(oper.rawResponse));
99
100         assertSame(oper.rawResponse, oper.client.get(cb, null, null).get());
101     }
102 }