Removed redundant env vars
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / aai / AaiQuery4CcvpnTest.java
1 /**
2  * Copyright 2018-2021 ZTE Corporation.
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  * <p>
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * <p>
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14
15 package org.onap.holmes.common.aai;
16
17 import com.google.gson.JsonObject;
18 import com.google.gson.JsonParser;
19 import org.easymock.EasyMock;
20 import org.junit.*;
21 import org.junit.rules.ExpectedException;
22 import org.junit.runner.RunWith;
23 import org.onap.holmes.common.aai.config.AaiConfig;
24 import org.onap.holmes.common.exception.CorrelationException;
25 import org.onap.holmes.common.utils.JerseyClient;
26 import org.powermock.api.easymock.PowerMock;
27 import org.powermock.core.classloader.annotations.PrepareForTest;
28 import org.powermock.modules.junit4.PowerMockRunner;
29 import org.powermock.reflect.Whitebox;
30
31 import java.io.*;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import static org.easymock.EasyMock.anyObject;
36 import static org.easymock.EasyMock.anyString;
37 import static org.hamcrest.CoreMatchers.equalTo;
38 import static org.junit.Assert.assertThat;
39
40 @RunWith(PowerMockRunner.class)
41 @PrepareForTest(JerseyClient.class)
42 public class AaiQuery4CcvpnTest {
43
44     @Rule
45     public ExpectedException thrown = ExpectedException.none();
46
47     private static JsonObject data;
48
49     private static AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();
50
51     private static Map<String, Object> headers = new HashMap<>();
52
53     private static JerseyClient client;
54
55     @BeforeClass
56     static public void beforeClass() {
57         File file = new File(AaiQuery4CcvpnTest.class.getClassLoader().getResource("./ccvpn.data.json").getFile());
58         BufferedReader reader = null;
59         try {
60             reader = new BufferedReader(new FileReader(file));
61             StringBuilder sb = new StringBuilder();
62             reader.lines().forEach(l -> sb.append(l));
63             data = JsonParser.parseString(sb.toString()).getAsJsonObject();
64         } catch (FileNotFoundException e) {
65             // Do nothing
66         } catch (IOException e) {
67             // Do nothing
68         } finally {
69             if (reader != null) {
70                 try {
71                     reader.close();
72                 } catch (IOException e) {
73                     // Do nothing
74                 }
75             }
76         }
77
78         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
79         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
80         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
81         headers.put("Accept", "application/json");
82         headers.put("Content-Type", "application/json");
83         Whitebox.setInternalState(aai, "headers", headers);
84     }
85
86     @Before
87     public void before() {
88         PowerMock.mockStatic(JerseyClient.class);
89         client = PowerMock.createMock(JerseyClient.class);
90         EasyMock.expect(JerseyClient.newInstance()).andReturn(client).anyTimes();
91     }
92
93     @After
94     public void after() {
95         PowerMock.resetAll();
96     }
97
98     @Test
99     public void test_getPath() throws Exception {
100         String path = "/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}";
101         String ret = Whitebox.invokeMethod(aai, "getPath", path);
102         assertThat(ret, equalTo("/api/aai-business/v14/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}"));
103     }
104
105     @Test
106     public void test_getLogicLink() {
107         mockGetMethod(data.get("logic-link").toString());
108
109         PowerMock.replayAll();
110
111         String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
112
113         PowerMock.verifyAll();
114
115         assertThat(linkId, equalTo("logic-link-1"));
116
117     }
118
119
120     @Test
121     public void test_getServiceInstance() {
122         mockGetMethod(data.get("vpn-binding").toString());
123         mockGetMethod(data.get("connectivity").toString());
124         mockGetMethod(data.get("service-instance-by-connectivity").toString());
125         mockGetMethod(data.get("service-instances-by-service-type").toString());
126
127         PowerMock.replayAll();
128
129         JsonObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN");
130
131         PowerMock.verifyAll();
132
133         assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 1"));
134         assertThat(instance.get("globalSubscriberId").getAsString(), equalTo("e151059a-d924-4629-845f-264db19e50b4"));
135         assertThat(instance.get("serviceType").getAsString(), equalTo("volte"));
136     }
137
138     @Test
139     public void test_updateTerminalPointStatus() throws CorrelationException {
140         mockGetMethod(data.toString());
141         mockPutMethod("ok");
142
143         PowerMock.replayAll();
144
145         aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
146
147         PowerMock.verifyAll();
148     }
149
150
151     @Test
152     public void test_updateLogicLinkStatus() {
153         mockGetMethod(data.toString());
154         mockPutMethod("ok");
155
156         PowerMock.replayAll();
157
158         aai.updateLogicLinkStatus("link-1", new HashMap<>());
159
160         PowerMock.verifyAll();
161     }
162
163     private void mockGetMethod(String ret) {
164         EasyMock.expect(client.path(anyString())).andReturn(client);
165         EasyMock.expect(client.headers(anyObject())).andReturn(client);
166         EasyMock.expect(client.get(anyString())).andReturn(ret);
167     }
168
169     private void mockPutMethod(String ok) {
170         EasyMock.expect(client.path(anyString())).andReturn(client);
171         EasyMock.expect(client.headers(anyObject())).andReturn(client);
172         EasyMock.expect(client.put(anyString(), anyObject())).andReturn(ok);
173     }
174 }