1420086fe7e5c6d53deb69c36f70fd5a3f22ff7d
[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.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.lang.invoke.MethodHandles;
31
32 import org.oransc.ric.portal.dashboard.k8sapi.SimpleKubernetesClient;
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
40 /**
41  * Creates mock implementations of Kubernetes clients that answer requests with
42  * sample data read from the filesystem.
43  */
44 @Configuration
45 @Profile("test")
46 public class CaasIngressMockConfiguration {
47
48         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
49
50         // Simulate remote method delay for UI testing
51         @Value("${mock.config.delay:0}")
52         private int delayMs;
53
54         private final String pltPods;
55
56         public CaasIngressMockConfiguration() throws IOException {
57                 logger.info("Configuring mock CAAS-Ingres clients");
58                 // Files in src/test/resources
59                 pltPods = readDataFromPath("caas-ingress-ricplt-pods.json");
60         }
61
62         private String readDataFromPath(String path) throws IOException {
63                 InputStream is = MethodHandles.lookup().lookupClass().getClassLoader().getResourceAsStream(path);
64                 if (is == null) {
65                         String msg = "Failed to find resource on classpath: " + path;
66                         logger.error(msg);
67                         throw new RuntimeException(msg);
68                 }
69                 InputStreamReader reader = new InputStreamReader(is, "UTF-8");
70                 StringBuilder sb = new StringBuilder();
71                 char[] buf = new char[8192];
72                 int i;
73                 while ((i = reader.read(buf)) > 0)
74                         sb.append(buf, 0, i);
75                 reader.close();
76                 is.close();
77                 return sb.toString();
78         }
79
80         private SimpleKubernetesClient simpleKubernetesClient() {
81                 SimpleKubernetesClient mockClient = mock(SimpleKubernetesClient.class);
82                 doAnswer(inv -> {
83                         String ns = inv.<String>getArgument(0);
84                         logger.debug("listPods for namespace {}", ns);
85                         if ("ricplt".equals(ns))
86                                 return pltPods;
87                         else
88                                 throw new IllegalArgumentException("Fake server failure");
89                 }).when(mockClient).listPods(any(String.class));
90                 return mockClient;
91         }
92
93         @Bean
94         // The bean (method) name must be globally unique
95         public SimpleKubernetesClientBuilder simpleKubernetesClientBuilder() {
96                 final SimpleKubernetesClientBuilder mockBuilder = mock(SimpleKubernetesClientBuilder.class);
97                 SimpleKubernetesClient client = simpleKubernetesClient();
98                 when(mockBuilder.getSimpleKubernetesClient(any(String.class))).thenReturn(client);
99                 return mockBuilder;
100         }
101
102 }