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;
30 import org.oransc.ric.portal.dashboard.TestUtils;
31 import org.oransc.ric.portal.dashboard.k8sapi.SimpleKubernetesClient;
32 import org.oransc.ric.portal.dashboard.model.RicRegionList;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.beans.factory.annotation.Value;
37 import org.springframework.context.annotation.Bean;
38 import org.springframework.context.annotation.Configuration;
39 import org.springframework.context.annotation.Profile;
42 * Creates mock implementations of Kubernetes clients that answer requests with
43 * sample data read from the filesystem.
47 public class CaasIngressMockConfiguration {
49 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
51 // Simulate remote method delay for UI testing
54 // Autowire all the properties required by the real class
55 // (even tho not used here) as a test of the properties.
57 public CaasIngressMockConfiguration(@Value("${caasingress.plt.url.suffix}") final String pltUrlSuffix, //
58 @Value("${caasingress.insecure}") final Boolean insecureFlag, //
59 final RicRegionList instanceConfig, //
60 @Value("${mock.config.delay:0}") int delayMs) {
61 logger.info("ctor: configured with suffix {}, insecure {}, instance {}, delay {}", pltUrlSuffix, insecureFlag,
62 instanceConfig, delayMs);
63 this.delayMs = delayMs;
66 private SimpleKubernetesClient simpleKubernetesClient(String instanceKey) throws IOException {
67 // File in src/test/resources
68 String podFile = RICInstanceMockConfiguration.INSTANCE_KEY_1.equals(instanceKey)
69 ? "caas-ingress-ricplt-pods-1.json"
70 : "caas-ingress-ricplt-pods-2.json";
71 String pltPods = TestUtils.readDataFromPath(podFile);
72 SimpleKubernetesClient mockClient = mock(SimpleKubernetesClient.class);
74 String ns = inv.<String>getArgument(0);
75 logger.debug("listPods for namespace {}", ns);
77 logger.debug("listPods sleeping {}", delayMs);
78 Thread.sleep(delayMs);
80 if ("ricplt".equals(ns))
83 throw new IllegalArgumentException("Fake server failure");
84 }).when(mockClient).listPods(any(String.class));
89 // The bean (method) name must be globally unique
90 public SimpleKubernetesClientBuilder simpleKubernetesClientBuilder() throws IOException {
91 final SimpleKubernetesClientBuilder mockBuilder = mock(SimpleKubernetesClientBuilder.class);
92 for (final String key : RICInstanceMockConfiguration.INSTANCE_KEYS) {
93 SimpleKubernetesClient client = simpleKubernetesClient(key);
94 when(mockBuilder.getSimpleKubernetesClient(key)).thenReturn(client);