Merge 'origin/casablanca' into master
[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
31 import java.io.File;
32 import java.io.IOException;
33
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;
45
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;
57
58 /**
59  * This class implements test methods of the MsoCommonUtils
60  */
61 public class MsoCommonUtilsTest extends BaseTest {
62         @Autowired
63         @Qualifier("CommonUtils")
64         private MsoCommonUtils commonUtils;
65         
66         @Mock
67         private OpenStackRequest openstackRequest;
68                 
69         @Test
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));
75         }
76         
77         @Test
78         public void testexecuteAndRecordOpenstackRequestResponseException() {
79                 expectedException.expect(OpenStackResponseException.class);
80                 
81                 doThrow(OpenStackResponseException.class).when(openstackRequest).execute();
82                 
83                 commonUtils.executeAndRecordOpenstackRequest(openstackRequest);
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);
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                 assertTrue("connect".equals(me.getMessage()));
105
106
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()));
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                 assertTrue("connect".equals(me.getMessage()));
136
137
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()));
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() 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 }