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.io.IOException;
28 import java.lang.invoke.MethodHandles;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
34 import org.oransc.ric.a1med.client.api.A1MediatorApi;
35 import org.oransc.ric.a1med.client.invoker.ApiClient;
36 import org.oransc.ric.a1med.client.model.PolicyTypeSchema;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Value;
40 import org.springframework.context.annotation.Bean;
41 import org.springframework.context.annotation.Configuration;
42 import org.springframework.context.annotation.Profile;
43 import org.springframework.http.HttpStatus;
45 import com.fasterxml.jackson.core.JsonParseException;
46 import com.fasterxml.jackson.databind.JsonMappingException;
47 import com.fasterxml.jackson.databind.ObjectMapper;
50 * Creates a mock implementation of the A1 mediator client builder with mock
51 * methods that answer Admission Control mock data.
55 public class A1MediatorMockConfiguration extends AbstractMockConfiguration {
57 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59 // A "control" is an element in the XApp descriptor
60 public static final String AC_CONTROL_NAME = "admission_control_policy";
61 public static final Integer ADMISSION_CONTROL_POLICY_ID = 21000;
63 // Simulate remote method delay for UI testing
64 private final int delayMs;
67 private final List<Integer> policyTypeIds;
68 private final PolicyTypeSchema rateControlPolicyType;
69 private final Map<String, String> appPolicyMap;
71 public A1MediatorMockConfiguration(@Value("${mock.config.delay:0}") int delayMs)
72 throws IOException, JsonParseException, JsonMappingException {
73 logger.debug("ctor: configured with delay {}", delayMs);
74 this.delayMs = delayMs;
75 policyTypeIds = new ArrayList<>();
76 policyTypeIds.add(ADMISSION_CONTROL_POLICY_ID);
77 ObjectMapper mapper = new ObjectMapper();
78 final String policyType = readDataFromPath("rate-control-policy-type.json");
79 rateControlPolicyType = mapper.readValue(policyType, PolicyTypeSchema.class);
80 final String policyInstance = readDataFromPath("rate-control-policy-instance.json");
81 appPolicyMap = new HashMap<>();
82 appPolicyMap.put(AC_CONTROL_NAME, policyInstance);
85 private void delay() throws InterruptedException {
87 logger.debug("delay: sleeping {}", delayMs);
88 Thread.sleep(delayMs);
92 private ApiClient apiClient() {
93 ApiClient mockClient = mock(ApiClient.class);
94 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
98 private A1MediatorApi a1MediatorApi(String instanceKey) {
99 logger.debug("a1MediatorApi: instance {}", instanceKey);
100 ApiClient apiClient = apiClient();
101 A1MediatorApi mockApi = mock(A1MediatorApi.class);
102 when(mockApi.getApiClient()).thenReturn(apiClient);
105 return policyTypeIds;
106 }).when(mockApi).a1ControllerGetAllPolicyTypes();
109 Integer policyTypeId = inv.<Integer>getArgument(0);
110 if (policyTypeId.compareTo(ADMISSION_CONTROL_POLICY_ID) != 0)
111 throw new IllegalArgumentException("Unexpected policy type: " + policyTypeId);
112 return rateControlPolicyType;
113 }).when(mockApi).a1ControllerGetPolicyType(any(Integer.class));
116 Integer policyTypeId = inv.<Integer>getArgument(0);
117 if (policyTypeId.compareTo(ADMISSION_CONTROL_POLICY_ID) != 0)
118 throw new IllegalArgumentException("Unexpected policy type: " + policyTypeId);
119 String policyInstId = inv.<String>getArgument(1);
120 if (!AC_CONTROL_NAME.equals(policyInstId))
121 throw new IllegalArgumentException("Unexpected policy instance: " + policyInstId);
122 return appPolicyMap.get(policyInstId);
123 }).when(mockApi).a1ControllerGetPolicyInstance(any(Integer.class), any(String.class));
126 Integer policyTypeId = inv.<Integer>getArgument(0);
127 if (policyTypeId.compareTo(ADMISSION_CONTROL_POLICY_ID) != 0)
128 throw new IllegalArgumentException("Unexpected policy type: " + policyTypeId);
129 String policyInstId = inv.<String>getArgument(1);
130 if (!AC_CONTROL_NAME.equals(policyInstId))
131 throw new IllegalArgumentException("Unexpected policy instance: " + policyInstId);
132 String policy = inv.<String>getArgument(2);
133 appPolicyMap.put(policyInstId, policy);
135 }).when(mockApi).a1ControllerCreateOrReplacePolicyInstance(any(Integer.class), any(String.class),
141 // Must use the same name as the non-mock configuration
142 public A1MediatorApiBuilder a1MediatorApiBuilder() {
143 final A1MediatorApiBuilder mockBuilder = mock(A1MediatorApiBuilder.class);
144 for (final String key : RICInstanceMockConfiguration.INSTANCE_KEYS) {
145 final A1MediatorApi mockApi = a1MediatorApi(key);
146 when(mockBuilder.getA1MediatorApi(key)).thenReturn(mockApi);