Released version 1.4.5
[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 import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR;
40
41
42 @RunWith(PowerMockRunner.class)
43 @PrepareForTest(JerseyClient.class)
44 public class AaiQuery4CcvpnTest {
45
46     @Rule
47     public ExpectedException thrown = ExpectedException.none();
48
49     private static JsonObject data;
50
51     private static AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();
52
53     private static Map<String, Object> headers = new HashMap<>();
54
55     private static JerseyClient client;
56
57     @BeforeClass
58     static public void beforeClass() {
59         System.setProperty(MSB_ADDR, "127.0.0.1:80");
60
61         File file = new File(AaiQuery4CcvpnTest.class.getClassLoader().getResource("./ccvpn.data.json").getFile());
62         BufferedReader reader = null;
63         try {
64             reader = new BufferedReader(new FileReader(file));
65             StringBuilder sb = new StringBuilder();
66             reader.lines().forEach(l -> sb.append(l));
67             data = JsonParser.parseString(sb.toString()).getAsJsonObject();
68         } catch (FileNotFoundException e) {
69             // Do nothing
70         } catch (IOException e) {
71             // Do nothing
72         } finally {
73             if (reader != null) {
74                 try {
75                     reader.close();
76                 } catch (IOException e) {
77                     // Do nothing
78                 }
79             }
80         }
81
82         headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
83         headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
84         headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
85         headers.put("Accept", "application/json");
86         headers.put("Content-Type", "application/json");
87         Whitebox.setInternalState(aai, "headers", headers);
88     }
89
90     @Before
91     public void before() {
92         PowerMock.mockStatic(JerseyClient.class);
93         client = PowerMock.createMock(JerseyClient.class);
94         EasyMock.expect(JerseyClient.newInstance()).andReturn(client).anyTimes();
95     }
96
97     @After
98     public void after() {
99         PowerMock.resetAll();
100     }
101
102     @Test
103     public void test_getPath() throws Exception {
104         String path = "/aai/v14/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}";
105         String ret = Whitebox.invokeMethod(aai, "getPath", path);
106         assertThat(ret, equalTo("/api/aai-business/v14/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances?service-instance-id={servId}"));
107     }
108
109     @Test
110     public void test_getLogicLink() {
111         mockGetMethod(data.get("logic-link").toString());
112
113         PowerMock.replayAll();
114
115         String linkId = aai.getLogicLink("network-1", "pnf-1", "interface-1", "DOWN");
116
117         PowerMock.verifyAll();
118
119         assertThat(linkId, equalTo("logic-link-1"));
120
121     }
122
123
124     @Test
125     public void test_getServiceInstance() {
126         mockGetMethod(data.get("vpn-binding").toString());
127         mockGetMethod(data.get("connectivity").toString());
128         mockGetMethod(data.get("service-instance-by-connectivity").toString());
129         mockGetMethod(data.get("service-instances-by-service-type").toString());
130
131         PowerMock.replayAll();
132
133         JsonObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN");
134
135         PowerMock.verifyAll();
136
137         assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 1"));
138         assertThat(instance.get("globalSubscriberId").getAsString(), equalTo("e151059a-d924-4629-845f-264db19e50b4"));
139         assertThat(instance.get("serviceType").getAsString(), equalTo("volte"));
140     }
141
142     @Test
143     public void test_updateTerminalPointStatus() throws CorrelationException {
144         mockGetMethod(data.toString());
145         mockPutMethod("ok");
146
147         PowerMock.replayAll();
148
149         aai.updateTerminalPointStatus("network-1", "pnf-1", "if-1", new HashMap<>());
150
151         PowerMock.verifyAll();
152     }
153
154
155     @Test
156     public void test_updateLogicLinkStatus() {
157         mockGetMethod(data.toString());
158         mockPutMethod("ok");
159
160         PowerMock.replayAll();
161
162         aai.updateLogicLinkStatus("link-1", new HashMap<>());
163
164         PowerMock.verifyAll();
165     }
166
167     private void mockGetMethod(String ret) {
168         EasyMock.expect(client.path(anyString())).andReturn(client);
169         EasyMock.expect(client.headers(anyObject())).andReturn(client);
170         EasyMock.expect(client.get(anyString())).andReturn(ret);
171     }
172
173     private void mockPutMethod(String ok) {
174         EasyMock.expect(client.path(anyString())).andReturn(client);
175         EasyMock.expect(client.headers(anyObject())).andReturn(client);
176         EasyMock.expect(client.put(anyString(), anyObject())).andReturn(ok);
177     }
178 }