Upgrade ANR API to version 0.0.7
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / config / AnrXappMockConfiguration.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.ArgumentMatchers.eq;
24 import static org.mockito.ArgumentMatchers.isNull;
25 import static org.mockito.ArgumentMatchers.startsWith;
26 import static org.mockito.Mockito.doAnswer;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.lang.invoke.MethodHandles;
31
32 import org.oransc.ric.anrxapp.client.api.HealthApi;
33 import org.oransc.ric.anrxapp.client.api.NcrtApi;
34 import org.oransc.ric.anrxapp.client.invoker.ApiClient;
35 import org.oransc.ric.anrxapp.client.model.NeighborCellRelation;
36 import org.oransc.ric.anrxapp.client.model.NeighborCellRelationMod;
37 import org.oransc.ric.anrxapp.client.model.NeighborCellRelationTable;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.context.annotation.Bean;
41 import org.springframework.context.annotation.Configuration;
42 import org.springframework.context.annotation.Profile;
43 import org.springframework.http.HttpStatus;
44
45 /**
46  * Creates a mock implementation of the ANR xApp client APIs.
47  */
48 @Profile("mock")
49 @Configuration
50 public class AnrXappMockConfiguration {
51
52         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
53
54         private final NeighborCellRelationTable ncrt, ncrtNodeB1, ncrtNodeB2;
55
56         public AnrXappMockConfiguration() {
57                 logger.info("Configuring mock ANR xApp client");
58                 ncrtNodeB1 = new NeighborCellRelationTable();
59                 ncrtNodeB2 = new NeighborCellRelationTable();
60                 ncrt = new NeighborCellRelationTable();
61                 String[] cells1 = { "A", "B", "C", "D" };
62                 for (String s : cells1)
63                         ncrtNodeB1.addNcrtRelationsItem(
64                                         new NeighborCellRelation().servingCellNrcgi(s + "12345").neighborCellNrpci(s + "12346")
65                                                         .neighborCellNrcgi(s + "12347").flagNoHo(true).flagNoXn(true).flagNoRemove(true));
66                 String[] cells2 = { "E", "F", "G", "H" };
67                 for (String s : cells2)
68                         ncrtNodeB2.addNcrtRelationsItem(
69                                         new NeighborCellRelation().servingCellNrcgi(s + "12345").neighborCellNrpci(s + "12346")
70                                                         .neighborCellNrcgi(s + "12347").flagNoHo(true).flagNoXn(true).flagNoRemove(true));
71                 for (NeighborCellRelation ncr : ncrtNodeB1.getNcrtRelations())
72                         ncrt.addNcrtRelationsItem(ncr);
73                 for (NeighborCellRelation ncr : ncrtNodeB2.getNcrtRelations())
74                         ncrt.addNcrtRelationsItem(ncr);
75         }
76
77         private ApiClient apiClient() {
78                 ApiClient mockClient = mock(ApiClient.class);
79                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
80                 return mockClient;
81         }
82
83         @Bean
84         public HealthApi anrHealthMockApi() {
85                 ApiClient mockClient = mock(ApiClient.class);
86                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
87                 HealthApi mockApi = mock(HealthApi.class);
88                 when(mockApi.getApiClient()).thenReturn(mockClient);
89                 doAnswer(i -> {
90                         return null;
91                 }).when(mockApi).getHealthAlive();
92                 doAnswer(i -> {
93                         return null;
94                 }).when(mockApi).getHealthReady();
95                 return mockApi;
96         }
97
98         @Bean
99         public NcrtApi ncrtMockApi() {
100                 ApiClient apiClient = apiClient();
101                 NcrtApi mockApi = mock(NcrtApi.class);
102                 when(mockApi.getApiClient()).thenReturn(apiClient);
103                 // Swagger sends nulls; front end sends empty strings
104                 when(mockApi.getNcrt((String) isNull(), (String) isNull(), (String) isNull())).thenReturn(ncrt);
105                 when(mockApi.getNcrt(eq(""), any(String.class), any(String.class))).thenReturn(ncrt);
106                 when(mockApi.getNcrt(startsWith("A"), any(String.class), any(String.class))).thenReturn(ncrtNodeB1);
107                 when(mockApi.getNcrt(startsWith("B"), any(String.class), any(String.class))).thenReturn(ncrtNodeB2);
108                 doAnswer(i -> {
109                         return null;
110                 }).when(mockApi).deleteNcrt(any(String.class), any(String.class));
111                 doAnswer(i -> {
112                         return null;
113                 }).when(mockApi).modifyNcrt(any(String.class), any(String.class), any(NeighborCellRelationMod.class));
114                 return mockApi;
115         }
116
117 }