Revise user controller to answer real data
[portal/ric-dashboard.git] / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / config / A1MediatorMockConfiguration.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
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.HashMap;
29 import java.util.Map;
30
31 import org.oransc.ric.a1med.client.api.A1MediatorApi;
32 import org.oransc.ric.a1med.client.invoker.ApiClient;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.context.annotation.Bean;
37 import org.springframework.context.annotation.Configuration;
38 import org.springframework.context.annotation.Profile;
39 import org.springframework.http.HttpStatus;
40
41 import com.fasterxml.jackson.databind.ObjectMapper;
42 import com.fasterxml.jackson.databind.node.ObjectNode;
43
44 /**
45  * Creates a mock implementation of the A1 mediator client API.
46  */
47 @Configuration
48 @Profile("test")
49 public class A1MediatorMockConfiguration {
50
51         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
52
53         // A "control" is an element in the XApp descriptor
54         public static final String AC_CONTROL_NAME = "admission_control_policy";
55
56         // Simulate remote method delay for UI testing
57         @Value("${mock.config.delay:0}")
58         private int delayMs;
59
60         private final Map<String, String> appPolicyMap;
61
62         public A1MediatorMockConfiguration() {
63                 logger.info("Configuring mock A1 Mediator");
64                 appPolicyMap = new HashMap<>();
65                 // Define a mock AC policy
66                 ObjectMapper mapper = new ObjectMapper();
67                 ObjectNode node = mapper.createObjectNode();
68                 // These fields are defined in the ACAdmissionIntervalControl
69                 // Typescript interface, but are otherwise unknown to this backend.
70                 node.put("enforce", Boolean.TRUE);
71                 node.put("window_length", 0);
72                 node.put("blocking_rate", 0);
73                 node.put("trigger_threshold", 0);
74                 appPolicyMap.put(AC_CONTROL_NAME, node.toString());
75         }
76
77         private ApiClient apiClient() {
78                 ApiClient mockClient = mock(ApiClient.class);
79                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
80                 return mockClient;
81         }
82
83         @Bean
84         // Use the same name as regular configuration
85         public A1MediatorApi a1MediatorApi() {
86                 ApiClient apiClient = apiClient();
87                 A1MediatorApi mockApi = mock(A1MediatorApi.class);
88                 when(mockApi.getApiClient()).thenReturn(apiClient);
89                 doAnswer(inv -> {
90                         if (delayMs > 0) {
91                                 logger.debug("a1ControllerGetHandler sleeping {}", delayMs);
92                                 Thread.sleep(delayMs);
93                         }
94                         String appName = inv.<String>getArgument(0);
95                         return appPolicyMap.get(appName);
96                 }).when(mockApi).a1ControllerGetHandler(any(String.class));
97                 doAnswer(inv -> {
98                         if (delayMs > 0) {
99                                 logger.debug("a1ControllerPutHandler sleeping {}", delayMs);
100                                 Thread.sleep(delayMs);
101                         }
102                         String appName = inv.<String>getArgument(0);
103                         String policy = inv.<String>getArgument(1);
104                         appPolicyMap.put(appName, policy);
105                         return null;
106                 }).when(mockApi).a1ControllerPutHandler(any(String.class), any(Object.class));
107                 return mockApi;
108         }
109
110 }