Move mock configurations to test area
[portal/ric-dashboard.git] / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / test / config / A1MediatorMockConfiguration.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
29 import org.oransc.ric.a1med.client.api.A1MediatorApi;
30 import org.oransc.ric.a1med.client.invoker.ApiClient;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.context.annotation.Bean;
34 import org.springframework.context.annotation.Configuration;
35 import org.springframework.context.annotation.Profile;
36 import org.springframework.http.HttpStatus;
37
38 /**
39  * Creates a mock implementation of the A1 mediator client API.
40  */
41 @Profile("test")
42 @Configuration
43 public class A1MediatorMockConfiguration {
44
45         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
46         // Simulate remote method delay for UI testing
47         private final int delayMs = 500;
48
49         public A1MediatorMockConfiguration() {
50                 logger.info("Configuring mock A1 Mediator");
51         }
52
53         private ApiClient apiClient() {
54                 ApiClient mockClient = mock(ApiClient.class);
55                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
56                 return mockClient;
57         }
58
59         @Bean
60         // Use the same name as regular configuration
61         public A1MediatorApi a1MediatorApi() {
62                 ApiClient apiClient = apiClient();
63                 A1MediatorApi mockApi = mock(A1MediatorApi.class);
64                 when(mockApi.getApiClient()).thenReturn(apiClient);
65                 doAnswer(inv -> {
66                         logger.debug("a1ControllerGetHandler sleeping {}", delayMs);
67                         Thread.sleep(delayMs);
68                         return null;
69                 }).when(mockApi).a1ControllerGetHandler(any(String.class));
70                 doAnswer(inv -> {
71                         logger.debug("a1ControllerPutHandler sleeping {}", delayMs);
72                         Thread.sleep(delayMs);
73                         return null;
74                 }).when(mockApi).a1ControllerPutHandler(any(String.class), any(Object.class));
75                 return mockApi;
76         }
77
78 }