d4a228e4be919568eab59b519ff265ad0a57e1f3
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / config / XappManagerMockConfiguration.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.xappmgr.client.api.HealthApi;
30 import org.oransc.ric.xappmgr.client.api.XappApi;
31 import org.oransc.ric.xappmgr.client.invoker.ApiClient;
32 import org.oransc.ric.xappmgr.client.model.AllXapps;
33 import org.oransc.ric.xappmgr.client.model.SubscriptionRequest;
34 import org.oransc.ric.xappmgr.client.model.SubscriptionResponse;
35 import org.oransc.ric.xappmgr.client.model.XAppInfo;
36 import org.oransc.ric.xappmgr.client.model.Xapp;
37 import org.oransc.ric.xappmgr.client.model.Xapp.StatusEnum;
38 import org.oransc.ric.xappmgr.client.model.XappInstance;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.context.annotation.Bean;
42 import org.springframework.context.annotation.Configuration;
43 import org.springframework.context.annotation.Profile;
44 import org.springframework.http.HttpStatus;
45
46 /**
47  * Creates an implementation of the xApp manager client that answers requests
48  * with mock data.
49  */
50 @Profile("mock")
51 @Configuration
52 public class XappManagerMockConfiguration {
53
54         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
55
56         private final AllXapps allXapps;
57
58         public XappManagerMockConfiguration() {
59                 logger.info("Configuring mock xApp Manager");
60                 Xapp ac = new Xapp().name("Admission Control").version("v3").status(StatusEnum.FAILED);
61                 ac.addInstancesItem(
62                                 new XappInstance().name("cdef-3456").ip("3.4.5.6").port(200).status(XappInstance.StatusEnum.RUNNING));
63                 Xapp an = new Xapp().name("ANR Control").version("v0").status(StatusEnum.SUPERSEDED);
64                 an.addInstancesItem(
65                                 new XappInstance().name("fedc-8765").ip("8.7.6.5").port(400).status(XappInstance.StatusEnum.RUNNING));
66                 Xapp dc = new Xapp().name("Dual Connectivity").version("v2").status(StatusEnum.DELETED);
67                 dc.addInstancesItem(
68                                 new XappInstance().name("def0-6789").ip("6.7.8.9").port(300).status(XappInstance.StatusEnum.COMPLETED));
69                 allXapps = new AllXapps();
70                 allXapps.add(ac);
71                 allXapps.add(an);
72                 allXapps.add(dc);
73         }
74
75         @Bean
76         public HealthApi xappHealthMockApi() {
77                 ApiClient mockClient = mock(ApiClient.class);
78                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
79                 HealthApi mockApi = mock(HealthApi.class);
80                 when(mockApi.getApiClient()).thenReturn(mockClient);
81                 doAnswer(i -> {
82                         return null;
83                 }).when(mockApi).getHealth();
84                 return mockApi;
85         }
86
87         @Bean
88         public XappApi xappMgrMockApi() {
89                 ApiClient mockClient = mock(ApiClient.class);
90                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
91
92                 XappApi mockApi = mock(XappApi.class);
93                 when(mockApi.getApiClient()).thenReturn(mockClient);
94
95                 SubscriptionResponse subRes = new SubscriptionResponse().eventType(SubscriptionResponse.EventTypeEnum.ALL)
96                                 .id("subid").version(1);
97                 when(mockApi.addSubscription(any(SubscriptionRequest.class))).thenReturn(subRes);
98
99                 doAnswer(i -> {
100                         return null;
101                 }).when(mockApi).deleteSubscription(any(Integer.class));
102
103                 when(mockApi.deployXapp(any(XAppInfo.class))).thenReturn(new Xapp());
104
105                 when(mockApi.getAllXapps()).thenReturn(allXapps);
106
107                 Xapp xappByName = new Xapp().name("name").status(StatusEnum.UNKNOWN).version("v1");
108                 when(mockApi.getXappByName(any(String.class))).thenReturn(xappByName);
109
110                 doAnswer(i -> {
111                         return null;
112                 }).when(mockApi).undeployXapp(any(String.class));
113
114                 return mockApi;
115         }
116
117 }