Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / test / java / org / openecomp / sparky / dal / rest / RestfulDataAccessorTest.java
1 /* 
2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
7 * All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12
13 *      http://www.apache.org/licenses/LICENSE-2.0
14
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
21
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
24 */
25
26 package org.openecomp.sparky.dal.rest;
27
28 import static org.junit.Assert.assertEquals;
29 import static org.mockito.Matchers.anyObject;
30 import static org.mockito.Matchers.anyString;
31 import static org.mockito.Matchers.same;
32 import static org.mockito.Mockito.doReturn;
33 import static org.mockito.Mockito.doThrow;
34 import static org.mockito.Mockito.mock;
35
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.powermock.modules.junit4.PowerMockRunner;
40
41 import com.sun.jersey.api.client.Client;
42 import com.sun.jersey.api.client.ClientResponse;
43 import com.sun.jersey.api.client.WebResource;
44 import com.sun.jersey.api.client.WebResource.Builder;
45
46 /**
47  * The Class RestfulDataAccessorTest.
48  */
49 @RunWith(PowerMockRunner.class)
50 public class RestfulDataAccessorTest {
51
52   private RestClientBuilder clientBuilderMock;
53   private Client mockClient;
54   private ClientResponse mockClientResponse;
55   private WebResource mockWebResource;
56   private Builder mockBuilder;
57   
58
59   /**
60    * Inits the.
61    *
62    * @throws Exception the exception
63    */
64   @Before
65   public void init() throws Exception {
66   
67     /*
68      * common collaborator mocking setup
69      */
70
71     clientBuilderMock = mock(RestClientBuilder.class);
72     mockClient = mock(Client.class);
73     mockClientResponse = mock(ClientResponse.class);
74     mockWebResource = mock(WebResource.class);
75     mockBuilder = mock(Builder.class);
76
77     doReturn(mockClient).when(clientBuilderMock).getClient();
78     doReturn(mockWebResource).when(mockClient).resource(anyString());
79     doReturn(mockBuilder).when(mockWebResource).accept(anyString());
80     doReturn(mockBuilder).when(mockBuilder).header(anyString(), anyObject());
81
82     doReturn(mockClientResponse).when(mockBuilder).get(same(ClientResponse.class));
83     doReturn(mockClientResponse).when(mockBuilder).put(same(ClientResponse.class), anyObject());
84     doReturn(mockClientResponse).when(mockBuilder).post(same(ClientResponse.class), anyObject());
85     doReturn(mockClientResponse).when(mockBuilder).delete(same(ClientResponse.class));
86   }
87
88   /**
89    * Successful do put.
90    *
91    * @throws Exception the exception
92    */
93   @Test
94   public void successfulDoPut() throws Exception {
95
96     /*
97      * set test mocking expectations
98      */
99
100     doReturn(200).when(mockClientResponse).getStatus();
101     doReturn("Success").when(mockClientResponse).getEntity(String.class);
102
103     // test code
104     RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
105     OperationResult actualResult = dataAccessor.doPut("myUrl", "jsonPayload", "acceptContentType");
106
107     assertEquals("Unexpected result", 200, actualResult.getResultCode());
108   }
109
110   /**
111    * Successful do get.
112    *
113    * @throws Exception the exception
114    */
115   @Test
116   public void successfulDoGet() throws Exception {
117
118     /*
119      * set test mocking expectations
120      */
121
122     doReturn(200).when(mockClientResponse).getStatus();
123     doReturn("Success").when(mockClientResponse).getEntity(String.class);
124
125     // test code
126     RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
127     OperationResult actualResult = dataAccessor.doGet("myUrl", "anyContentType");
128
129     assertEquals("Unexpected result", 200, actualResult.getResultCode());
130
131   }
132
133   /**
134    * Successful do post.
135    *
136    * @throws Exception the exception
137    */
138   @Test
139   public void successfulDoPost() throws Exception {
140
141     /*
142      * set test mocking expectations
143      */
144
145     doReturn(200).when(mockClientResponse).getStatus();
146     doReturn("Success").when(mockClientResponse).getEntity(String.class);
147
148     // test code
149     RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
150     OperationResult actualResult = dataAccessor.doPost("myUrl", "jsonPayload", "anyContentType");
151
152     assertEquals("Unexpected result", 200, actualResult.getResultCode());
153
154   }
155
156   /**
157    * Successful do delete.
158    *
159    * @throws Exception the exception
160    */
161   @Test
162   public void successfulDoDelete() throws Exception {
163
164     /*
165      * set test mocking expectations
166      */
167
168     doReturn(200).when(mockClientResponse).getStatus();
169     doReturn("Success").when(mockClientResponse).getEntity(String.class);
170
171     // test code
172     RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
173     OperationResult actualResult = dataAccessor.doDelete("myUrl", "anyContentType");
174
175     assertEquals("Unexpected result", 200, actualResult.getResultCode());
176
177   }
178
179   /**
180    * Operation results in null pointer exception.
181    *
182    * @throws Exception the exception
183    */
184   @Test
185   public void operationResultsInNullPointerException() throws Exception {
186
187     /*
188      * set test mocking expectations
189      */
190
191
192     doThrow(new NullPointerException("Parameter can't be null")).when(clientBuilderMock)
193         .getClient();
194
195     // test code
196     RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
197     OperationResult actualResult = dataAccessor.doDelete("myUrl", "anyContentType");
198
199     assertEquals("Unexpected result", 500, actualResult.getResultCode());
200
201   }
202
203   /**
204    * Operation results in null client response.
205    *
206    * @throws Exception the exception
207    */
208   @Test
209   public void operationResultsInNullClientResponse() throws Exception {
210
211     /*
212      * set test mocking expectations
213      */
214     // return null client response
215     doReturn(null).when(mockBuilder).delete(same(ClientResponse.class));
216
217     // test code
218     RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
219     OperationResult actualResult = dataAccessor.doDelete("myUrl", "anyContentType");
220
221     assertEquals("Unexpected result", 500, actualResult.getResultCode());
222
223   }
224
225
226 }