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