1cde4c86084c2ea081e1cbd416ea2e077896834f
[policy/models.git] / models-interactions / model-actors / actor.aai / src / test / java / org / onap / policy / controlloop / actor / aai / AaiGetOperationTest.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.aai;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertSame;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import java.util.Map;
32 import javax.ws.rs.client.Invocation.Builder;
33 import javax.ws.rs.client.WebTarget;
34 import org.junit.AfterClass;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.policy.aai.AaiConstants;
39
40 public class AaiGetOperationTest extends BasicAaiOperation {
41     private static final String MY_NAME = "my-operation-name";
42     private static final String PARAM_NAME = "my-param";
43     private static final String PARAM_VALUE = "my-value";
44
45     private AaiGetOperation oper;
46
47     public AaiGetOperationTest() {
48         super(AaiConstants.ACTOR_NAME, MY_NAME);
49     }
50
51     @BeforeClass
52     public static void setUpBeforeClass() throws Exception {
53         initBeforeClass();
54     }
55
56     @AfterClass
57     public static void tearDownAfterClass() {
58         destroyAfterClass();
59     }
60
61     /**
62      * Sets up.
63      */
64     @Before
65     public void setUp() throws Exception {
66         super.setUpBasic();
67         oper = new AaiGetOperation(params, config);
68     }
69
70     @Test
71     public void testConstructor() {
72         assertEquals(AaiConstants.ACTOR_NAME, oper.getActorName());
73         assertEquals(MY_NAME, oper.getName());
74     }
75
76     @Test
77     public void testGenerateSubRequestId() {
78         oper.generateSubRequestId(3);
79         assertEquals("3", oper.getSubRequestId());
80     }
81
82     @Test
83     public void testAddQuery() {
84         WebTarget web = mock(WebTarget.class);
85         when(web.queryParam(any(), any())).thenReturn(web);
86
87         StringBuilder bldr = new StringBuilder();
88
89         assertSame(web, oper.addQuery(web, bldr, ",", PARAM_NAME, PARAM_VALUE));
90         assertEquals(",my-param=my-value", bldr.toString());
91     }
92
93     @Test
94     public void testAddHeaders() {
95         Builder bldr = mock(Builder.class);
96         oper.addHeaders(bldr, Map.of("hdrA", "valA", "hdrB", "valB"));
97
98         verify(bldr, times(2)).header(any(), any());
99         verify(bldr).header("hdrA", "valA");
100         verify(bldr).header("hdrB", "valB");
101     }
102
103     @Test
104     public void testGetRetry() {
105         // use default if null retry
106         assertEquals(AaiGetOperation.DEFAULT_RETRY, oper.getRetry(null));
107
108         // otherwise, use specified value
109         assertEquals(0, oper.getRetry(0));
110         assertEquals(10, oper.getRetry(10));
111     }
112
113     @Test
114     public void testMakeHeaders() {
115         verifyHeaders(oper.makeHeaders());
116     }
117 }