Configure "portal app_password" in DB by environment variable
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controller / ErrorReportControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
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.onap.vid.controller;
21
22 import com.google.common.collect.ImmutableMap;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.onap.vid.model.errorReport.ReportCreationParameters;
30 import org.onap.vid.reports.BasicReportGenerator;
31 import org.onap.vid.reports.DeploymentReportGenerator;
32
33 import javax.servlet.http.HttpServletRequest;
34
35 import java.util.Arrays;
36 import java.util.HashMap;
37 import java.util.Map;
38
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class ErrorReportControllerTest {
45
46         private BasicReportGenerator basicReportGenerator;
47         private DeploymentReportGenerator deploymentReportGenerator;
48         private HttpServletRequest httpServletRequest;
49
50         private ErrorReportController errorReportController;
51
52         @Before
53         public void setUp() {
54                 basicReportGenerator = mock(BasicReportGenerator.class);
55                 deploymentReportGenerator = mock(DeploymentReportGenerator.class);
56                 httpServletRequest = mock(HttpServletRequest.class);
57
58                 errorReportController
59                                 = new ErrorReportController(Arrays.asList(basicReportGenerator, deploymentReportGenerator));
60         }
61
62         @Test
63         public void shouldGenerateBasicReportDataWhenNoParameters() {
64                 //given
65                 ReportCreationParameters parameters = new ReportCreationParameters();
66
67                 ImmutableMap<String, Object> expectedReport = ImmutableMap.<String, Object>builder()
68                                                 .put("X-ECOMP-RequestID", "request_id")
69                                                 .put("X-ECOMP-RequestID1", "request_id1")
70                                                 .put("X-ECOMP-RequestID2", "request_id2")
71                                                 .put("X-ECOMP-RequestID3", "request_id3")
72                                                 .build();
73
74                 when(basicReportGenerator.apply(httpServletRequest, parameters)).thenReturn(expectedReport);
75                 when(basicReportGenerator.canGenerate(parameters)).thenReturn(true);
76                 when(deploymentReportGenerator.canGenerate(parameters)).thenReturn(false);
77
78                 //when
79                 Map<String, Object> actualReport = errorReportController.generateReportsData(httpServletRequest, parameters);
80
81                 //then
82                 assertThat(actualReport).isEqualTo(expectedReport);
83         }
84
85         @Test
86         public void shouldGenerateDeploymentReportDataWhenSpecificParameters() {
87                 //given
88                 ReportCreationParameters parameters =
89                                 new ReportCreationParameters("request_id", "service_uuid");
90
91                 ImmutableMap<String, Object> basicReport = ImmutableMap.<String, Object>builder()
92                                 .put("X-ECOMP-RequestID", "request_id")
93                                 .put("X-ECOMP-RequestID1", "request_id1")
94                                 .build();
95
96                 ImmutableMap<String, Object> extendedReport = ImmutableMap.<String, Object>builder()
97                                 .put("serviceInstanceInfo", "serviceInstanceInfoVal")
98                                 .put("serviceInstanceInfo1", "serviceInstanceInfoVal1")
99                                 .build();
100
101                 HashMap<String, Object> expectedReport = new HashMap<>(basicReport);
102                 expectedReport.putAll(extendedReport);
103
104                 when(basicReportGenerator.apply(httpServletRequest, parameters)).thenReturn(basicReport);
105                 when(deploymentReportGenerator.apply(httpServletRequest, parameters)).thenReturn(extendedReport);
106                 when(basicReportGenerator.canGenerate(parameters)).thenReturn(true);
107                 when(deploymentReportGenerator.canGenerate(parameters)).thenReturn(true);
108
109                 //when
110                 Map<String, Object> actualReport = errorReportController.generateReportsData(httpServletRequest, parameters);
111
112                 //then
113                 assertThat(actualReport).isEqualTo(expectedReport);
114         }
115 }