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