Removed MSB Invocation During AAI Calling
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / aai / AaiQueryMdonsTest.java
1 /**
2  * Copyright 2020 - 2023 Fujitsu Limited, 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 jakarta.ws.rs.client.Entity;
20 import jakarta.ws.rs.core.MultivaluedHashMap;
21 import jakarta.ws.rs.core.MultivaluedMap;
22 import org.easymock.EasyMock;
23 import org.junit.*;
24 import org.junit.rules.ExpectedException;
25 import org.junit.runner.RunWith;
26 import org.onap.holmes.common.aai.config.AaiConfig;
27 import org.onap.holmes.common.config.MicroServiceConfig;
28 import org.onap.holmes.common.utils.JerseyClient;
29 import org.powermock.api.easymock.PowerMock;
30 import org.powermock.core.classloader.annotations.PowerMockIgnore;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32 import org.powermock.modules.junit4.PowerMockRunner;
33 import org.powermock.reflect.Whitebox;
34
35 import java.io.*;
36 import java.util.HashMap;
37 import java.util.Map;
38
39 import static org.easymock.EasyMock.anyObject;
40 import static org.junit.Assert.assertEquals;
41 import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR;
42
43 @RunWith(PowerMockRunner.class)
44 @PowerMockIgnore("javax.net.ssl.*")
45 @PrepareForTest({AaiQueryMdons.class, MicroServiceConfig.class, JerseyClient.class})
46 public class AaiQueryMdonsTest {
47
48     @Rule
49     public ExpectedException thrown = ExpectedException.none();
50
51     private static AaiQueryMdons aaiMdonsQuery = AaiQueryMdons.newInstance();
52     private static MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
53
54     private static JsonObject data;
55
56     private static final String AAI_ADDR = "https://aai.onap:443/aai/v19/";
57
58     @BeforeClass
59     public static void beforeClass() {
60         System.setProperty(MSB_ADDR, "127.0.0.1:80");
61         System.setProperty("ENABLE_ENCRYPT", "true");
62         System.setProperty(MicroServiceConfig.AAI_ADDR, "aai");
63         System.setProperty(MicroServiceConfig.BASE_URL, "onap");
64
65         File file = new File(AaiQueryMdonsTest.class.getClassLoader().getResource("./aai-mdons.json").getFile());
66         BufferedReader reader = null;
67         try {
68             reader = new BufferedReader(new FileReader(file));
69             StringBuilder sb = new StringBuilder();
70             reader.lines().forEach(l -> sb.append(l));
71             data = JsonParser.parseString(sb.toString()).getAsJsonObject();
72         } catch (FileNotFoundException e) {
73             // Do nothing
74         } catch (IOException e) {
75             // Do nothing
76         } finally {
77             if (reader != null) {
78                 try {
79                     reader.close();
80                 } catch (IOException e) {
81                     // Do nothing
82                 }
83             }
84         }
85
86         headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
87         headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
88         headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
89         headers.add("Accept", "application/json");
90         headers.add("Content-Type", "application/json");
91
92     }
93
94     @Before
95     public void before() {
96         Whitebox.setInternalState(aaiMdonsQuery, "headers", headers);
97
98     }
99
100     @After
101     public void after() {
102         PowerMock.resetAll();
103     }
104
105     @Test
106     public void testProcessPnf() throws Exception {
107         String pnfUrl = AAI_ADDR + "network/pnfs/pnf/test1?depth=all";
108         String domainService = AAI_ADDR
109                 + "business/customers/customer/Orange/service-subscriptions/service-subscription/MDONS_OTN/service-instances/service-instance/789";
110         PowerMock.resetAll();
111
112         aaiMdonsQuery = PowerMock.createPartialMock(AaiQueryMdons.class, "getResponse");
113         PowerMock.expectPrivate(aaiMdonsQuery, "getResponse", pnfUrl).andReturn(data.get("pnf-depth-all").toString());
114         PowerMock.expectPrivate(aaiMdonsQuery, "getResponse", domainService)
115                 .andReturn(data.get("get-domain-service").toString());
116
117         PowerMock.replayAll();
118         Map<String, String> accessMap = aaiMdonsQuery.processPnf("test1");
119         PowerMock.verifyAll();
120         Map<String, String> verifyMap = new HashMap<>();
121         verifyMap.put("123", "access-service");
122         assertEquals(accessMap, verifyMap);
123     }
124
125     @Test
126     public void testGetPnfName() throws Exception {
127         String pnfUrl = AAI_ADDR + "network/pnfs?pnf-id=test1-id";
128
129         Whitebox.setInternalState(aaiMdonsQuery, "headers", headers);
130
131         aaiMdonsQuery = PowerMock.createPartialMock(AaiQueryMdons.class, "getResponse");
132         PowerMock.expectPrivate(aaiMdonsQuery, "getResponse", pnfUrl).andReturn(data.get("get-pnf-by-id").toString());
133         PowerMock.replayAll();
134         String pnfName = Whitebox.invokeMethod(aaiMdonsQuery, "getPnfNameFromPnfId", "test1-id");
135
136         PowerMock.verifyAll();
137         assertEquals(pnfName, "test1");
138
139     }
140
141     @Test
142     public void testUpdatelinksAccess() throws Exception {
143         Map<String, String> accessMap = new HashMap<>();
144         accessMap.put("123", "access-service");
145         String accessService = AAI_ADDR
146                 + "business/customers/customer/Orange/service-subscriptions/service-subscription/MDONS_OTN/service-instances/service-instance/123";
147         String linkUrl = AAI_ADDR + "network/logical-links/logical-link/link1";
148
149         String response =
150                 "{\"link-name\":\"link1\",\"in-maint\":false,\"link-type\":\"inter-domain\",\"available-capacity\":\"ODU2\",\"resource-version\":\"1584338211407\",\"operational-status\":\"down\"}";
151
152         aaiMdonsQuery = PowerMock.createPartialMock(AaiQueryMdons.class, "getResponse");
153
154         JerseyClient mockedClient = PowerMock.createMock(JerseyClient.class);
155         PowerMock.expectNew(JerseyClient.class).andReturn(mockedClient);
156         EasyMock.expect(mockedClient.headers(anyObject(Map.class))).andReturn(mockedClient);
157         EasyMock.expect(mockedClient.put(anyObject(String.class), anyObject(Entity.class))).andReturn("");
158
159         PowerMock.expectPrivate(aaiMdonsQuery, "getResponse", accessService)
160                 .andReturn(data.get("get-access-service").toString());
161         PowerMock.expectPrivate(aaiMdonsQuery, "getResponse", linkUrl).andReturn(data.get("get-inter-link").toString());
162
163         PowerMock.replayAll();
164         Whitebox.invokeMethod(aaiMdonsQuery, "updateLinksForAccessService", accessMap);
165
166         PowerMock.verifyAll();
167
168     }
169
170 }