Updating Dockerfile
[aai/sparky-be.git] / src / test / java / org / onap / aai / 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.onap.aai.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.mockito.runners.MockitoJUnitRunner;
40 import org.onap.aai.sparky.dal.rest.OperationResult;
41 import org.onap.aai.sparky.dal.rest.RestClientBuilder;
42 import org.onap.aai.sparky.dal.rest.RestfulDataAccessor;
43
44 import com.sun.jersey.api.client.Client;
45 import com.sun.jersey.api.client.ClientResponse;
46 import com.sun.jersey.api.client.WebResource;
47 import com.sun.jersey.api.client.WebResource.Builder;
48
49 /**
50  * The Class RestfulDataAccessorTest.
51  */
52 @RunWith(MockitoJUnitRunner.class)
53 public class RestfulDataAccessorTest {
54
55   private RestClientBuilder clientBuilderMock;
56   private Client mockClient;
57   private ClientResponse mockClientResponse;
58   private WebResource mockWebResource;
59   private Builder mockBuilder;
60
61
62   /**
63    * Inits the.
64    *
65    * @throws Exception the exception
66    */
67   @Before
68   public void init() throws Exception {
69
70     /*
71      * common collaborator mocking setup
72      */
73
74     clientBuilderMock = mock(RestClientBuilder.class);
75     mockClient = mock(Client.class);
76     mockClientResponse = mock(ClientResponse.class);
77     mockWebResource = mock(WebResource.class);
78     mockBuilder = mock(Builder.class);
79
80     doReturn(mockClient).when(clientBuilderMock).getClient();
81     doReturn(mockWebResource).when(mockClient).resource(anyString());
82     doReturn(mockBuilder).when(mockWebResource).accept(anyString());
83     doReturn(mockBuilder).when(mockBuilder).header(anyString(), anyObject());
84
85     doReturn(mockClientResponse).when(mockBuilder).get(same(ClientResponse.class));
86     doReturn(mockClientResponse).when(mockBuilder).put(same(ClientResponse.class), anyObject());
87     doReturn(mockClientResponse).when(mockBuilder).post(same(ClientResponse.class), anyObject());
88     doReturn(mockClientResponse).when(mockBuilder).delete(same(ClientResponse.class));
89   }
90
91   /**
92    * Successful do put.
93    *
94    * @throws Exception the exception
95    */
96   @Test
97   public void successfulDoPut() throws Exception {
98
99     /*
100      * set test mocking expectations
101      */
102
103     doReturn(200).when(mockClientResponse).getStatus();
104     doReturn("Success").when(mockClientResponse).getEntity(String.class);
105
106     // test code
107     RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
108     OperationResult actualResult = dataAccessor.doPut("myUrl", "jsonPayload", "acceptContentType");
109
110     assertEquals("Unexpected result", 200, actualResult.getResultCode());
111   }
112
113   /**
114    * Successful do get.
115    *
116    * @throws Exception the exception
117    */
118   @Test
119   public void successfulDoGet() throws Exception {
120
121     /*
122      * set test mocking expectations
123      */
124
125     doReturn(200).when(mockClientResponse).getStatus();
126     doReturn("Success").when(mockClientResponse).getEntity(String.class);
127
128     // test code
129     RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
130     OperationResult actualResult = dataAccessor.doGet("myUrl", "anyContentType");
131
132     assertEquals("Unexpected result", 200, actualResult.getResultCode());
133
134   }
135
136   /**
137    * Successful do post.
138    *
139    * @throws Exception the exception
140    */
141   @Test
142   public void successfulDoPost() throws Exception {
143
144     /*
145      * set test mocking expectations
146      */
147
148     doReturn(200).when(mockClientResponse).getStatus();
149     doReturn("Success").when(mockClientResponse).getEntity(String.class);
150
151     // test code
152     RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
153     OperationResult actualResult = dataAccessor.doPost("myUrl", "jsonPayload", "anyContentType");
154
155     assertEquals("Unexpected result", 200, actualResult.getResultCode());
156
157   }
158
159   /**
160    * Successful do delete.
161    *
162    * @throws Exception the exception
163    */
164   @Test
165   public void successfulDoDelete() throws Exception {
166
167     /*
168      * set test mocking expectations
169      */
170
171     doReturn(200).when(mockClientResponse).getStatus();
172     doReturn("Success").when(mockClientResponse).getEntity(String.class);
173
174     // test code
175     RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
176     OperationResult actualResult = dataAccessor.doDelete("myUrl", "anyContentType");
177
178     assertEquals("Unexpected result", 200, actualResult.getResultCode());
179
180   }
181
182   /**
183    * Operation results in null pointer exception.
184    *
185    * @throws Exception the exception
186    */
187   @Test
188   public void operationResultsInNullPointerException() throws Exception {
189
190     /*
191      * set test mocking expectations
192      */
193
194
195     doThrow(new NullPointerException("Parameter can't be null")).when(clientBuilderMock)
196         .getClient();
197
198     // test code
199     RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
200     OperationResult actualResult = dataAccessor.doDelete("myUrl", "anyContentType");
201
202     assertEquals("Unexpected result", 500, actualResult.getResultCode());
203
204   }
205
206   /**
207    * Operation results in null client response.
208    *
209    * @throws Exception the exception
210    */
211   @Test
212   public void operationResultsInNullClientResponse() throws Exception {
213
214     /*
215      * set test mocking expectations
216      */
217     // return null client response
218     doReturn(null).when(mockBuilder).delete(same(ClientResponse.class));
219
220     // test code
221     RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
222     OperationResult actualResult = dataAccessor.doDelete("myUrl", "anyContentType");
223
224     assertEquals("Unexpected result", 500, actualResult.getResultCode());
225
226   }
227
228
229 }