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;
31 import java.io.IOException;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.onap.so.BaseTest;
36 import org.onap.so.openstack.exceptions.MsoAdapterException;
37 import org.onap.so.openstack.exceptions.MsoException;
38 import org.onap.so.openstack.exceptions.MsoExceptionCategory;
39 import org.onap.so.openstack.exceptions.MsoIOException;
40 import org.onap.so.openstack.exceptions.MsoOpenstackException;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.beans.factory.annotation.Qualifier;
43 import com.fasterxml.jackson.core.JsonParseException;
44 import com.fasterxml.jackson.databind.JsonMappingException;
45 import com.fasterxml.jackson.databind.ObjectMapper;
46 import com.woorea.openstack.base.client.OpenStackBaseException;
47 import com.woorea.openstack.base.client.OpenStackConnectException;
48 import com.woorea.openstack.base.client.OpenStackRequest;
49 import com.woorea.openstack.base.client.OpenStackResponse;
50 import com.woorea.openstack.base.client.OpenStackResponseException;
51 import com.woorea.openstack.heat.model.Explanation;
52 import com.woorea.openstack.keystone.model.Error;
53 import com.woorea.openstack.quantum.model.NeutronError;
56 * This class implements test methods of the MsoCommonUtils
58 public class MsoCommonUtilsTest extends BaseTest {
60 @Qualifier("CommonUtils")
61 private MsoCommonUtils commonUtils;
64 private OpenStackRequest openstackRequest;
67 public final void testExecuteAndRecordOpenstackRequest() {
68 Mockito.when(openstackRequest.endpoint()).thenReturn("localhost");
69 Mockito.when(openstackRequest.path()).thenReturn("/test");
70 // TODO:Must try a real connection
71 assertNull(commonUtils.executeAndRecordOpenstackRequest(openstackRequest));
75 public void testexecuteAndRecordOpenstackRequestResponseException() {
76 expectedException.expect(OpenStackResponseException.class);
78 doThrow(OpenStackResponseException.class).when(openstackRequest).execute();
80 commonUtils.executeAndRecordOpenstackRequest(openstackRequest);
84 public void testexecuteAndRecordOpenstackRequestConnectException() {
85 expectedException.expect(OpenStackConnectException.class);
87 doThrow(OpenStackConnectException.class).when(openstackRequest).execute();
89 commonUtils.executeAndRecordOpenstackRequest(openstackRequest);
93 public final void testKeystoneErrorToMsoException() throws JsonParseException, JsonMappingException, IOException {
94 OpenStackBaseException openStackConnectException = new OpenStackConnectException("connect");
96 OpenStackBaseException openStackResponseException = new OpenStackResponseException("response", 1);
98 MsoException me = commonUtils.keystoneErrorToMsoException(openStackConnectException, "ContextError");
100 assertTrue(me instanceof MsoIOException);
101 assertTrue("connect".equals(me.getMessage()));
104 MsoException me2 = commonUtils.keystoneErrorToMsoException(openStackResponseException, "ContextError");
105 assertTrue(me2 instanceof MsoOpenstackException);
106 assertTrue("ContextError".equals(me2.getContext()));
107 assertTrue(MsoExceptionCategory.OPENSTACK.equals(me2.getCategory()));
110 OpenStackResponse openStackResponse = Mockito.mock(OpenStackResponse.class);
111 Error error = mapper.readValue(new File(RESOURCE_PATH + "Error.json"), Error.class);
113 doReturn(error).when(openStackResponse).getErrorEntity(eq(Error.class));
115 openStackResponseException = new OpenStackResponseException("response", 501, openStackResponse);
117 MsoException me3 = commonUtils.keystoneErrorToMsoException(openStackResponseException, "ContextError");
119 assertTrue(me3 instanceof MsoOpenstackException);
120 assertEquals("1 title: message", me3.toString());
124 public final void testHeatExceptionToMsoException() throws JsonParseException, JsonMappingException, IOException {
125 OpenStackBaseException openStackConnectException = new OpenStackConnectException("connect");
127 OpenStackBaseException openStackResponseException = new OpenStackResponseException("response", 1);
129 MsoException me = commonUtils.heatExceptionToMsoException(openStackConnectException, "ContextError");
131 assertTrue(me instanceof MsoIOException);
132 assertTrue("connect".equals(me.getMessage()));
135 MsoException me2 = commonUtils.heatExceptionToMsoException(openStackResponseException, "ContextError");
136 assertTrue(me2 instanceof MsoOpenstackException);
137 assertTrue("ContextError".equals(me2.getContext()));
138 assertTrue(MsoExceptionCategory.OPENSTACK.equals(me2.getCategory()));
141 OpenStackResponse openStackResponse = Mockito.mock(OpenStackResponse.class);
142 Explanation explanation = mapper.readValue(new File(RESOURCE_PATH + "Explanation.json"), Explanation.class);
144 doReturn(explanation).when(openStackResponse).getErrorEntity(eq(Explanation.class));
146 openStackResponseException = new OpenStackResponseException("response", 501, openStackResponse);
148 MsoException me3 = commonUtils.heatExceptionToMsoException(openStackResponseException, "ContextError");
150 assertTrue(me3 instanceof MsoOpenstackException);
151 assertEquals("1 title: explanation, error.type=null, error.message=null", me3.toString());
155 public final void testNeutronExceptionToMsoException()
156 throws JsonParseException, JsonMappingException, IOException {
157 OpenStackBaseException openStackConnectException = new OpenStackConnectException("connect");
159 OpenStackBaseException openStackResponseException = new OpenStackResponseException("response", 1);
161 MsoException me = commonUtils.neutronExceptionToMsoException(openStackConnectException, "ContextError");
163 assertTrue(me instanceof MsoIOException);
164 assertTrue("connect".equals(me.getMessage()));
166 MsoException me2 = commonUtils.neutronExceptionToMsoException(openStackResponseException, "ContextError");
167 assertTrue(me2 instanceof MsoOpenstackException);
168 assertTrue("ContextError".equals(me2.getContext()));
169 assertTrue(MsoExceptionCategory.OPENSTACK.equals(me2.getCategory()));
172 OpenStackResponse openStackResponse = Mockito.mock(OpenStackResponse.class);
173 NeutronError explanation = mapper.readValue(new File(RESOURCE_PATH + "NeutronError.json"), NeutronError.class);
175 doReturn(explanation).when(openStackResponse).getErrorEntity(eq(NeutronError.class));
177 openStackResponseException = new OpenStackResponseException("response", 501, openStackResponse);
179 MsoException me3 = commonUtils.neutronExceptionToMsoException(openStackResponseException, "ContextError");
181 assertTrue(me3 instanceof MsoOpenstackException);
182 assertEquals("501 type: message", me3.toString());
186 public final void testRuntimeExceptionToMsoException() {
187 RuntimeException re = new RuntimeException("runtime");
188 MsoException me = commonUtils.runtimeExceptionToMsoException(re, "ContextError");
190 assertTrue(me instanceof MsoAdapterException);
191 assertTrue("ContextError".equals(me.getContext()));
192 assertTrue(MsoExceptionCategory.INTERNAL.equals(me.getCategory()));
196 public void testIoExceptionToMsoException() {
197 IOException exception = new IOException("IOExceptionTestMessage");
199 MsoException msoException = commonUtils.ioExceptionToMsoException(exception, "ContextError");
201 assertTrue(msoException instanceof MsoAdapterException);
202 assertEquals("ContextError", msoException.getContext());
203 assertTrue(MsoExceptionCategory.INTERNAL.equals(msoException.getCategory()));