6ec5eefd5cfd113ec6b77dae390e53cba1fbe152
[policy/gui.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.gui.editors.apex.rest.handling.converter.tosca;
21
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.is;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.eq;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.Map;
34 import java.util.Optional;
35 import javax.ws.rs.core.Response;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.InjectMocks;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation;
44 import org.onap.policy.apex.model.modelapi.ApexApiResult;
45 import org.onap.policy.apex.model.modelapi.ApexModel;
46 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
47 import org.onap.policy.gui.editors.apex.rest.UploadPluginConfigParameters;
48 import org.onap.policy.gui.editors.apex.rest.handling.PolicyUploadHandler;
49 import org.onap.policy.gui.editors.apex.rest.handling.converter.tosca.exception.PolicyToscaConverterException;
50 import org.onap.policy.gui.editors.apex.rest.handling.plugin.upload.UploadPluginClient;
51 import org.onap.policy.gui.editors.apex.rest.handling.plugin.upload.UploadPolicyRequestDto;
52
53 public class PolicyUploadHandlerTest {
54
55     private static final String USER_ID = "cs0008";
56     @Mock
57     private PolicyToscaConverter policyToscaConverter;
58     @Mock
59     private ToscaTemplateProcessor toscaTemplateProcessor;
60     @Mock
61     private ApexConfigProcessor apexConfigProcessor;
62     @Mock
63     private UploadPluginClient uploadPluginClient;
64     @Mock
65     private UploadPluginConfigParameters config;
66
67     @InjectMocks
68     private PolicyUploadHandler policyUploadHandler;
69
70     @Before
71     public void setup() {
72         MockitoAnnotations.initMocks(this);
73         when(config.isEnabled()).thenReturn(true);
74     }
75
76     @Test
77     public void doUploadResponseSuccessAndFail() throws PolicyToscaConverterException, IOException {
78         final ApexModel apexModel = mockApexModel();
79         final ProcessedTemplate processedToscaTemplate = new ProcessedTemplate();
80         processedToscaTemplate.setContent("tosca");
81         final ProcessedTemplate processedApexConfig = new ProcessedTemplate();
82         processedApexConfig.setContent("apexConfig");
83         when(toscaTemplateProcessor.process(any(InputStream.class))).thenReturn(processedToscaTemplate);
84         when(apexConfigProcessor.process(any(InputStream.class))).thenReturn(processedApexConfig);
85         when(policyToscaConverter.convert(eq("policy\n"), eq("apexConfig"), eq("tosca")))
86             .thenReturn(Optional.of("test"));
87         when(uploadPluginClient.upload(any(UploadPolicyRequestDto.class)))
88             .thenReturn(Response.ok().status(201).build());
89
90         ApexApiResult apexApiResult = policyUploadHandler
91             .doUpload(apexModel, mock(InputStream.class), mock(InputStream.class), USER_ID);
92
93         assertThat("Response should be ok", apexApiResult.isOk(), is(true));
94         String expectedSuccessMsg =
95             String.format("Policy '%s' uploaded successfully", apexModel.getPolicyModel().getId());
96         assertThat("Response message should be as expected",
97             apexApiResult.getMessage(), is(expectedSuccessMsg + "\n"));
98
99         when(uploadPluginClient.upload(any(UploadPolicyRequestDto.class)))
100             .thenReturn(Response.serverError().build());
101
102         apexApiResult = policyUploadHandler
103             .doUpload(apexModel, mock(InputStream.class), mock(InputStream.class), USER_ID);
104
105         assertThat("Response should not be ok", apexApiResult.isNok(), is(true));
106         expectedSuccessMsg =
107             String.format("An error has occurred while uploading the Policy '%s'. Status was %s",
108                 apexModel.getPolicyModel().getId(), 500);
109         assertThat("Response message should be as expected",
110             apexApiResult.getMessage(), is(expectedSuccessMsg + "\n"));
111     }
112
113     @Test
114     public void doUploadPluginDisabled() throws IOException {
115         when(config.isEnabled()).thenReturn(false);
116
117         final ProcessedTemplate processedToscaTemplate = new ProcessedTemplate();
118         final ProcessedTemplate processedApexConfig = new ProcessedTemplate();
119         when(toscaTemplateProcessor.process(any(InputStream.class))).thenReturn(processedToscaTemplate);
120         when(apexConfigProcessor.process(any(InputStream.class))).thenReturn(processedApexConfig);
121         final ApexApiResult apexApiResult = policyUploadHandler
122             .doUpload(mock(ApexModel.class), mock(InputStream.class), mock(InputStream.class), USER_ID);
123
124         assertThat("Response should not be ok", apexApiResult.isNok(), is(true));
125         assertThat("Response message should be as expected",
126             apexApiResult.getMessage(), is("Upload feature is disabled\n"));
127     }
128
129     @Test
130     public void doUploadInvalidToscaTemplate() throws IOException {
131         when(config.isEnabled()).thenReturn(false);
132
133         final ProcessedTemplate processedToscaTemplate = new ProcessedTemplate();
134         final String errorMsg = "an error";
135         processedToscaTemplate.addToErrors(Collections.singleton(errorMsg));
136         when(toscaTemplateProcessor.process(any(InputStream.class))).thenReturn(processedToscaTemplate);
137         final ApexApiResult apexApiResult = policyUploadHandler
138             .doUpload(mock(ApexModel.class), mock(InputStream.class), mock(InputStream.class), USER_ID);
139
140         assertThat("Response should not be ok", apexApiResult.isNok(), is(true));
141         assertThat("Response message should be as expected",
142             apexApiResult.getMessage(), is(errorMsg + "\n"));
143     }
144
145     @Test
146     public void doUploadInvalidApexConfigTemplate() throws IOException {
147         when(config.isEnabled()).thenReturn(false);
148
149         when(toscaTemplateProcessor.process(any(InputStream.class))).thenReturn(new ProcessedTemplate());
150         final ProcessedTemplate processedApexConfig = new ProcessedTemplate();
151         final String errorMsg = "an error";
152         processedApexConfig.addToErrors(Collections.singleton(errorMsg));
153         when(apexConfigProcessor.process(any(InputStream.class))).thenReturn(processedApexConfig);
154         final ApexApiResult apexApiResult = policyUploadHandler
155             .doUpload(mock(ApexModel.class), mock(InputStream.class), mock(InputStream.class), USER_ID);
156
157         assertThat("Response should not be ok", apexApiResult.isNok(), is(true));
158         assertThat("Response message should be as expected",
159             apexApiResult.getMessage(), is(errorMsg + "\n"));
160     }
161
162     @Test
163     public void doUploadConversionFailed() throws PolicyToscaConverterException, IOException {
164         final ApexModel apexModel = mockApexModel();
165         final ProcessedTemplate processedToscaTemplate = new ProcessedTemplate();
166         processedToscaTemplate.setContent("tosca");
167         final ProcessedTemplate processedApexConfig = new ProcessedTemplate();
168         processedApexConfig.setContent("apexConfig");
169         when(toscaTemplateProcessor.process(any(InputStream.class))).thenReturn(processedToscaTemplate);
170         when(apexConfigProcessor.process(any(InputStream.class))).thenReturn(processedApexConfig);
171         when(policyToscaConverter.convert(eq("policy\n"), eq("apexConfig"), eq("tosca")))
172             .thenThrow(PolicyToscaConverterException.class);
173         when(uploadPluginClient.upload(any(UploadPolicyRequestDto.class)))
174             .thenReturn(Response.ok().status(201).build());
175
176         final ApexApiResult apexApiResult = policyUploadHandler
177             .doUpload(apexModel, mock(InputStream.class), mock(InputStream.class), USER_ID);
178
179         assertThat("Response should not be ok", apexApiResult.isNok(), is(true));
180         final String expectedErrorMsg = String
181             .format("An error has occurred while uploading the converting the Policy '%s' to YAML.",
182                 apexModel.getPolicyModel().getId());
183         assertThat("Response message should be as expected",
184             apexApiResult.getMessage(), is(expectedErrorMsg + "\n"));
185     }
186
187     private ApexModel mockApexModel() {
188         final ApexModel apexModel = mock(ApexModel.class);
189         final ApexApiResult listModelApexApiResult = new ApexApiResult();
190         listModelApexApiResult.addMessage("policy");
191         when(apexModel.listModel()).thenReturn(listModelApexApiResult);
192         final AxPolicyModel axPolicyModel = new AxPolicyModel();
193         final AxArtifactKey axArtifactKey = new AxArtifactKey("policyKey", "1.0.0");
194         final Map<AxArtifactKey, AxKeyInfo> keyInfoMap = new HashMap<>();
195         keyInfoMap.put(axArtifactKey, new AxKeyInfo(axArtifactKey));
196         final AxKeyInformation axKeyInformation = new AxKeyInformation(axArtifactKey, keyInfoMap);
197         axPolicyModel.setKeyInformation(axKeyInformation);
198         when(apexModel.getPolicyModel()).thenReturn(axPolicyModel);
199         return apexModel;
200     }
201 }