Improve coverage and silence sonar warnings
[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
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.lang.invoke.MethodHandles;
30
31 import org.oransc.ric.portal.dashboard.k8sapi.SimpleKubernetesClient;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
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 {
46
47         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
48
49         // Simulate remote method delay for UI testing
50         @Value("${mock.config.delay:0}")
51         private int delayMs;
52
53         private final String auxPods;
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                 auxPods = readDataFromPath("caas-ingress-ricaux-pods.json");
60                 pltPods = readDataFromPath("caas-ingress-ricplt-pods.json");
61         }
62
63         private String readDataFromPath(String path) throws IOException {
64                 InputStream is = MethodHandles.lookup().lookupClass().getClassLoader().getResourceAsStream(path);
65                 if (is == null) {
66                         String msg = "Failed to find resource on classpath: " + path;
67                         logger.error(msg);
68                         throw new RuntimeException(msg);
69                 }
70                 InputStreamReader reader = new InputStreamReader(is, "UTF-8");
71                 StringBuilder sb = new StringBuilder();
72                 char[] buf = new char[8192];
73                 int i;
74                 while ((i = reader.read(buf)) > 0)
75                         sb.append(buf, 0, i);
76                 reader.close();
77                 is.close();
78                 return sb.toString();
79         }
80
81         @Bean
82         // Use the same name as regular configuration
83         public SimpleKubernetesClient ciAuxApi() throws IOException {
84                 SimpleKubernetesClient mockClient = mock(SimpleKubernetesClient.class);
85                 doAnswer(inv -> {
86                         logger.debug("listPods for aux");
87                         return auxPods;
88                 }).when(mockClient).listPods("ricaux");
89                 return mockClient;
90         }
91
92         @Bean
93         // Use the same name as regular configuration
94         public SimpleKubernetesClient ciPltApi() throws IOException {
95                 SimpleKubernetesClient mockClient = mock(SimpleKubernetesClient.class);
96                 doAnswer(inv -> {
97                         String ns = inv.<String>getArgument(0);
98                         logger.debug("listPods for namespace {}", ns);
99                         if ("ricplt".equals(ns))
100                                 return pltPods;
101                         else
102                                 throw new Exception("Fake server failure");
103                 }).when(mockClient).listPods(any(String.class));
104                 return mockClient;
105         }
106
107 }