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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.policy.gui.editors.apex.rest.handling.converter.tosca;
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;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.Collections;
32 import java.util.HashMap;
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;
53 public class PolicyUploadHandlerTest {
56 private PolicyToscaConverter policyToscaConverter;
58 private ToscaTemplateProcessor toscaTemplateProcessor;
60 private ApexConfigProcessor apexConfigProcessor;
62 private UploadPluginClient uploadPluginClient;
64 private UploadPluginConfigParameters config;
67 private PolicyUploadHandler policyUploadHandler;
71 MockitoAnnotations.initMocks(this);
72 when(config.isEnabled()).thenReturn(true);
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());
89 ApexApiResult apexApiResult = policyUploadHandler
90 .doUpload(apexModel, mock(InputStream.class), mock(InputStream.class));
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"));
98 when(uploadPluginClient.upload(any(UploadPolicyRequestDto.class)))
99 .thenReturn(Response.serverError().build());
101 apexApiResult = policyUploadHandler
102 .doUpload(apexModel, mock(InputStream.class), mock(InputStream.class));
104 assertThat("Response should not be ok", apexApiResult.isNok(), is(true));
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"));
113 public void doUploadPluginDisabled() throws IOException {
114 when(config.isEnabled()).thenReturn(false);
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));
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"));
129 public void doUploadInvalidToscaTemplate() throws IOException {
130 when(config.isEnabled()).thenReturn(false);
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));
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"));
145 public void doUploadInvalidApexConfigTemplate() throws IOException {
146 when(config.isEnabled()).thenReturn(false);
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));
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"));
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());
175 final ApexApiResult apexApiResult = policyUploadHandler
176 .doUpload(apexModel, mock(InputStream.class), mock(InputStream.class));
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"));
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);