622ad4f94f01712e493665d4807b74fa1aa003e8
[so.git] / adapters / mso-adapter-utils / src / test / java / org / onap / so / openstack / utils / MsoCommonUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.openstack.utils;
22
23
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;
30 import java.io.File;
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;
54
55 /**
56  * This class implements test methods of the MsoCommonUtils
57  */
58 public class MsoCommonUtilsTest extends BaseTest {
59     @Autowired
60     @Qualifier("CommonUtils")
61     private MsoCommonUtils commonUtils;
62
63     @Mock
64     private OpenStackRequest openstackRequest;
65
66     @Test
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));
72         assertNull(commonUtils.executeAndRecordOpenstackRequest(openstackRequest, true));
73     }
74
75     @Test
76     public void testexecuteAndRecordOpenstackRequestResponseException() {
77         expectedException.expect(OpenStackResponseException.class);
78
79         doThrow(OpenStackResponseException.class).when(openstackRequest).execute();
80
81         commonUtils.executeAndRecordOpenstackRequest(openstackRequest);
82         commonUtils.executeAndRecordOpenstackRequest(openstackRequest, true);
83     }
84
85     @Test
86     public void testexecuteAndRecordOpenstackRequestConnectException() {
87         expectedException.expect(OpenStackConnectException.class);
88
89         doThrow(OpenStackConnectException.class).when(openstackRequest).execute();
90
91         commonUtils.executeAndRecordOpenstackRequest(openstackRequest, true);
92     }
93
94     @Test
95     public final void testKeystoneErrorToMsoException() throws JsonParseException, JsonMappingException, IOException {
96         OpenStackBaseException openStackConnectException = new OpenStackConnectException("connect");
97
98         OpenStackBaseException openStackResponseException = new OpenStackResponseException("response", 1);
99
100         MsoException me = commonUtils.keystoneErrorToMsoException(openStackConnectException, "ContextError");
101
102         assertTrue(me instanceof MsoIOException);
103         assertTrue("connect".equals(me.getMessage()));
104
105
106         MsoException me2 = commonUtils.keystoneErrorToMsoException(openStackResponseException, "ContextError");
107         assertTrue(me2 instanceof MsoOpenstackException);
108         assertTrue("ContextError".equals(me2.getContext()));
109         assertTrue(MsoExceptionCategory.OPENSTACK.equals(me2.getCategory()));
110
111
112         OpenStackResponse openStackResponse = Mockito.mock(OpenStackResponse.class);
113         Error error = mapper.readValue(new File(RESOURCE_PATH + "Error.json"), Error.class);
114
115         doReturn(error).when(openStackResponse).getErrorEntity(eq(Error.class));
116
117         openStackResponseException = new OpenStackResponseException("response", 501, openStackResponse);
118
119         MsoException me3 = commonUtils.keystoneErrorToMsoException(openStackResponseException, "ContextError");
120
121         assertTrue(me3 instanceof MsoOpenstackException);
122         assertEquals("1 title: message", me3.toString());
123     }
124
125     @Test
126     public final void testHeatExceptionToMsoException() throws JsonParseException, JsonMappingException, IOException {
127         OpenStackBaseException openStackConnectException = new OpenStackConnectException("connect");
128
129         OpenStackBaseException openStackResponseException = new OpenStackResponseException("response", 1);
130
131         MsoException me = commonUtils.heatExceptionToMsoException(openStackConnectException, "ContextError");
132
133         assertTrue(me instanceof MsoIOException);
134         assertTrue("connect".equals(me.getMessage()));
135
136
137         MsoException me2 = commonUtils.heatExceptionToMsoException(openStackResponseException, "ContextError");
138         assertTrue(me2 instanceof MsoOpenstackException);
139         assertTrue("ContextError".equals(me2.getContext()));
140         assertTrue(MsoExceptionCategory.OPENSTACK.equals(me2.getCategory()));
141
142
143         OpenStackResponse openStackResponse = Mockito.mock(OpenStackResponse.class);
144         Explanation explanation = mapper.readValue(new File(RESOURCE_PATH + "Explanation.json"), Explanation.class);
145
146         doReturn(explanation).when(openStackResponse).getErrorEntity(eq(Explanation.class));
147
148         openStackResponseException = new OpenStackResponseException("response", 501, openStackResponse);
149
150         MsoException me3 = commonUtils.heatExceptionToMsoException(openStackResponseException, "ContextError");
151
152         assertTrue(me3 instanceof MsoOpenstackException);
153         assertEquals("1 title: explanation, error.type=null, error.message=null", me3.toString());
154     }
155
156     @Test
157     public final void testNeutronExceptionToMsoException()
158             throws JsonParseException, JsonMappingException, IOException {
159         OpenStackBaseException openStackConnectException = new OpenStackConnectException("connect");
160
161         OpenStackBaseException openStackResponseException = new OpenStackResponseException("response", 1);
162
163         MsoException me = commonUtils.neutronExceptionToMsoException(openStackConnectException, "ContextError");
164
165         assertTrue(me instanceof MsoIOException);
166         assertTrue("connect".equals(me.getMessage()));
167
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()));
172
173
174         OpenStackResponse openStackResponse = Mockito.mock(OpenStackResponse.class);
175         NeutronError explanation = mapper.readValue(new File(RESOURCE_PATH + "NeutronError.json"), NeutronError.class);
176
177         doReturn(explanation).when(openStackResponse).getErrorEntity(eq(NeutronError.class));
178
179         openStackResponseException = new OpenStackResponseException("response", 501, openStackResponse);
180
181         MsoException me3 = commonUtils.neutronExceptionToMsoException(openStackResponseException, "ContextError");
182
183         assertTrue(me3 instanceof MsoOpenstackException);
184         assertEquals("501 type: message", me3.toString());
185     }
186
187     @Test
188     public final void testRuntimeExceptionToMsoException() {
189         RuntimeException re = new RuntimeException("runtime");
190         MsoException me = commonUtils.runtimeExceptionToMsoException(re, "ContextError");
191
192         assertTrue(me instanceof MsoAdapterException);
193         assertTrue("ContextError".equals(me.getContext()));
194         assertTrue(MsoExceptionCategory.INTERNAL.equals(me.getCategory()));
195     }
196
197     @Test
198     public void testIoExceptionToMsoException() {
199         IOException exception = new IOException("IOExceptionTestMessage");
200
201         MsoException msoException = commonUtils.ioExceptionToMsoException(exception, "ContextError");
202
203         assertTrue(msoException instanceof MsoAdapterException);
204         assertEquals("ContextError", msoException.getContext());
205         assertTrue(MsoExceptionCategory.INTERNAL.equals(msoException.getCategory()));
206     }
207 }