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