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.ric.portal.dashboard.model.RicRegionList;
34 import org.oransc.ricplt.e2mgr.client.api.HealthCheckApi;
35 import org.oransc.ricplt.e2mgr.client.api.NodebApi;
36 import org.oransc.ricplt.e2mgr.client.invoker.ApiClient;
37 import org.oransc.ricplt.e2mgr.client.model.GetNodebResponse;
38 import org.oransc.ricplt.e2mgr.client.model.NodebIdentity;
39 import org.oransc.ricplt.e2mgr.client.model.NodebIdentityGlobalNbId;
40 import org.oransc.ricplt.e2mgr.client.model.ResetRequest;
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;
51 * Creates a mock implementation of the E2 Manager client API.
55 public class E2ManagerMockConfiguration {
57 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59 public static final String RAN_NAME_1 = "Connected-RAN";
60 public static final String RAN_NAME_2 = "Unknown-RAN";
62 // Simulate remote method delay for UI testing
65 // Autowire all the properties required by the real class
66 // (even tho not used here) as a test of the properties.
68 public E2ManagerMockConfiguration(@Value("${e2mgr.url.suffix}") final String urlSuffix, //
69 final RicRegionList instanceConfig, //
70 @Value("${mock.config.delay:0}") int delayMs) {
71 logger.info("ctor: configured with suffix {}, instances {}, delay {}", urlSuffix, instanceConfig, delayMs);
72 this.delayMs = delayMs;
75 private ApiClient apiClient() {
76 ApiClient mockClient = mock(ApiClient.class);
77 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
81 private HealthCheckApi healthCheckApi() {
82 ApiClient apiClient = apiClient();
83 HealthCheckApi mockApi = mock(HealthCheckApi.class);
84 when(mockApi.getApiClient()).thenReturn(apiClient);
85 doAnswer(i -> null).when(mockApi).healthGet();
90 * Builds a mock NodebApi object.
94 * @return Object that returns instance-specific results
96 private NodebApi nodebApi(String instanceKey) {
98 final NodebIdentityGlobalNbId globalNbId = new NodebIdentityGlobalNbId().nbId("mockNbId-" + instanceKey)
100 final List<NodebIdentity> nodebIdList = new ArrayList<>();
101 final Map<String, GetNodebResponse> nodebResponseMap = new HashMap<>();
103 nodebIdList.add(new NodebIdentity().inventoryName(RAN_NAME_1).globalNbId(globalNbId));
104 nodebResponseMap.put(RAN_NAME_1, new GetNodebResponse().connectionStatus("CONNECTED").failureType("")
105 .ip("127.0.0.1").nodeType("mockNodeType").port(123).ranName(RAN_NAME_1));
107 // [{"nodebIdentity":{"globalNbId":null,"inventoryName":"AAAA123456"},
108 // "nodebStatus":{"connectionStatus":"CONNECTING","enb":null,"failureType":null,
109 // "globalNbId":null,"gnb":null,"ip":"10.2.0.6","nodeType":null,"port":36444,
110 // "ranName":"AAAA123456","setupFailure":null}}]
111 nodebIdList.add(new NodebIdentity().inventoryName(RAN_NAME_1).globalNbId(globalNbId));
112 nodebResponseMap.put(RAN_NAME_1, new GetNodebResponse().connectionStatus("CONNECTING").ip("127.0.0.1").port(456)
113 .ranName(RAN_NAME_2).nodeType("ENDC").port(100));
114 nodebIdList.add(new NodebIdentity().inventoryName(RAN_NAME_2).globalNbId(globalNbId));
115 nodebResponseMap.put(RAN_NAME_2, new GetNodebResponse().connectionStatus("CONNECTED").ip("127.0.0.2").port(456)
116 .ranName(RAN_NAME_2).nodeType("X2").port(200));
118 ApiClient apiClient = apiClient();
119 NodebApi mockApi = mock(NodebApi.class);
120 when(mockApi.getApiClient()).thenReturn(apiClient);
123 logger.debug("nodebShutdownPut sleeping {}", delayMs);
124 Thread.sleep(delayMs);
128 }).when(mockApi).nodebShutdownPut();
131 logger.debug("reset sleeping {}", delayMs);
132 Thread.sleep(delayMs);
135 }).when(mockApi).reset(any(String.class), any(ResetRequest.class));
138 logger.debug("getNb sleeping {}", delayMs);
139 Thread.sleep(delayMs);
141 String invName = inv.<String>getArgument(0);
142 return nodebResponseMap.get(invName);
143 }).when(mockApi).getNb(any(String.class));
146 logger.debug("getNodebIdList sleeping {}", delayMs);
147 Thread.sleep(delayMs);
150 }).when(mockApi).getNodebIdList();
155 // Must use the same name as the non-mock configuration
156 public E2ManagerApiBuilder e2ManagerApiBuilder() {
157 final E2ManagerApiBuilder mockBuilder = mock(E2ManagerApiBuilder.class);
158 final HealthCheckApi mockHealthCheckApi = healthCheckApi();
159 when(mockBuilder.getHealthCheckApi(any(String.class))).thenReturn(mockHealthCheckApi);
160 for (final String key : RICInstanceMockConfiguration.INSTANCE_KEYS) {
161 final NodebApi mockNodebApi = nodebApi(key);
162 when(mockBuilder.getNodebApi(key)).thenReturn(mockNodebApi);