7a6ead7633b5c1cf78139f7fb1cfc9b495459dab
[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     @Mock
56     private PolicyToscaConverter policyToscaConverter;
57     @Mock
58     private ToscaTemplateProcessor toscaTemplateProcessor;
59     @Mock
60     private ApexConfigProcessor apexConfigProcessor;
61     @Mock
62     private UploadPluginClient uploadPluginClient;
63     @Mock
64     private UploadPluginConfigParameters config;
65
66     @InjectMocks
67     private PolicyUploadHandler policyUploadHandler;
68
69     @Before
70     public void setup() {
71         MockitoAnnotations.initMocks(this);
72         when(config.isEnabled()).thenReturn(true);
73     }
74
75     @Test
76     public void doUploadResponseSuccessAndFail() throws PolicyToscaConverterException, IOException {
77         final ApexModel apexModel = mockApexModel();
78         final ProcessedTemplate processedToscaTemplate = new ProcessedTemplate();
79         processedToscaTemplate.setContent("tosca");
80         final ProcessedTemplate processedApexConfig = new ProcessedTemplate();
81         processedApexConfig.setContent("apexConfig");
82         when(toscaTemplateProcessor.process(any(InputStream.class))).thenReturn(processedToscaTemplate);
83         when(apexConfigProcessor.process(any(InputStream.class))).thenReturn(processedApexConfig);
84         when(policyToscaConverter.convert(eq("policy\n"), eq("apexConfig"), eq("tosca")))
85             .thenReturn(Optional.of("test"));
86         when(uploadPluginClient.upload(any(UploadPolicyRequestDto.class)))
87             .thenReturn(Response.ok().status(201).build());
88
89         ApexApiResult apexApiResult = policyUploadHandler
90             .doUpload(apexModel, mock(InputStream.class), mock(InputStream.class));
91
92         assertThat("Response should be ok", apexApiResult.isOk(), is(true));
93         String expectedSuccessMsg =
94             String.format("Policy '%s' uploaded successfully", apexModel.getPolicyModel().getId());
95         assertThat("Response message should be as expected",
96             apexApiResult.getMessage(), is(expectedSuccessMsg + "\n"));
97
98         when(uploadPluginClient.upload(any(UploadPolicyRequestDto.class)))
99             .thenReturn(Response.serverError().build());
100
101         apexApiResult = policyUploadHandler
102             .doUpload(apexModel, mock(InputStream.class), mock(InputStream.class));
103
104         assertThat("Response should not be ok", apexApiResult.isNok(), is(true));
105         expectedSuccessMsg =
106             String.format("An error has occurred while uploading the Policy '%s'. Status was %s",
107                 apexModel.getPolicyModel().getId(), 500);
108         assertThat("Response message should be as expected",
109             apexApiResult.getMessage(), is(expectedSuccessMsg + "\n"));
110     }
111
112     @Test
113     public void doUploadPluginDisabled() throws IOException {
114         when(config.isEnabled()).thenReturn(false);
115
116         final ProcessedTemplate processedToscaTemplate = new ProcessedTemplate();
117         final ProcessedTemplate processedApexConfig = new ProcessedTemplate();
118         when(toscaTemplateProcessor.process(any(InputStream.class))).thenReturn(processedToscaTemplate);
119         when(apexConfigProcessor.process(any(InputStream.class))).thenReturn(processedApexConfig);
120         final ApexApiResult apexApiResult = policyUploadHandler
121             .doUpload(mock(ApexModel.class), mock(InputStream.class), mock(InputStream.class));
122
123         assertThat("Response should not be ok", apexApiResult.isNok(), is(true));
124         assertThat("Response message should be as expected",
125             apexApiResult.getMessage(), is("Upload feature is disabled\n"));
126     }
127
128     @Test
129     public void doUploadInvalidToscaTemplate() throws IOException {
130         when(config.isEnabled()).thenReturn(false);
131
132         final ProcessedTemplate processedToscaTemplate = new ProcessedTemplate();
133         final String errorMsg = "an error";
134         processedToscaTemplate.addToErrors(Collections.singleton(errorMsg));
135         when(toscaTemplateProcessor.process(any(InputStream.class))).thenReturn(processedToscaTemplate);
136         final ApexApiResult apexApiResult = policyUploadHandler
137             .doUpload(mock(ApexModel.class), mock(InputStream.class), mock(InputStream.class));
138
139         assertThat("Response should not be ok", apexApiResult.isNok(), is(true));
140         assertThat("Response message should be as expected",
141             apexApiResult.getMessage(), is(errorMsg + "\n"));
142     }
143
144     @Test
145     public void doUploadInvalidApexConfigTemplate() throws IOException {
146         when(config.isEnabled()).thenReturn(false);
147
148         when(toscaTemplateProcessor.process(any(InputStream.class))).thenReturn(new ProcessedTemplate());
149         final ProcessedTemplate processedApexConfig = new ProcessedTemplate();
150         final String errorMsg = "an error";
151         processedApexConfig.addToErrors(Collections.singleton(errorMsg));
152         when(apexConfigProcessor.process(any(InputStream.class))).thenReturn(processedApexConfig);
153         final ApexApiResult apexApiResult = policyUploadHandler
154             .doUpload(mock(ApexModel.class), mock(InputStream.class), mock(InputStream.class));
155
156         assertThat("Response should not be ok", apexApiResult.isNok(), is(true));
157         assertThat("Response message should be as expected",
158             apexApiResult.getMessage(), is(errorMsg + "\n"));
159     }
160
161     @Test
162     public void doUploadConversionFailed() throws PolicyToscaConverterException, IOException {
163         final ApexModel apexModel = mockApexModel();
164         final ProcessedTemplate processedToscaTemplate = new ProcessedTemplate();
165         processedToscaTemplate.setContent("tosca");
166         final ProcessedTemplate processedApexConfig = new ProcessedTemplate();
167         processedApexConfig.setContent("apexConfig");
168         when(toscaTemplateProcessor.process(any(InputStream.class))).thenReturn(processedToscaTemplate);
169         when(apexConfigProcessor.process(any(InputStream.class))).thenReturn(processedApexConfig);
170         when(policyToscaConverter.convert(eq("policy\n"), eq("apexConfig"), eq("tosca")))
171             .thenThrow(PolicyToscaConverterException.class);
172         when(uploadPluginClient.upload(any(UploadPolicyRequestDto.class)))
173             .thenReturn(Response.ok().status(201).build());
174
175         final ApexApiResult apexApiResult = policyUploadHandler
176             .doUpload(apexModel, mock(InputStream.class), mock(InputStream.class));
177
178         assertThat("Response should not be ok", apexApiResult.isNok(), is(true));
179         final String expectedErrorMsg = String
180             .format("An error has occurred while uploading the converting the Policy '%s' to YAML.",
181                 apexModel.getPolicyModel().getId());
182         assertThat("Response message should be as expected",
183             apexApiResult.getMessage(), is(expectedErrorMsg + "\n"));
184     }
185
186     private ApexModel mockApexModel() {
187         final ApexModel apexModel = mock(ApexModel.class);
188         final ApexApiResult listModelApexApiResult = new ApexApiResult();
189         listModelApexApiResult.addMessage("policy");
190         when(apexModel.listModel()).thenReturn(listModelApexApiResult);
191         final AxPolicyModel axPolicyModel = new AxPolicyModel();
192         final AxArtifactKey axArtifactKey = new AxArtifactKey("policyKey", "1.0.0");
193         final Map<AxArtifactKey, AxKeyInfo> keyInfoMap = new HashMap<>();
194         keyInfoMap.put(axArtifactKey, new AxKeyInfo(axArtifactKey));
195         final AxKeyInformation axKeyInformation = new AxKeyInformation(axArtifactKey, keyInfoMap);
196         axPolicyModel.setKeyInformation(axKeyInformation);
197         when(apexModel.getPolicyModel()).thenReturn(axPolicyModel);
198         return apexModel;
199     }
200 }