2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.openstack.utils;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.doThrow;
32 import java.io.IOException;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.onap.so.BaseTest;
38 import org.onap.so.openstack.exceptions.MsoAdapterException;
39 import org.onap.so.openstack.exceptions.MsoException;
40 import org.onap.so.openstack.exceptions.MsoExceptionCategory;
41 import org.onap.so.openstack.exceptions.MsoIOException;
42 import org.onap.so.openstack.exceptions.MsoOpenstackException;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.beans.factory.annotation.Qualifier;
46 import com.fasterxml.jackson.core.JsonParseException;
47 import com.fasterxml.jackson.databind.JsonMappingException;
48 import com.fasterxml.jackson.databind.ObjectMapper;
49 import com.woorea.openstack.base.client.OpenStackBaseException;
50 import com.woorea.openstack.base.client.OpenStackConnectException;
51 import com.woorea.openstack.base.client.OpenStackRequest;
52 import com.woorea.openstack.base.client.OpenStackResponse;
53 import com.woorea.openstack.base.client.OpenStackResponseException;
54 import com.woorea.openstack.heat.model.Explanation;
55 import com.woorea.openstack.keystone.model.Error;
56 import com.woorea.openstack.quantum.model.NeutronError;
59 * This class implements test methods of the MsoCommonUtils
61 public class MsoCommonUtilsTest extends BaseTest {
63 @Qualifier("CommonUtils")
64 private MsoCommonUtils commonUtils;
67 private OpenStackRequest openstackRequest;
70 public final void testExecuteAndRecordOpenstackRequest() {
71 Mockito.when(openstackRequest.endpoint()).thenReturn("localhost");
72 Mockito.when(openstackRequest.path()).thenReturn("/test");
73 //TODO:Must try a real connection
74 assertNull(commonUtils.executeAndRecordOpenstackRequest (openstackRequest));
78 public void testexecuteAndRecordOpenstackRequestResponseException() {
79 expectedException.expect(OpenStackResponseException.class);
81 doThrow(OpenStackResponseException.class).when(openstackRequest).execute();
83 commonUtils.executeAndRecordOpenstackRequest(openstackRequest);
87 public void testexecuteAndRecordOpenstackRequestConnectException() {
88 expectedException.expect(OpenStackConnectException.class);
90 doThrow(OpenStackConnectException.class).when(openstackRequest).execute();
92 commonUtils.executeAndRecordOpenstackRequest(openstackRequest);
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 assertTrue("connect".equals(me.getMessage()));
107 MsoException me2 = commonUtils.keystoneErrorToMsoException(openStackResponseException,"ContextError");
108 assertTrue(me2 instanceof MsoOpenstackException);
109 assertTrue("ContextError".equals(me2.getContext()));
110 assertTrue(MsoExceptionCategory.OPENSTACK.equals(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(eq(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 assertTrue("connect".equals(me.getMessage()));
138 MsoException me2 = commonUtils.heatExceptionToMsoException(openStackResponseException,"ContextError");
139 assertTrue(me2 instanceof MsoOpenstackException);
140 assertTrue("ContextError".equals(me2.getContext()));
141 assertTrue(MsoExceptionCategory.OPENSTACK.equals(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(eq(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() throws JsonParseException, JsonMappingException, IOException {
159 OpenStackBaseException openStackConnectException = new OpenStackConnectException("connect");
161 OpenStackBaseException openStackResponseException = new OpenStackResponseException("response",1);
163 MsoException me = commonUtils.neutronExceptionToMsoException(openStackConnectException,"ContextError");
165 assertTrue(me instanceof MsoIOException);
166 assertTrue("connect".equals(me.getMessage()));
168 MsoException me2 = commonUtils.neutronExceptionToMsoException(openStackResponseException,"ContextError");
169 assertTrue(me2 instanceof MsoOpenstackException);
170 assertTrue("ContextError".equals(me2.getContext()));
171 assertTrue(MsoExceptionCategory.OPENSTACK.equals(me2.getCategory()));
174 OpenStackResponse openStackResponse = Mockito.mock(OpenStackResponse.class);
175 NeutronError explanation = mapper.readValue(new File(RESOURCE_PATH + "NeutronError.json"), NeutronError.class);
177 doReturn(explanation).when(openStackResponse).getErrorEntity(eq(NeutronError.class));
179 openStackResponseException = new OpenStackResponseException("response", 501, openStackResponse);
181 MsoException me3 = commonUtils.neutronExceptionToMsoException(openStackResponseException,"ContextError");
183 assertTrue(me3 instanceof MsoOpenstackException);
184 assertEquals("501 type: message", me3.toString());
188 public final void testRuntimeExceptionToMsoException() {
189 RuntimeException re = new RuntimeException("runtime");
190 MsoException me = commonUtils.runtimeExceptionToMsoException(re, "ContextError");
192 assertTrue(me instanceof MsoAdapterException);
193 assertTrue("ContextError".equals(me.getContext()));
194 assertTrue(MsoExceptionCategory.INTERNAL.equals(me.getCategory()));
198 public void testIoExceptionToMsoException() {
199 IOException exception = new IOException("IOExceptionTestMessage");
201 MsoException msoException = commonUtils.ioExceptionToMsoException(exception, "ContextError");
203 assertTrue(msoException instanceof MsoAdapterException);
204 assertEquals("ContextError", msoException.getContext());
205 assertTrue(MsoExceptionCategory.INTERNAL.equals(msoException.getCategory()));