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