2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.ric.portal.dashboard.config;
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;
27 import java.lang.invoke.MethodHandles;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
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.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
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;
50 * Creates a mock implementation of the E2 Manager client API.
54 public class E2ManagerMockConfiguration {
56 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
58 public static final String RAN_NAME_1 = "Connected-RAN";
59 public static final String RAN_NAME_2 = "Unknown-RAN";
61 // Simulate remote method delay for UI testing
65 public E2ManagerMockConfiguration(@Value("${mock.config.delay:0}") int delayMs) {
66 logger.debug("ctor: configured with delay {}", delayMs);
67 this.delayMs = delayMs;
70 private ApiClient apiClient() {
71 ApiClient mockClient = mock(ApiClient.class);
72 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
76 private HealthCheckApi healthCheckApi() {
77 ApiClient apiClient = apiClient();
78 HealthCheckApi mockApi = mock(HealthCheckApi.class);
79 when(mockApi.getApiClient()).thenReturn(apiClient);
80 doAnswer(i -> null).when(mockApi).healthGet();
85 * Builds a mock NodebApi object.
89 * @return Object that returns instance-specific results
91 private NodebApi nodebApi(String instanceKey) {
93 final NodebIdentityGlobalNbId globalNbId = new NodebIdentityGlobalNbId().nbId("mockNbId-" + instanceKey)
95 final List<NodebIdentity> nodebIdList = new ArrayList<>();
96 final Map<String, GetNodebResponse> nodebResponseMap = new HashMap<>();
98 nodebIdList.add(new NodebIdentity().inventoryName(RAN_NAME_1).globalNbId(globalNbId));
99 nodebResponseMap.put(RAN_NAME_1, new GetNodebResponse().connectionStatus("CONNECTED").failureType("")
100 .ip("127.0.0.1").nodeType("mockNodeType").port(123).ranName(RAN_NAME_1));
102 // [{"nodebIdentity":{"globalNbId":null,"inventoryName":"AAAA123456"},
103 // "nodebStatus":{"connectionStatus":"CONNECTING","enb":null,"failureType":null,
104 // "globalNbId":null,"gnb":null,"ip":"10.2.0.6","nodeType":null,"port":36444,
105 // "ranName":"AAAA123456","setupFailure":null}}]
106 nodebIdList.add(new NodebIdentity().inventoryName(RAN_NAME_1).globalNbId(globalNbId));
107 nodebResponseMap.put(RAN_NAME_1,
108 new GetNodebResponse().connectionStatus("CONNECTING").ip("127.0.0.1").port(456).ranName(RAN_NAME_2).nodeType("ENDC").port(100));
109 nodebIdList.add(new NodebIdentity().inventoryName(RAN_NAME_2).globalNbId(globalNbId));
110 nodebResponseMap.put(RAN_NAME_2,
111 new GetNodebResponse().connectionStatus("CONNECTED").ip("127.0.0.2").port(456).ranName(RAN_NAME_2).nodeType("X2").port(200));
113 ApiClient apiClient = apiClient();
114 NodebApi mockApi = mock(NodebApi.class);
115 when(mockApi.getApiClient()).thenReturn(apiClient);
118 logger.debug("nodebShutdownPut sleeping {}", delayMs);
119 Thread.sleep(delayMs);
123 }).when(mockApi).nodebShutdownPut();
126 logger.debug("reset sleeping {}", delayMs);
127 Thread.sleep(delayMs);
130 }).when(mockApi).reset(any(String.class), any(ResetRequest.class));
133 logger.debug("getNb sleeping {}", delayMs);
134 Thread.sleep(delayMs);
136 String invName = inv.<String>getArgument(0);
137 return nodebResponseMap.get(invName);
138 }).when(mockApi).getNb(any(String.class));
141 logger.debug("getNodebIdList sleeping {}", delayMs);
142 Thread.sleep(delayMs);
145 }).when(mockApi).getNodebIdList();
150 // Must use the same name as the non-mock configuration
151 public E2ManagerApiBuilder e2ManagerApiBuilder() {
152 final E2ManagerApiBuilder mockBuilder = mock(E2ManagerApiBuilder.class);
153 final HealthCheckApi mockHealthCheckApi = healthCheckApi();
154 when(mockBuilder.getHealthCheckApi(any(String.class))).thenReturn(mockHealthCheckApi);
155 for (final String key : RICInstanceMockConfiguration.INSTANCE_KEYS) {
156 final NodebApi mockNodebApi = nodebApi(key);
157 when(mockBuilder.getNodebApi(key)).thenReturn(mockNodebApi);