Skip to content

Commit f463c91

Browse files
author
TanyaEf
committed
Added new unit test, updated existing unit tests
1 parent 1024447 commit f463c91

File tree

5 files changed

+267
-118
lines changed

5 files changed

+267
-118
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.jaspersoft.jasperserver.jaxrs.client.core;
2+
3+
import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.AbstractAdapter;
4+
import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.bundles.BundlesService;
5+
import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.serverInfo.ServerInfoService;
6+
import com.jaspersoft.jasperserver.jaxrs.client.apiadapters.settings.SettingsService;
7+
import org.mockito.Mock;
8+
import org.testng.annotations.AfterMethod;
9+
import org.testng.annotations.BeforeMethod;
10+
import org.testng.annotations.Test;
11+
12+
import static org.mockito.MockitoAnnotations.initMocks;
13+
import static org.testng.Assert.assertNotNull;
14+
import static org.testng.Assert.assertSame;
15+
16+
17+
/**
18+
* Unit tests for {@link com.jaspersoft.jasperserver.jaxrs.client.core.Session}
19+
*/
20+
public class AnonymousSessionTest {
21+
@Mock
22+
public SessionStorage storageMock;
23+
24+
@BeforeMethod
25+
public void before() {
26+
initMocks(this);
27+
}
28+
29+
@Test(expectedExceptions = RuntimeException.class)
30+
public void should_throw_exception_when_cannot_instantiate_service_class() {
31+
AnonymousSession anonymousSession = new AnonymousSession(storageMock);
32+
class CustomAdapter extends AbstractAdapter {
33+
public CustomAdapter(SessionStorage sessionStorage) {
34+
super(sessionStorage);
35+
}
36+
}
37+
anonymousSession.getService(CustomAdapter.class);
38+
}
39+
40+
@Test
41+
public void should_return_proper_storage() {
42+
AnonymousSession anonymousSession = new AnonymousSession(storageMock);
43+
assertSame(anonymousSession.getStorage(), storageMock);
44+
}
45+
46+
@Test
47+
public void should_return_not_null_ServerInfoService() {
48+
AnonymousSession anonymousSession = new AnonymousSession(storageMock);
49+
ServerInfoService retrieved = anonymousSession.serverInfoService();
50+
assertNotNull(retrieved);
51+
}
52+
53+
@Test
54+
public void should_return_not_null_settingsService() {
55+
AnonymousSession anonymousSession = new AnonymousSession(storageMock);
56+
SettingsService retrieved = anonymousSession.settingsService();
57+
assertNotNull(retrieved);
58+
}
59+
60+
@Test
61+
public void should_return_not_null_bundlesService() {
62+
AnonymousSession anonymousSession = new AnonymousSession(storageMock);
63+
BundlesService retrieved = anonymousSession.bundlesService();
64+
assertNotNull(retrieved);
65+
}
66+
67+
@AfterMethod
68+
public void after() {
69+
storageMock = null;
70+
}
71+
}

src/test/java/com/jaspersoft/jasperserver/jaxrs/client/core/JasperserverRestClientTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public void should_return_proper_Session_object() throws Exception {
7070
final JasperserverRestClient client = new JasperserverRestClient(configurationMock);
7171
final JasperserverRestClient spyClient = spy(client);
7272

73-
7473
whenNew(AuthenticationCredentials.class)
7574
.withArguments(USER_NAME, PASSWORD)
7675
.thenReturn(credentialsMock);

src/test/java/com/jaspersoft/jasperserver/jaxrs/client/core/RestClientConfigurationTest.java

Lines changed: 122 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.jaspersoft.jasperserver.jaxrs.client.core;
22

3+
import com.jaspersoft.jasperserver.jaxrs.client.core.enums.AuthenticationType;
34
import com.jaspersoft.jasperserver.jaxrs.client.core.enums.JRSVersion;
5+
import com.jaspersoft.jasperserver.jaxrs.client.core.enums.MimeType;
46
import org.mockito.Mockito;
57
import org.powermock.api.mockito.PowerMockito;
68
import org.powermock.api.support.membermodification.MemberMatcher;
@@ -134,18 +136,23 @@ public void should_load_configuration_from_property_file_with_all_kind_of_setted
134136

135137
@Test(testName = "setJrsVersion")
136138
public void should_set_setJrsVersion_field() throws IllegalAccessException {
139+
// Given
137140
RestClientConfiguration config = new RestClientConfiguration();
138141
config.setJrsVersion(JRSVersion.v5_0_0);
139142
Field field = field(RestClientConfiguration.class, "jrsVersion");
143+
// When
140144
Object retrieved = field.get(config);
145+
//Then
141146
assertNotNull(retrieved);
142147
assertEquals(retrieved, JRSVersion.v5_0_0);
143148
}
144149

145150
@Test(testName = "setJrsVersion")
146151
public void should_get_not_null_setJrsVersion_field() throws IllegalAccessException {
152+
// Given
147153
RestClientConfiguration config = new RestClientConfiguration();
148154
Field field = field(RestClientConfiguration.class, "jrsVersion");
155+
// When
149156
field.set(config, JRSVersion.v4_7_0);
150157
assertEquals(config.getJrsVersion(), JRSVersion.v4_7_0);
151158
}
@@ -179,9 +186,9 @@ public X509Certificate[] getAcceptedIssuers() {
179186
assertEquals(expected, config.getTrustManagers()[0]);
180187
}
181188

182-
@Test
189+
@Test(testName = "loadConfiguration")
183190
public void should_invoke_private_method() throws Exception {
184-
191+
// Given
185192
Properties propertiesSpy = PowerMockito.spy(new Properties());
186193

187194
propertiesSpy.setProperty("url", "http://localhost:8080/jasperserver-pro/");
@@ -193,30 +200,137 @@ public void should_invoke_private_method() throws Exception {
193200
PowerMockito.whenNew(Properties.class).withNoArguments().thenReturn(propertiesSpy);
194201
PowerMockito.doNothing().when(propertiesSpy).load(any(InputStream.class));
195202
PowerMockito.suppress(method(Properties.class, "load", InputStream.class));
196-
203+
// When
197204
RestClientConfiguration retrieved = RestClientConfiguration.loadConfiguration("path");
198-
205+
//Then
199206
AssertJUnit.assertNotNull(retrieved);
200207
Mockito.verify(propertiesSpy, times(1)).load(any(InputStream.class));
201208
}
202209

203-
@Test(expectedExceptions = NullPointerException.class, enabled = false)
210+
@Test(testName = "ExceptionWhileLoadingProperties", expectedExceptions = NullPointerException.class, enabled = false)
204211
public void should_throw_an_exception_while_loading_props() throws Exception {
212+
// When
205213
RestClientConfiguration.loadConfiguration("path");
206214
}
207215

208-
@Test
216+
@Test(testName = "getTrustManagers")
209217
public void should_return_trusted_manager() throws Exception {
210-
218+
// Given
211219
RestClientConfiguration config = Mockito.spy(new RestClientConfiguration());
220+
// When
212221
TrustManager[] managers = config.getTrustManagers();
213222

214223
assertNotNull(managers);
215224
assertTrue(managers.length == 1);
216225

217226
((X509TrustManager) managers[0]).checkClientTrusted(null, "abc");
218227
X509Certificate[] retrieved = ((X509TrustManager) managers[0]).getAcceptedIssuers();
219-
228+
//Then
220229
Assert.assertNull(retrieved);
221230
}
231+
232+
@Test(testName = "getAuthenticationType")
233+
public void should_return_not_null_value_of_authenticationType_field() throws Exception {
234+
// Given
235+
RestClientConfiguration config = new RestClientConfiguration();
236+
Field field = field(RestClientConfiguration.class, "authenticationType");
237+
// When
238+
Object retrieved = field.get(config);
239+
//Then
240+
assertNotNull(retrieved);
241+
assertEquals(retrieved, AuthenticationType.SPRING);
242+
243+
}
244+
245+
@Test(testName = "setAuthenticationType")
246+
public void should_set_authenticationType_field() throws IllegalAccessException {
247+
// Given
248+
RestClientConfiguration config = new RestClientConfiguration();
249+
config.setAuthenticationType(AuthenticationType.BASIC);
250+
Field field = field(RestClientConfiguration.class, "authenticationType");
251+
// When
252+
Object retrieved = field.get(config);
253+
//Then
254+
assertNotNull(retrieved);
255+
assertEquals(retrieved, AuthenticationType.BASIC);
256+
}
257+
258+
@Test(testName = "getRestrictedHttpMethods")
259+
public void should_return_not_null_value_of_restrictedHttpMethods_field() throws Exception {
260+
// Given
261+
RestClientConfiguration config = new RestClientConfiguration();
262+
Field field = field(RestClientConfiguration.class, "restrictedHttpMethods");
263+
// When
264+
Object retrieved = field.get(config);
265+
//Then
266+
assertNotNull(retrieved);
267+
assertEquals(retrieved, false);
268+
269+
}
270+
271+
@Test(testName = "setRestrictedHttpMethods")
272+
public void should_set_restrictedHttpMethods_field() throws IllegalAccessException {
273+
// Given
274+
RestClientConfiguration config = new RestClientConfiguration();
275+
config.setRestrictedHttpMethods(true);
276+
Field field = field(RestClientConfiguration.class, "restrictedHttpMethods");
277+
// When
278+
Object retrieved = field.get(config);
279+
//Then
280+
assertNotNull(retrieved);
281+
assertEquals(retrieved, true);
282+
}
283+
284+
@Test(testName = "setContentMimeType")
285+
public void should_set_contentMimeType_field() throws IllegalAccessException {
286+
// Given
287+
RestClientConfiguration config = new RestClientConfiguration();
288+
config.setContentMimeType(MimeType.XML);
289+
Field field = field(RestClientConfiguration.class, "contentMimeType");
290+
// When
291+
Object retrieved = field.get(config);
292+
//Then
293+
assertNotNull(retrieved);
294+
assertEquals(retrieved, MimeType.XML);
295+
296+
}
297+
298+
@Test(testName = "getContentMimeType")
299+
public void should_get_not_null_contentMimeType_field() throws IllegalAccessException {
300+
// Given
301+
RestClientConfiguration config = new RestClientConfiguration();
302+
Field field = field(RestClientConfiguration.class, "contentMimeType");
303+
// When
304+
Object retrieved = field.get(config);
305+
//Then
306+
assertNotNull(retrieved);
307+
assertEquals(retrieved, MimeType.JSON);
308+
}
309+
310+
@Test(testName = "geAcceptMimeType")
311+
public void should_set_acceptMimeType_field() throws IllegalAccessException {
312+
// Given
313+
RestClientConfiguration config = new RestClientConfiguration();
314+
config.setAcceptMimeType(MimeType.XML);
315+
Field field = field(RestClientConfiguration.class, "acceptMimeType");
316+
// When
317+
Object retrieved = field.get(config);
318+
//Then
319+
assertNotNull(retrieved);
320+
assertEquals(retrieved, MimeType.XML);
321+
322+
}
323+
324+
@Test(testName = "setAcceptMimeType")
325+
public void should_get_not_null_acceptMimeType_field() throws IllegalAccessException {
326+
// Given
327+
RestClientConfiguration config = new RestClientConfiguration();
328+
Field field = field(RestClientConfiguration.class, "acceptMimeType");
329+
// When
330+
Object retrieved = field.get(config);
331+
//Then
332+
assertNotNull(retrieved);
333+
assertEquals(retrieved, MimeType.JSON);
334+
}
335+
222336
}

src/test/java/com/jaspersoft/jasperserver/jaxrs/client/core/SessionStorageTest.java

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,18 @@ public class SessionStorageTest extends PowerMockTestCase {
3434

3535
@Mock
3636
private ClientBuilder builderMock;
37-
3837
@Mock
3938
private RestClientConfiguration configurationMock;
40-
4139
@Mock
4240
private AuthenticationCredentials credentialsMock;
43-
4441
@Mock
4542
private SSLContext ctxMock;
46-
4743
@Mock
4844
private Client clientMock;
49-
5045
@Mock
5146
private WebTarget targetMock;
52-
5347
@Mock
5448
private Invocation.Builder invocationBuilderMock;
55-
5649
@Mock
5750
private Response responseMock;
5851

@@ -61,23 +54,21 @@ public void before() {
6154
initMocks(this);
6255
}
6356

64-
@Test(enabled = false)
57+
@Test
6558
public void should_create_new_instance_session_storage() throws Exception {
6659

67-
/* Given */
60+
// Given
6861
PowerMockito.suppress(method(SessionStorage.class, "init"));
69-
70-
RestClientConfiguration configurationMock = PowerMockito.mock(RestClientConfiguration.class);
71-
AuthenticationCredentials credentialsMock = PowerMockito.mock(AuthenticationCredentials.class);
72-
SessionStorage sessionStorageSpy = PowerMockito.spy(new SessionStorage(configurationMock, credentialsMock));
73-
74-
assertNotNull(sessionStorageSpy);
62+
// When
63+
SessionStorage sessionStorage = new SessionStorage(configurationMock, credentialsMock);
64+
// Then
65+
assertNotNull(sessionStorage);
7566
}
7667

7768
@Test(expectedExceptions = RuntimeException.class)
7869
public void should_throw_an_exception_when_unable_to_init_SSL_context() throws Exception {
7970

80-
/* Given */
71+
// Given
8172
TrustManager[] managers = new TrustManager[]{
8273
new X509TrustManager() {
8374
@Override
@@ -104,22 +95,22 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {
10495
PowerMockito.doReturn(managers).when(configurationMock).getTrustManagers();
10596
PowerMockito.doReturn(100L).when(configurationMock).getReadTimeout();
10697

107-
/* When */
108-
SessionStorage sessionStorageSpy = PowerMockito.spy(new SessionStorage(configurationMock, credentialsMock));
98+
// When
99+
SessionStorage sessionStorageSpy = new SessionStorage(configurationMock, credentialsMock);
109100

110-
/* Then throw an exception */
101+
// Then throw an exception
111102
}
112103

113-
@Test(enabled = false)
104+
@Test
114105
public void should_set_and_get_state_for_object() {
115106

116-
/* Given */
107+
// Given
117108
PowerMockito.suppress(method(SessionStorage.class, "init"));
118109
SessionStorage sessionStorageSpy = PowerMockito.spy(new SessionStorage(configurationMock, credentialsMock));
110+
// When
119111
Whitebox.setInternalState(sessionStorageSpy, "rootTarget", targetMock);
120112
Whitebox.setInternalState(sessionStorageSpy, "sessionId", "sessionId");
121-
122-
/* Verify */
113+
// Then
123114
assertNotNull(sessionStorageSpy.getConfiguration());
124115
assertNotNull(sessionStorageSpy.getCredentials());
125116
assertNotNull(sessionStorageSpy.getRootTarget());

0 commit comments

Comments
 (0)