2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.ric.portal.dashboard.config;
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;
27 import java.lang.invoke.MethodHandles;
28 import java.util.HashMap;
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;
41 import com.fasterxml.jackson.databind.ObjectMapper;
42 import com.fasterxml.jackson.databind.node.ObjectNode;
45 * Creates a mock implementation of the A1 mediator client builder.
49 public class A1MediatorMockConfiguration {
51 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
53 // A "control" is an element in the XApp descriptor
54 public static final String AC_CONTROL_NAME = "admission_control_policy";
56 // Simulate remote method delay for UI testing
57 private final int delayMs;
60 private final Map<String, String> appPolicyMap;
62 public A1MediatorMockConfiguration(@Value("${mock.config.delay:0}") int delayMs) {
63 logger.debug("ctor: 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());
78 private ApiClient apiClient() {
79 ApiClient mockClient = mock(ApiClient.class);
80 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
84 private A1MediatorApi a1MediatorApi(String instanceKey) {
85 ApiClient apiClient = apiClient();
86 A1MediatorApi mockApi = mock(A1MediatorApi.class);
87 when(mockApi.getApiClient()).thenReturn(apiClient);
90 logger.debug("a1ControllerGetHandler sleeping {}", delayMs);
91 Thread.sleep(delayMs);
93 String appName = inv.<String>getArgument(0);
94 return appPolicyMap.get(appName);
95 }).when(mockApi).a1ControllerGetHandler(any(String.class));
98 logger.debug("a1ControllerPutHandler sleeping {}", delayMs);
99 Thread.sleep(delayMs);
101 String appName = inv.<String>getArgument(0);
102 String policy = inv.<String>getArgument(1);
103 appPolicyMap.put(appName, policy);
105 }).when(mockApi).a1ControllerPutHandler(any(String.class), any(Object.class));
110 // Must use the same name as the non-mock configuration
111 public A1MediatorApiBuilder a1MediatorApiBuilder() {
112 final A1MediatorApiBuilder mockBuilder = mock(A1MediatorApiBuilder.class);
113 for (final String key : RICInstanceMockConfiguration.INSTANCE_KEYS) {
114 final A1MediatorApi mockApi = a1MediatorApi(key);
115 when(mockBuilder.getA1MediatorApi(key)).thenReturn(mockApi);