Non-functional changes to silence Sonar
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / config / AppManagerMockConfiguration.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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.lang.invoke.MethodHandles;
28
29 import org.oransc.ric.plt.appmgr.client.api.HealthApi;
30 import org.oransc.ric.plt.appmgr.client.api.XappApi;
31 import org.oransc.ric.plt.appmgr.client.invoker.ApiClient;
32 import org.oransc.ric.plt.appmgr.client.model.AllDeployableXapps;
33 import org.oransc.ric.plt.appmgr.client.model.AllDeployedXapps;
34 import org.oransc.ric.plt.appmgr.client.model.AllXappConfig;
35 import org.oransc.ric.plt.appmgr.client.model.ConfigMetadata;
36 import org.oransc.ric.plt.appmgr.client.model.SubscriptionRequest;
37 import org.oransc.ric.plt.appmgr.client.model.SubscriptionResponse;
38 import org.oransc.ric.plt.appmgr.client.model.XAppConfig;
39 import org.oransc.ric.plt.appmgr.client.model.XAppInfo;
40 import org.oransc.ric.plt.appmgr.client.model.Xapp;
41 import org.oransc.ric.plt.appmgr.client.model.Xapp.StatusEnum;
42 import org.oransc.ric.plt.appmgr.client.model.XappInstance;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.context.annotation.Bean;
46 import org.springframework.context.annotation.Configuration;
47 import org.springframework.context.annotation.Profile;
48 import org.springframework.http.HttpStatus;
49
50 /**
51  * Creates an implementation of the xApp manager client that answers requests
52  * with mock data.
53  */
54 @Profile("mock")
55 @Configuration
56 public class AppManagerMockConfiguration {
57
58         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59
60         private final AllDeployableXapps availXapps;
61         private final AllDeployedXapps deployedXapps;
62         private final AllXappConfig allXappConfigs;
63
64         public AppManagerMockConfiguration() {
65                 logger.info("Configuring mock xApp Manager");
66                 final String[] appNames = { "AdmissionControl", "Automatic Neighbor Relation", "Dual Connectivity" };
67                 final String configJson = " { \"config\" : \"example\" }";
68                 final String descriptorJson = " { \"descriptor\" : \"example\" }";
69                 allXappConfigs = new AllXappConfig();
70                 availXapps = new AllDeployableXapps();
71                 deployedXapps = new AllDeployedXapps();
72                 for (String n : appNames) {
73                         ConfigMetadata metadata = new ConfigMetadata().configName("config-" + n).name(n).namespace("namespace");
74                         XAppConfig config = new XAppConfig().config(configJson).descriptor(descriptorJson).metadata(metadata);
75                         allXappConfigs.add(config);
76                         availXapps.add(n);
77                         Xapp xapp = new Xapp().name(n).version("version").status(StatusEnum.UNKNOWN);
78                         xapp.addInstancesItem(new XappInstance().name("abcd-1234").ip("127.0.0.1").port(200)
79                                         .status(XappInstance.StatusEnum.RUNNING));
80                         deployedXapps.add(xapp);
81                 }
82         }
83
84         @Bean
85         // Use the same name as regular configuration
86         public HealthApi xappMgrHealthApi() {
87                 ApiClient mockClient = mock(ApiClient.class);
88                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
89                 HealthApi mockApi = mock(HealthApi.class);
90                 when(mockApi.getApiClient()).thenReturn(mockClient);
91                 doAnswer(i -> null).when(mockApi).getHealthAlive();
92                 doAnswer(i -> null).when(mockApi).getHealthReady();
93                 return mockApi;
94         }
95
96         @Bean
97         // Use the same name as regular configuration
98         public XappApi xappMgrXappApi() {
99                 ApiClient mockClient = mock(ApiClient.class);
100                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
101                 XappApi mockApi = mock(XappApi.class);
102                 when(mockApi.getApiClient()).thenReturn(mockClient);
103                 when(mockApi.getAllXappConfig()).thenReturn(allXappConfigs);
104                 when(mockApi.createXappConfig(any(XAppConfig.class))).thenReturn(allXappConfigs.get(0));
105                 when(mockApi.modifyXappConfig(any(XAppConfig.class))).thenReturn(allXappConfigs.get(0));
106                 doAnswer(i -> null).when(mockApi).deleteXappConfig(any(ConfigMetadata.class));
107                 when(mockApi.deployXapp(any(XAppInfo.class))).thenReturn(deployedXapps.get(0));
108                 when(mockApi.listAllXapps()).thenReturn(availXapps);
109                 when(mockApi.getAllXapps()).thenReturn(deployedXapps);
110                 when(mockApi.getXappByName(any(String.class))).thenReturn(deployedXapps.get(0));
111                 doAnswer(i -> null).when(mockApi).undeployXapp(any(String.class));
112                 SubscriptionResponse subRes = new SubscriptionResponse().eventType(SubscriptionResponse.EventTypeEnum.ALL)
113                                 .id("subid").version(1);
114                 when(mockApi.addSubscription(any(SubscriptionRequest.class))).thenReturn(subRes);
115                 doAnswer(i -> null).when(mockApi).deleteSubscription(any(String.class));
116                 return mockApi;
117         }
118
119 }