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