Revise groupIds in POM files; no functional changes
[portal/ric-dashboard.git] / dashboard / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / config / E2ManagerMockConfiguration.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
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 package org.oransc.ric.portal.dashboard.config;
21
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.doAnswer;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26
27 import java.lang.invoke.MethodHandles;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.oransc.ricplt.e2mgr.client.api.HealthCheckApi;
34 import org.oransc.ricplt.e2mgr.client.api.NodebApi;
35 import org.oransc.ricplt.e2mgr.client.invoker.ApiClient;
36 import org.oransc.ricplt.e2mgr.client.model.GetNodebResponse;
37 import org.oransc.ricplt.e2mgr.client.model.NodebIdentity;
38 import org.oransc.ricplt.e2mgr.client.model.NodebIdentityGlobalNbId;
39 import org.oransc.ricplt.e2mgr.client.model.ResetRequest;
40 import org.oransc.ricplt.e2mgr.client.model.SetupRequest;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.beans.factory.annotation.Value;
45 import org.springframework.context.annotation.Bean;
46 import org.springframework.context.annotation.Configuration;
47 import org.springframework.context.annotation.Profile;
48 import org.springframework.http.HttpStatus;
49
50 /**
51  * Creates a mock implementation of the E2 Manager client API.
52  */
53 @Configuration
54 @Profile("test")
55 public class E2ManagerMockConfiguration {
56
57         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
58
59         public static final String RAN_NAME_1 = "Connected-RAN";
60         public static final String RAN_NAME_2 = "Unknown-RAN";
61
62         // Simulate remote method delay for UI testing
63         private int delayMs;
64
65         @Autowired
66         public E2ManagerMockConfiguration(@Value("${mock.config.delay:0}") int delayMs) {
67                 logger.debug("ctor: configured with delay {}", delayMs);
68                 this.delayMs = delayMs;
69         }
70
71         private ApiClient apiClient() {
72                 ApiClient mockClient = mock(ApiClient.class);
73                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
74                 return mockClient;
75         }
76
77         private HealthCheckApi healthCheckApi() {
78                 ApiClient apiClient = apiClient();
79                 HealthCheckApi mockApi = mock(HealthCheckApi.class);
80                 when(mockApi.getApiClient()).thenReturn(apiClient);
81                 doAnswer(i -> null).when(mockApi).healthGet();
82                 return mockApi;
83         }
84
85         /**
86          * Builds a mock NodebApi object.
87          * 
88          * @param instanceKey
89          *                        RIC instance
90          * @return Object that returns instance-specific results
91          */
92         private NodebApi nodebApi(String instanceKey) {
93
94                 final NodebIdentityGlobalNbId globalNbId = new NodebIdentityGlobalNbId().nbId("mockNbId-" + instanceKey)
95                                 .plmnId("mockPlmId");
96                 final List<NodebIdentity> nodebIdList = new ArrayList<>();
97                 final Map<String, GetNodebResponse> nodebResponseMap = new HashMap<>();
98                 // Complete entry
99                 nodebIdList.add(new NodebIdentity().inventoryName(RAN_NAME_1).globalNbId(globalNbId));
100                 nodebResponseMap.put(RAN_NAME_1, new GetNodebResponse().connectionStatus("CONNECTED").failureType("")
101                                 .ip("127.0.0.1").nodeType("mockNodeType").port(123).ranName(RAN_NAME_1));
102                 // Partial entry
103                 // [{"nodebIdentity":{"globalNbId":null,"inventoryName":"AAAA123456"},
104                 // "nodebStatus":{"connectionStatus":"CONNECTING","enb":null,"failureType":null,
105                 // "globalNbId":null,"gnb":null,"ip":"10.2.0.6","nodeType":null,"port":36444,
106                 // "ranName":"AAAA123456","setupFailure":null}}]
107                 nodebIdList.add(new NodebIdentity().inventoryName(RAN_NAME_2));
108                 nodebResponseMap.put(RAN_NAME_2,
109                                 new GetNodebResponse().connectionStatus("CONNECTING").ip("127.0.0.2").port(456).ranName(RAN_NAME_2));
110
111                 ApiClient apiClient = apiClient();
112                 NodebApi mockApi = mock(NodebApi.class);
113                 when(mockApi.getApiClient()).thenReturn(apiClient);
114                 doAnswer(inv -> {
115                         if (delayMs > 0) {
116                                 logger.debug("nodebShutdownPut sleeping {}", delayMs);
117                                 Thread.sleep(delayMs);
118                         }
119                         nodebIdList.clear();
120                         return null;
121                 }).when(mockApi).nodebShutdownPut();
122                 doAnswer(inv -> {
123                         if (delayMs > 0) {
124                                 logger.debug("reset sleeping {}", delayMs);
125                                 Thread.sleep(delayMs);
126                         }
127                         return null;
128                 }).when(mockApi).reset(any(String.class), any(ResetRequest.class));
129                 doAnswer(inv -> {
130                         if (delayMs > 0) {
131                                 logger.debug("getNb sleeping {}", delayMs);
132                                 Thread.sleep(delayMs);
133                         }
134                         String invName = inv.<String>getArgument(0);
135                         return nodebResponseMap.get(invName);
136                 }).when(mockApi).getNb(any(String.class));
137                 doAnswer(inv -> {
138                         if (delayMs > 0) {
139                                 logger.debug("getNodebIdList sleeping {}", delayMs);
140                                 Thread.sleep(delayMs);
141                         }
142                         return nodebIdList;
143                 }).when(mockApi).getNodebIdList();
144                 doAnswer(inv -> {
145                         if (delayMs > 0) {
146                                 logger.debug("endcSetup sleeping {}", delayMs);
147                                 Thread.sleep(delayMs);
148                         }
149                         SetupRequest sr = inv.<SetupRequest>getArgument(0);
150                         nodebIdList.add(new NodebIdentity().inventoryName(sr.getRanName()).globalNbId(globalNbId));
151                         nodebResponseMap.put(sr.getRanName(),
152                                         new GetNodebResponse().connectionStatus("mockConnectionStatus").failureType("mockFailureType")
153                                                         .ip(sr.getRanIp()).nodeType("ENDC").port(sr.getRanPort()).ranName(sr.getRanName()));
154                         return null;
155                 }).when(mockApi).endcSetup(any(SetupRequest.class));
156                 doAnswer(inv -> {
157                         if (delayMs > 0) {
158                                 logger.debug("x2Setup sleeping {}", delayMs);
159                                 Thread.sleep(delayMs);
160                         }
161                         SetupRequest sr = inv.<SetupRequest>getArgument(0);
162                         nodebIdList.add(new NodebIdentity().inventoryName(sr.getRanName()).globalNbId(globalNbId));
163                         nodebResponseMap.put(sr.getRanName(),
164                                         new GetNodebResponse().connectionStatus("mockConnectionStatus").failureType("mockFailureType")
165                                                         .ip(sr.getRanIp()).nodeType("X2").port(sr.getRanPort()).ranName(sr.getRanName()));
166                         return null;
167                 }).when(mockApi).x2Setup(any(SetupRequest.class));
168                 return mockApi;
169         }
170
171         @Bean
172         // Must use the same name as the non-mock configuration
173         public E2ManagerApiBuilder e2ManagerApiBuilder() {
174                 final E2ManagerApiBuilder mockBuilder = mock(E2ManagerApiBuilder.class);
175                 final HealthCheckApi mockHealthCheckApi = healthCheckApi();
176                 when(mockBuilder.getHealthCheckApi(any(String.class))).thenReturn(mockHealthCheckApi);
177                 for (final String key : RICInstanceMockConfiguration.INSTANCE_KEYS) {
178                         final NodebApi mockNodebApi = nodebApi(key);
179                         when(mockBuilder.getNodebApi(key)).thenReturn(mockNodebApi);
180                 }
181                 return mockBuilder;
182         }
183
184 }