d33852672ae9a9b8ec48b49adf9fa697d32a0c91
[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.List;
30
31 import org.oransc.ric.e2mgr.client.api.HealthCheckApi;
32 import org.oransc.ric.e2mgr.client.api.NodebApi;
33 import org.oransc.ric.e2mgr.client.invoker.ApiClient;
34 import org.oransc.ric.e2mgr.client.model.GetNodebResponse;
35 import org.oransc.ric.e2mgr.client.model.NodebIdentity;
36 import org.oransc.ric.e2mgr.client.model.NodebIdentityGlobalNbId;
37 import org.oransc.ric.e2mgr.client.model.SetupRequest;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.context.annotation.Bean;
42 import org.springframework.context.annotation.Configuration;
43 import org.springframework.context.annotation.Profile;
44 import org.springframework.http.HttpStatus;
45
46 /**
47  * Creates a mock implementation of the E2 Manager client API.
48  */
49 @Profile("test")
50 @Configuration
51 public class E2ManagerMockConfiguration {
52
53         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
54
55         // Simulate remote method delay for UI testing
56         @Value("${mock.config.delay:0}")
57         private int delayMs;
58
59         private final List<NodebIdentity> nodebIdList;
60         private final GetNodebResponse nodebResponse;
61
62         public E2ManagerMockConfiguration() {
63                 logger.info("Configuring mock E2 Manager");
64                 NodebIdentityGlobalNbId globalNbId = new NodebIdentityGlobalNbId().nbId("mockNbId").plmnId("mockPlmId");
65                 NodebIdentity nbid = new NodebIdentity().inventoryName("mockInvName").globalNbId(globalNbId);
66                 nodebIdList = new ArrayList<>();
67                 nodebIdList.add(nbid);
68                 nodebResponse = new GetNodebResponse().connectionStatus("mockConnectionStatus").failureType("mockFailureType")
69                                 .ip("127.0.0.1").nodeType("mockNodeType").port(123).ranName("mockRanName");
70         }
71
72         private ApiClient apiClient() {
73                 ApiClient mockClient = mock(ApiClient.class);
74                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
75                 return mockClient;
76         }
77
78         @Bean
79         // Use the same name as regular configuration
80         public HealthCheckApi e2MgrHealthCheckApi() {
81                 ApiClient apiClient = apiClient();
82                 HealthCheckApi mockApi = mock(HealthCheckApi.class);
83                 when(mockApi.getApiClient()).thenReturn(apiClient);
84                 doAnswer(i -> null).when(mockApi).healthGet();
85                 return mockApi;
86         }
87
88         @Bean
89         // Use the same name as regular configuration
90         public NodebApi e2MgrNodebApi() {
91                 ApiClient apiClient = apiClient();
92                 NodebApi mockApi = mock(NodebApi.class);
93                 when(mockApi.getApiClient()).thenReturn(apiClient);
94                 doAnswer(inv -> {
95                         if (delayMs > 0) {
96                                 logger.debug("nodebDelete sleeping {}", delayMs);
97                                 Thread.sleep(delayMs);
98                         }
99                         return null;
100                 }).when(mockApi).nodebDelete();
101                 doAnswer(inv -> {
102                         if (delayMs > 0) {
103                                 logger.debug("getNb sleeping {}", delayMs);
104                                 Thread.sleep(delayMs);
105                         }
106                         return nodebResponse;
107                 }).when(mockApi).getNb(any(String.class));
108                 doAnswer(inv -> {
109                         if (delayMs > 0) {
110                                 logger.debug("getNodebIdList sleeping {}", delayMs);
111                                 Thread.sleep(delayMs);
112                         }
113                         return nodebIdList;
114                 }).when(mockApi).getNodebIdList();
115                 doAnswer(inv -> {
116                         if (delayMs > 0) {
117                                 logger.debug("endcSetup sleeping {}", delayMs);
118                                 Thread.sleep(delayMs);
119                         }
120                         return null;
121                 }).when(mockApi).endcSetup(any(SetupRequest.class));
122                 doAnswer(inv -> {
123                         if (delayMs > 0) {
124                                 logger.debug("x2Setup sleeping {}", delayMs);
125                                 Thread.sleep(delayMs);
126                         }
127                         return null;
128                 }).when(mockApi).x2Setup(any(SetupRequest.class));
129                 return mockApi;
130         }
131
132 }