b9cbaf9092b33fb0ffa6e1f3ddb0a05eb6ea3be7
[portal/ric-dashboard.git] / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / config / CaasIngressMockConfiguration.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
30 import org.oransc.ric.portal.dashboard.k8sapi.SimpleKubernetesClient;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.beans.factory.annotation.Value;
35 import org.springframework.context.annotation.Bean;
36 import org.springframework.context.annotation.Configuration;
37 import org.springframework.context.annotation.Profile;
38
39 /**
40  * Creates mock implementations of Kubernetes clients that answer requests with
41  * sample data read from the filesystem.
42  */
43 @Configuration
44 @Profile("test")
45 public class CaasIngressMockConfiguration extends AbstractMockConfiguration {
46
47         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
48
49         // Simulate remote method delay for UI testing
50         private int delayMs;
51
52         @Autowired
53         public CaasIngressMockConfiguration(@Value("${mock.config.delay:0}") int delayMs) {
54                 logger.debug("ctor: configured with delay {}", delayMs);
55                 this.delayMs = delayMs;
56         }
57
58         private SimpleKubernetesClient simpleKubernetesClient(String instanceKey) throws IOException {
59                 // File in src/test/resources
60                 String pltPods;
61                 if (RICInstanceMockConfiguration.INSTANCE_KEY_1.equals(instanceKey))
62                         pltPods = readDataFromPath("caas-ingress-ricplt-pods-1.json");
63                 else
64                         pltPods = readDataFromPath("caas-ingress-ricplt-pods-2.json");
65                 SimpleKubernetesClient mockClient = mock(SimpleKubernetesClient.class);
66                 doAnswer(inv -> {
67                         String ns = inv.<String>getArgument(0);
68                         logger.debug("listPods for namespace {}", ns);
69                         if (delayMs > 0) {
70                                 logger.debug("listPods sleeping {}", delayMs);
71                                 Thread.sleep(delayMs);
72                         }
73                         if ("ricplt".equals(ns))
74                                 return pltPods;
75                         else
76                                 throw new IllegalArgumentException("Fake server failure");
77                 }).when(mockClient).listPods(any(String.class));
78                 return mockClient;
79         }
80
81         @Bean
82         // The bean (method) name must be globally unique
83         public SimpleKubernetesClientBuilder simpleKubernetesClientBuilder() throws IOException {
84                 final SimpleKubernetesClientBuilder mockBuilder = mock(SimpleKubernetesClientBuilder.class);
85                 for (final String key : RICInstanceMockConfiguration.INSTANCE_KEYS) {
86                         SimpleKubernetesClient client = simpleKubernetesClient(key);
87                         when(mockBuilder.getSimpleKubernetesClient(key)).thenReturn(client);
88                 }
89                 return mockBuilder;
90         }
91
92 }