16e2c75caede5cae9788a27879cce400776d31ef
[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.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;
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         // Autowire all the properties required by the real class
66         // (even tho not used here) as a test of the properties.
67         @Autowired
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;
73         }
74
75         private ApiClient apiClient() {
76                 ApiClient mockClient = mock(ApiClient.class);
77                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
78                 return mockClient;
79         }
80
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();
86                 return mockApi;
87         }
88
89         /**
90          * Builds a mock NodebApi object.
91          * 
92          * @param instanceKey
93          *                        RIC instance
94          * @return Object that returns instance-specific results
95          */
96         private NodebApi nodebApi(String instanceKey) {
97
98                 final NodebIdentityGlobalNbId globalNbId = new NodebIdentityGlobalNbId().nbId("mockNbId-" + instanceKey)
99                                 .plmnId("mockPlmId");
100                 final List<NodebIdentity> nodebIdList = new ArrayList<>();
101                 final Map<String, GetNodebResponse> nodebResponseMap = new HashMap<>();
102                 // Complete entry
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));
106                 // Partial entry
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));
117
118                 ApiClient apiClient = apiClient();
119                 NodebApi mockApi = mock(NodebApi.class);
120                 when(mockApi.getApiClient()).thenReturn(apiClient);
121                 doAnswer(inv -> {
122                         if (delayMs > 0) {
123                                 logger.debug("nodebShutdownPut sleeping {}", delayMs);
124                                 Thread.sleep(delayMs);
125                         }
126                         nodebIdList.clear();
127                         return null;
128                 }).when(mockApi).nodebShutdownPut();
129                 doAnswer(inv -> {
130                         if (delayMs > 0) {
131                                 logger.debug("reset sleeping {}", delayMs);
132                                 Thread.sleep(delayMs);
133                         }
134                         return null;
135                 }).when(mockApi).reset(any(String.class), any(ResetRequest.class));
136                 doAnswer(inv -> {
137                         if (delayMs > 0) {
138                                 logger.debug("getNb sleeping {}", delayMs);
139                                 Thread.sleep(delayMs);
140                         }
141                         String invName = inv.<String>getArgument(0);
142                         return nodebResponseMap.get(invName);
143                 }).when(mockApi).getNb(any(String.class));
144                 doAnswer(inv -> {
145                         if (delayMs > 0) {
146                                 logger.debug("getNodebIdList sleeping {}", delayMs);
147                                 Thread.sleep(delayMs);
148                         }
149                         return nodebIdList;
150                 }).when(mockApi).getNodebIdList();
151                 return mockApi;
152         }
153
154         @Bean
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);
163                 }
164                 return mockBuilder;
165         }
166
167 }