Extend Mock E2Mgr configuration to save state
[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 and Nokia
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 @Profile("test")
53 @Configuration
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 MOCK_RAN_NAME = "Mock RAN";
63
64         private final List<NodebIdentity> nodebIdList;
65         private final Map<String, GetNodebResponse> nodebResponseMap;
66         private final NodebIdentityGlobalNbId globalNbId;
67
68         public E2ManagerMockConfiguration() {
69                 logger.info("Configuring mock E2 Manager");
70                 globalNbId = new NodebIdentityGlobalNbId().nbId("mockNbId").plmnId("mockPlmId");
71                 nodebIdList = new ArrayList<>();
72                 nodebResponseMap = new HashMap<>();
73                 nodebIdList.add(new NodebIdentity().inventoryName(MOCK_RAN_NAME).globalNbId(globalNbId));
74                 nodebResponseMap.put(MOCK_RAN_NAME,
75                                 new GetNodebResponse().connectionStatus("mockConnectionStatus").failureType("mockFailureType")
76                                                 .ip("127.0.0.1").nodeType("mockNodeType").port(123).ranName(MOCK_RAN_NAME));
77         }
78
79         private ApiClient apiClient() {
80                 ApiClient mockClient = mock(ApiClient.class);
81                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
82                 return mockClient;
83         }
84
85         @Bean
86         // Use the same name as regular configuration
87         public HealthCheckApi e2MgrHealthCheckApi() {
88                 ApiClient apiClient = apiClient();
89                 HealthCheckApi mockApi = mock(HealthCheckApi.class);
90                 when(mockApi.getApiClient()).thenReturn(apiClient);
91                 doAnswer(i -> null).when(mockApi).healthGet();
92                 return mockApi;
93         }
94
95         @Bean
96         // Use the same name as regular configuration
97         public NodebApi e2MgrNodebApi() {
98                 ApiClient apiClient = apiClient();
99                 NodebApi mockApi = mock(NodebApi.class);
100                 when(mockApi.getApiClient()).thenReturn(apiClient);
101                 doAnswer(inv -> {
102                         if (delayMs > 0) {
103                                 logger.debug("nodebShutdownPut sleeping {}", delayMs);
104                                 Thread.sleep(delayMs);
105                         }
106                         nodebIdList.clear();
107                         return null;
108                 }).when(mockApi).nodebShutdownPut();
109                 doAnswer(inv -> {
110                         if (delayMs > 0) {
111                                 logger.debug("reset sleeping {}", delayMs);
112                                 Thread.sleep(delayMs);
113                         }
114                         return null;
115                 }).when(mockApi).reset(any(String.class), any(ResetRequest.class));
116                 doAnswer(inv -> {
117                         if (delayMs > 0) {
118                                 logger.debug("getNb sleeping {}", delayMs);
119                                 Thread.sleep(delayMs);
120                         }
121                         String invName = inv.<String>getArgument(0);
122                         return nodebResponseMap.get(invName);
123                 }).when(mockApi).getNb(any(String.class));
124                 doAnswer(inv -> {
125                         if (delayMs > 0) {
126                                 logger.debug("getNodebIdList sleeping {}", delayMs);
127                                 Thread.sleep(delayMs);
128                         }
129                         return nodebIdList;
130                 }).when(mockApi).getNodebIdList();
131                 doAnswer(inv -> {
132                         if (delayMs > 0) {
133                                 logger.debug("endcSetup sleeping {}", delayMs);
134                                 Thread.sleep(delayMs);
135                         }
136                         SetupRequest sr = inv.<SetupRequest>getArgument(0);
137                         nodebIdList.add(new NodebIdentity().inventoryName(sr.getRanName()).globalNbId(globalNbId));
138                         nodebResponseMap.put(sr.getRanName(),
139                                         new GetNodebResponse().connectionStatus("mockConnectionStatus").failureType("mockFailureType")
140                                                         .ip(sr.getRanIp()).nodeType("ENDC").port(sr.getRanPort()).ranName(sr.getRanName()));
141                         return null;
142                 }).when(mockApi).endcSetup(any(SetupRequest.class));
143                 doAnswer(inv -> {
144                         if (delayMs > 0) {
145                                 logger.debug("x2Setup sleeping {}", delayMs);
146                                 Thread.sleep(delayMs);
147                         }
148                         SetupRequest sr = inv.<SetupRequest>getArgument(0);
149                         nodebIdList.add(new NodebIdentity().inventoryName(sr.getRanName()).globalNbId(globalNbId));
150                         nodebResponseMap.put(sr.getRanName(),
151                                         new GetNodebResponse().connectionStatus("mockConnectionStatus").failureType("mockFailureType")
152                                                         .ip(sr.getRanIp()).nodeType("X2").port(sr.getRanPort()).ranName(sr.getRanName()));
153                         return null;
154                 }).when(mockApi).x2Setup(any(SetupRequest.class));
155                 return mockApi;
156         }
157
158 }