21b2face37116d99a7463ef79927ec2be6509966
[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.io.IOException;
28 import java.lang.invoke.MethodHandles;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
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;
44
45 import com.fasterxml.jackson.core.JsonParseException;
46 import com.fasterxml.jackson.databind.JsonMappingException;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48
49 /**
50  * Creates a mock implementation of the A1 mediator client builder with mock
51  * methods that answer Admission Control mock data.
52  */
53 @Configuration
54 @Profile("test")
55 public class A1MediatorMockConfiguration extends AbstractMockConfiguration {
56
57         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
58
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;
62
63         // Simulate remote method delay for UI testing
64         private final int delayMs;
65
66         // Mock values
67         private final List<Integer> policyTypeIds;
68         private final PolicyTypeSchema rateControlPolicyType;
69         private final Map<String, String> appPolicyMap;
70
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);
83         }
84
85         private void delay() throws InterruptedException {
86                 if (delayMs > 0) {
87                         logger.debug("delay: sleeping {}", delayMs);
88                         Thread.sleep(delayMs);
89                 }
90         }
91
92         private ApiClient apiClient() {
93                 ApiClient mockClient = mock(ApiClient.class);
94                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
95                 return mockClient;
96         }
97
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);
103                 doAnswer(inv -> {
104                         delay();
105                         return policyTypeIds;
106                 }).when(mockApi).a1ControllerGetAllPolicyTypes();
107                 doAnswer(inv -> {
108                         delay();
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));
114                 doAnswer(inv -> {
115                         delay();
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));
124                 doAnswer(inv -> {
125                         delay();
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);
134                         return null;
135                 }).when(mockApi).a1ControllerCreateOrReplacePolicyInstance(any(Integer.class), any(String.class),
136                                 any(Object.class));
137                 return mockApi;
138         }
139
140         @Bean
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);
147                 }
148                 return mockBuilder;
149         }
150
151 }