2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2022 - Samsung Electronics. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.so.openstack.utils;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.doThrow;
32 import java.io.IOException;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.onap.so.BaseTest;
37 import org.onap.so.openstack.exceptions.MsoAdapterException;
38 import org.onap.so.openstack.exceptions.MsoException;
39 import org.onap.so.openstack.exceptions.MsoExceptionCategory;
40 import org.onap.so.openstack.exceptions.MsoIOException;
41 import org.onap.so.openstack.exceptions.MsoOpenstackException;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.beans.factory.annotation.Qualifier;
44 import com.fasterxml.jackson.core.JsonParseException;
45 import com.fasterxml.jackson.databind.JsonMappingException;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.woorea.openstack.base.client.OpenStackBaseException;
48 import com.woorea.openstack.base.client.OpenStackConnectException;
49 import com.woorea.openstack.base.client.OpenStackRequest;
50 import com.woorea.openstack.base.client.OpenStackResponse;
51 import com.woorea.openstack.base.client.OpenStackResponseException;
52 import com.woorea.openstack.heat.model.Explanation;
53 import com.woorea.openstack.keystone.model.Error;
54 import com.woorea.openstack.quantum.model.NeutronError;
57 * This class implements test methods of the MsoCommonUtils
59 public class MsoCommonUtilsTest extends BaseTest {
61 @Qualifier("CommonUtils")
62 private MsoCommonUtils commonUtils;
65 private OpenStackRequest openstackRequest;
68 public final void testExecuteAndRecordOpenstackRequest() {
69 Mockito.when(openstackRequest.endpoint()).thenReturn("localhost");
70 Mockito.when(openstackRequest.path()).thenReturn("/test");
71 // TODO:Must try a real connection
72 assertNull(commonUtils.executeAndRecordOpenstackRequest(openstackRequest));
73 assertNull(commonUtils.executeAndRecordOpenstackRequest(openstackRequest, true));
77 public void testexecuteAndRecordOpenstackRequestResponseException() {
78 expectedException.expect(OpenStackResponseException.class);
80 doThrow(OpenStackResponseException.class).when(openstackRequest).execute();
82 commonUtils.executeAndRecordOpenstackRequest(openstackRequest);
83 commonUtils.executeAndRecordOpenstackRequest(openstackRequest, true);
87 public void testexecuteAndRecordOpenstackRequestConnectException() {
88 expectedException.expect(OpenStackConnectException.class);
90 doThrow(OpenStackConnectException.class).when(openstackRequest).execute();
92 commonUtils.executeAndRecordOpenstackRequest(openstackRequest, true);
96 public final void testKeystoneErrorToMsoException() throws JsonParseException, JsonMappingException, IOException {
97 OpenStackBaseException openStackConnectException = new OpenStackConnectException("connect");
99 OpenStackBaseException openStackResponseException = new OpenStackResponseException("response", 1);
101 MsoException me = commonUtils.keystoneErrorToMsoException(openStackConnectException, "ContextError");
103 assertTrue(me instanceof MsoIOException);
104 assertEquals("connect", me.getMessage());
107 MsoException me2 = commonUtils.keystoneErrorToMsoException(openStackResponseException, "ContextError");
108 assertTrue(me2 instanceof MsoOpenstackException);
109 assertEquals("ContextError", me2.getContext());
110 assertEquals(MsoExceptionCategory.OPENSTACK, me2.getCategory());
113 OpenStackResponse openStackResponse = Mockito.mock(OpenStackResponse.class);
114 Error error = mapper.readValue(new File(RESOURCE_PATH + "Error.json"), Error.class);
116 doReturn(error).when(openStackResponse).getErrorEntity(Error.class);
118 openStackResponseException = new OpenStackResponseException("response", 501, openStackResponse);
120 MsoException me3 = commonUtils.keystoneErrorToMsoException(openStackResponseException, "ContextError");
122 assertTrue(me3 instanceof MsoOpenstackException);
123 assertEquals("1 title: message", me3.toString());
127 public final void testHeatExceptionToMsoException() throws JsonParseException, JsonMappingException, IOException {
128 OpenStackBaseException openStackConnectException = new OpenStackConnectException("connect");
130 OpenStackBaseException openStackResponseException = new OpenStackResponseException("response", 1);
132 MsoException me = commonUtils.heatExceptionToMsoException(openStackConnectException, "ContextError");
134 assertTrue(me instanceof MsoIOException);
135 assertEquals("connect", me.getMessage());
138 MsoException me2 = commonUtils.heatExceptionToMsoException(openStackResponseException, "ContextError");
139 assertTrue(me2 instanceof MsoOpenstackException);
140 assertEquals("ContextError", me2.getContext());
141 assertEquals(MsoExceptionCategory.OPENSTACK, me2.getCategory());
144 OpenStackResponse openStackResponse = Mockito.mock(OpenStackResponse.class);
145 Explanation explanation = mapper.readValue(new File(RESOURCE_PATH + "Explanation.json"), Explanation.class);
147 doReturn(explanation).when(openStackResponse).getErrorEntity(Explanation.class);
149 openStackResponseException = new OpenStackResponseException("response", 501, openStackResponse);
151 MsoException me3 = commonUtils.heatExceptionToMsoException(openStackResponseException, "ContextError");
153 assertTrue(me3 instanceof MsoOpenstackException);
154 assertEquals("1 title: explanation, error.type=null, error.message=null", me3.toString());
158 public final void testNeutronExceptionToMsoException()
159 throws JsonParseException, JsonMappingException, IOException {
160 OpenStackBaseException openStackConnectException = new OpenStackConnectException("connect");
162 OpenStackBaseException openStackResponseException = new OpenStackResponseException("response", 1);
164 MsoException me = commonUtils.neutronExceptionToMsoException(openStackConnectException, "ContextError");
166 assertTrue(me instanceof MsoIOException);
167 assertEquals("connect", me.getMessage());
169 MsoException me2 = commonUtils.neutronExceptionToMsoException(openStackResponseException, "ContextError");
170 assertTrue(me2 instanceof MsoOpenstackException);
171 assertEquals("ContextError", me2.getContext());
172 assertEquals(MsoExceptionCategory.OPENSTACK, me2.getCategory());
175 OpenStackResponse openStackResponse = Mockito.mock(OpenStackResponse.class);
176 NeutronError explanation = mapper.readValue(new File(RESOURCE_PATH + "NeutronError.json"), NeutronError.class);
178 doReturn(explanation).when(openStackResponse).getErrorEntity(NeutronError.class);
180 openStackResponseException = new OpenStackResponseException("response", 501, openStackResponse);
182 MsoException me3 = commonUtils.neutronExceptionToMsoException(openStackResponseException, "ContextError");
184 assertTrue(me3 instanceof MsoOpenstackException);
185 assertEquals("501 type: message", me3.toString());
189 public final void testRuntimeExceptionToMsoException() {
190 RuntimeException re = new RuntimeException("runtime");
191 MsoException me = commonUtils.runtimeExceptionToMsoException(re, "ContextError");
193 assertTrue(me instanceof MsoAdapterException);
194 assertEquals("ContextError", me.getContext());
195 assertEquals(MsoExceptionCategory.INTERNAL, me.getCategory());
199 public void testIoExceptionToMsoException() {
200 IOException exception = new IOException("IOExceptionTestMessage");
202 MsoException msoException = commonUtils.ioExceptionToMsoException(exception, "ContextError");
204 assertTrue(msoException instanceof MsoAdapterException);
205 assertEquals("ContextError", msoException.getContext());
206 assertEquals(MsoExceptionCategory.INTERNAL, msoException.getCategory());