Merge "Support for SO to ExtAPI"
[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     }
73
74     @Test
75     public void testexecuteAndRecordOpenstackRequestResponseException() {
76         expectedException.expect(OpenStackResponseException.class);
77
78         doThrow(OpenStackResponseException.class).when(openstackRequest).execute();
79
80         commonUtils.executeAndRecordOpenstackRequest(openstackRequest);
81     }
82
83     @Test
84     public void testexecuteAndRecordOpenstackRequestConnectException() {
85         expectedException.expect(OpenStackConnectException.class);
86
87         doThrow(OpenStackConnectException.class).when(openstackRequest).execute();
88
89         commonUtils.executeAndRecordOpenstackRequest(openstackRequest);
90     }
91
92     @Test
93     public final void testKeystoneErrorToMsoException() throws JsonParseException, JsonMappingException, IOException {
94         OpenStackBaseException openStackConnectException = new OpenStackConnectException("connect");
95
96         OpenStackBaseException openStackResponseException = new OpenStackResponseException("response", 1);
97
98         MsoException me = commonUtils.keystoneErrorToMsoException(openStackConnectException, "ContextError");
99
100         assertTrue(me instanceof MsoIOException);
101         assertTrue("connect".equals(me.getMessage()));
102
103
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()));
108
109
110         OpenStackResponse openStackResponse = Mockito.mock(OpenStackResponse.class);
111         Error error = mapper.readValue(new File(RESOURCE_PATH + "Error.json"), Error.class);
112
113         doReturn(error).when(openStackResponse).getErrorEntity(eq(Error.class));
114
115         openStackResponseException = new OpenStackResponseException("response", 501, openStackResponse);
116
117         MsoException me3 = commonUtils.keystoneErrorToMsoException(openStackResponseException, "ContextError");
118
119         assertTrue(me3 instanceof MsoOpenstackException);
120         assertEquals("1 title: message", me3.toString());
121     }
122
123     @Test
124     public final void testHeatExceptionToMsoException() throws JsonParseException, JsonMappingException, IOException {
125         OpenStackBaseException openStackConnectException = new OpenStackConnectException("connect");
126
127         OpenStackBaseException openStackResponseException = new OpenStackResponseException("response", 1);
128
129         MsoException me = commonUtils.heatExceptionToMsoException(openStackConnectException, "ContextError");
130
131         assertTrue(me instanceof MsoIOException);
132         assertTrue("connect".equals(me.getMessage()));
133
134
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()));
139
140
141         OpenStackResponse openStackResponse = Mockito.mock(OpenStackResponse.class);
142         Explanation explanation = mapper.readValue(new File(RESOURCE_PATH + "Explanation.json"), Explanation.class);
143
144         doReturn(explanation).when(openStackResponse).getErrorEntity(eq(Explanation.class));
145
146         openStackResponseException = new OpenStackResponseException("response", 501, openStackResponse);
147
148         MsoException me3 = commonUtils.heatExceptionToMsoException(openStackResponseException, "ContextError");
149
150         assertTrue(me3 instanceof MsoOpenstackException);
151         assertEquals("1 title: explanation, error.type=null, error.message=null", me3.toString());
152     }
153
154     @Test
155     public final void testNeutronExceptionToMsoException()
156             throws JsonParseException, JsonMappingException, IOException {
157         OpenStackBaseException openStackConnectException = new OpenStackConnectException("connect");
158
159         OpenStackBaseException openStackResponseException = new OpenStackResponseException("response", 1);
160
161         MsoException me = commonUtils.neutronExceptionToMsoException(openStackConnectException, "ContextError");
162
163         assertTrue(me instanceof MsoIOException);
164         assertTrue("connect".equals(me.getMessage()));
165
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()));
170
171
172         OpenStackResponse openStackResponse = Mockito.mock(OpenStackResponse.class);
173         NeutronError explanation = mapper.readValue(new File(RESOURCE_PATH + "NeutronError.json"), NeutronError.class);
174
175         doReturn(explanation).when(openStackResponse).getErrorEntity(eq(NeutronError.class));
176
177         openStackResponseException = new OpenStackResponseException("response", 501, openStackResponse);
178
179         MsoException me3 = commonUtils.neutronExceptionToMsoException(openStackResponseException, "ContextError");
180
181         assertTrue(me3 instanceof MsoOpenstackException);
182         assertEquals("501 type: message", me3.toString());
183     }
184
185     @Test
186     public final void testRuntimeExceptionToMsoException() {
187         RuntimeException re = new RuntimeException("runtime");
188         MsoException me = commonUtils.runtimeExceptionToMsoException(re, "ContextError");
189
190         assertTrue(me instanceof MsoAdapterException);
191         assertTrue("ContextError".equals(me.getContext()));
192         assertTrue(MsoExceptionCategory.INTERNAL.equals(me.getCategory()));
193     }
194
195     @Test
196     public void testIoExceptionToMsoException() {
197         IOException exception = new IOException("IOExceptionTestMessage");
198
199         MsoException msoException = commonUtils.ioExceptionToMsoException(exception, "ContextError");
200
201         assertTrue(msoException instanceof MsoAdapterException);
202         assertEquals("ContextError", msoException.getContext());
203         assertTrue(MsoExceptionCategory.INTERNAL.equals(msoException.getCategory()));
204     }
205 }