869889309d9651435b62212bc7d13ea9ccbbf3c0
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / config / XappManagerMockConfiguration.java
1 /*-
2  * ========================LICENSE_START=================================
3  * ORAN-OSC
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.DefaultApi;
30 import org.oransc.ric.xappmgr.client.invoker.ApiClient;
31 import org.oransc.ric.xappmgr.client.model.AllXapps;
32 import org.oransc.ric.xappmgr.client.model.SubscriptionRequest;
33 import org.oransc.ric.xappmgr.client.model.SubscriptionResponse;
34 import org.oransc.ric.xappmgr.client.model.XAppInfo;
35 import org.oransc.ric.xappmgr.client.model.Xapp;
36 import org.oransc.ric.xappmgr.client.model.Xapp.StatusEnum;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.context.annotation.Bean;
40 import org.springframework.context.annotation.Configuration;
41 import org.springframework.context.annotation.Profile;
42 import org.springframework.http.HttpStatus;
43
44 /**
45  * Creates an implementation of the xApp manager client that answers requests
46  * with mock data.
47  */
48 @Profile("mock")
49 @Configuration
50 public class XappManagerMockConfiguration {
51
52         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
53
54         private final AllXapps allXapps;
55
56         public XappManagerMockConfiguration() {
57                 logger.info("Configuring mock xApp Manager");
58                 allXapps = new AllXapps();
59                 allXapps.add(new Xapp().name("Pendulum Control").version("v1").status(StatusEnum.DEPLOYED));
60                 allXapps.add(new Xapp().name("Dual Connectivity").version("v2").status(StatusEnum.DELETED));
61                 allXapps.add(new Xapp().name("Admission Control").version("v3").status(StatusEnum.FAILED));
62                 allXapps.add(new Xapp().name("ANR Control").version("v0").status(StatusEnum.SUPERSEDED));
63         }
64
65         @Bean
66         public DefaultApi xappManagerMockClient() {
67                 ApiClient mockClient = mock(ApiClient.class);
68                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
69
70                 DefaultApi mockApi = mock(DefaultApi.class);
71                 when(mockApi.getApiClient()).thenReturn(mockClient);
72
73                 SubscriptionResponse subRes = new SubscriptionResponse().eventType(SubscriptionResponse.EventTypeEnum.ALL)
74                                 .id("subid").version(1);
75                 when(mockApi.addSubscription(any(SubscriptionRequest.class))).thenReturn(subRes);
76
77                 doAnswer(i -> {
78                         return null;
79                 }).when(mockApi).deleteSubscription(any(Integer.class));
80
81                 when(mockApi.deployXapp(any(XAppInfo.class))).thenReturn(new Xapp());
82
83                 when(mockApi.getAllXapps()).thenReturn(allXapps);
84
85                 doAnswer(i -> {
86                         return null;
87                 }).when(mockApi).getHealth();
88
89                 Xapp xappByName = new Xapp().name("name").status(StatusEnum.UNKNOWN).version("v1");
90                 when(mockApi.getXappByName(any(String.class))).thenReturn(xappByName);
91
92                 doAnswer(i -> {
93                         return null;
94                 }).when(mockApi).undeployXapp(any(String.class));
95
96                 return mockApi;
97         }
98
99 }