2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.openecomp.sparky.dal.rest;
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;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.powermock.modules.junit4.PowerMockRunner;
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;
47 * The Class RestfulDataAccessorTest.
49 @RunWith(PowerMockRunner.class)
50 public class RestfulDataAccessorTest {
52 private RestClientBuilder clientBuilderMock;
53 private Client mockClient;
54 private ClientResponse mockClientResponse;
55 private WebResource mockWebResource;
56 private Builder mockBuilder;
62 * @throws Exception the exception
65 public void init() throws Exception {
68 * common collaborator mocking setup
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);
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());
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));
91 * @throws Exception the exception
94 public void successfulDoPut() throws Exception {
97 * set test mocking expectations
100 doReturn(200).when(mockClientResponse).getStatus();
101 doReturn("Success").when(mockClientResponse).getEntity(String.class);
104 RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
105 OperationResult actualResult = dataAccessor.doPut("myUrl", "jsonPayload", "acceptContentType");
107 assertEquals("Unexpected result", 200, actualResult.getResultCode());
113 * @throws Exception the exception
116 public void successfulDoGet() throws Exception {
119 * set test mocking expectations
122 doReturn(200).when(mockClientResponse).getStatus();
123 doReturn("Success").when(mockClientResponse).getEntity(String.class);
126 RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
127 OperationResult actualResult = dataAccessor.doGet("myUrl", "anyContentType");
129 assertEquals("Unexpected result", 200, actualResult.getResultCode());
134 * Successful do post.
136 * @throws Exception the exception
139 public void successfulDoPost() throws Exception {
142 * set test mocking expectations
145 doReturn(200).when(mockClientResponse).getStatus();
146 doReturn("Success").when(mockClientResponse).getEntity(String.class);
149 RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
150 OperationResult actualResult = dataAccessor.doPost("myUrl", "jsonPayload", "anyContentType");
152 assertEquals("Unexpected result", 200, actualResult.getResultCode());
157 * Successful do delete.
159 * @throws Exception the exception
162 public void successfulDoDelete() throws Exception {
165 * set test mocking expectations
168 doReturn(200).when(mockClientResponse).getStatus();
169 doReturn("Success").when(mockClientResponse).getEntity(String.class);
172 RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
173 OperationResult actualResult = dataAccessor.doDelete("myUrl", "anyContentType");
175 assertEquals("Unexpected result", 200, actualResult.getResultCode());
180 * Operation results in null pointer exception.
182 * @throws Exception the exception
185 public void operationResultsInNullPointerException() throws Exception {
188 * set test mocking expectations
192 doThrow(new NullPointerException("Parameter can't be null")).when(clientBuilderMock)
196 RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
197 OperationResult actualResult = dataAccessor.doDelete("myUrl", "anyContentType");
199 assertEquals("Unexpected result", 500, actualResult.getResultCode());
204 * Operation results in null client response.
206 * @throws Exception the exception
209 public void operationResultsInNullClientResponse() throws Exception {
212 * set test mocking expectations
214 // return null client response
215 doReturn(null).when(mockBuilder).delete(same(ClientResponse.class));
218 RestfulDataAccessor dataAccessor = new RestfulDataAccessor(clientBuilderMock);
219 OperationResult actualResult = dataAccessor.doDelete("myUrl", "anyContentType");
221 assertEquals("Unexpected result", 500, actualResult.getResultCode());