d3ac7a7d344833cfba6012ac43758f8b80299a6c
[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 builder.
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         private final int delayMs;
58
59         // Mock values
60         private final Map<String, String> appPolicyMap;
61
62         public A1MediatorMockConfiguration(@Value("${mock.config.delay:0}") int delayMs) {
63                 logger.info("ctor: mock A1 Mediator configured with delay {}", delayMs);
64                 this.delayMs = delayMs;
65                 appPolicyMap = new HashMap<>();
66                 // Define a mock AC policy
67                 ObjectMapper mapper = new ObjectMapper();
68                 ObjectNode node = mapper.createObjectNode();
69                 // These fields are defined in the ACAdmissionIntervalControl
70                 // Typescript interface, but are otherwise unknown to this backend.
71                 node.put("enforce", Boolean.TRUE);
72                 node.put("window_length", 0);
73                 node.put("blocking_rate", 0);
74                 node.put("trigger_threshold", 0);
75                 appPolicyMap.put(AC_CONTROL_NAME, node.toString());
76         }
77
78         private ApiClient apiClient() {
79                 ApiClient mockClient = mock(ApiClient.class);
80                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
81                 return mockClient;
82         }
83
84         private A1MediatorApi a1MediatorApi() {
85                 ApiClient apiClient = apiClient();
86                 A1MediatorApi mockApi = mock(A1MediatorApi.class);
87                 when(mockApi.getApiClient()).thenReturn(apiClient);
88                 doAnswer(inv -> {
89                         if (delayMs > 0) {
90                                 logger.debug("a1ControllerGetHandler sleeping {}", delayMs);
91                                 Thread.sleep(delayMs);
92                         }
93                         String appName = inv.<String>getArgument(0);
94                         return appPolicyMap.get(appName);
95                 }).when(mockApi).a1ControllerGetHandler(any(String.class));
96                 doAnswer(inv -> {
97                         if (delayMs > 0) {
98                                 logger.debug("a1ControllerPutHandler sleeping {}", delayMs);
99                                 Thread.sleep(delayMs);
100                         }
101                         String appName = inv.<String>getArgument(0);
102                         String policy = inv.<String>getArgument(1);
103                         appPolicyMap.put(appName, policy);
104                         return null;
105                 }).when(mockApi).a1ControllerPutHandler(any(String.class), any(Object.class));
106                 return mockApi;
107         }
108
109         @Bean
110         // Must use the same name as the non-mock configuration
111         public A1MediatorApiBuilder a1MediatorApiBuilder() {
112                 final A1MediatorApi mockApi = a1MediatorApi();
113                 final A1MediatorApiBuilder mockBuilder = mock(A1MediatorApiBuilder.class);
114                 when(mockBuilder.getA1MediatorApi(any(String.class))).thenReturn(mockApi);
115                 return mockBuilder;
116         }
117
118 }