Skip to content

Commit f50e395

Browse files
author
TanyaEf
committed
Resolved issue #122 (refactoring in integration tests, new method in RestClientConfiguration)
1 parent 92d0c47 commit f50e395

File tree

5 files changed

+169
-87
lines changed

5 files changed

+169
-87
lines changed

src/integration-test/java/com/jaspersoft/jasperserver/jaxrs/client/RestClientTestUtil.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,41 @@
33
import com.jaspersoft.jasperserver.jaxrs.client.core.JasperserverRestClient;
44
import com.jaspersoft.jasperserver.jaxrs.client.core.RestClientConfiguration;
55
import com.jaspersoft.jasperserver.jaxrs.client.core.Session;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.util.Properties;
69

710
/**
811
* @author Tetiana Iefimenko
912
*/
1013
public abstract class RestClientTestUtil {
11-
protected RestClientConfiguration configuration;
12-
protected JasperserverRestClient client;
14+
protected RestClientConfiguration configuration;
15+
protected JasperserverRestClient client;
1316
protected Session session;
17+
protected Properties properties;
1418

1519
protected void initClient() {
16-
configuration = RestClientConfiguration.loadConfiguration("test_config.properties");
20+
loadTestProperties("test_config.properties");
21+
configuration = RestClientConfiguration.loadConfiguration(properties);
1722
client = new JasperserverRestClient(configuration);
1823
}
1924

2025
protected void initSession() {
21-
session = client.authenticate("superuser", "superuser");
26+
session = client.authenticate(properties.getProperty("username"), properties.getProperty("password"));
27+
}
28+
29+
private void loadTestProperties(String path) {
30+
properties = new Properties();
31+
InputStream is;
32+
try {
33+
is = RestClientConfiguration.class.getClassLoader().getResourceAsStream(path);
34+
if (is == null) {
35+
throw new IOException("Property file is not found");
36+
}
37+
properties.load(is);
38+
} catch (Exception e) {
39+
e.printStackTrace();
40+
}
41+
2242
}
2343
}

src/integration-test/resources/test_config.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#
22
url=http://localhost:4444/jasperserver-pro
3+
username=superuser
4+
password=superuser
35
#connectionTimeout=
46
#readTimeout=
57
jasperserverVersion=v6_1_0

src/main/java/com/jaspersoft/jasperserver/jaxrs/client/core/RestClientConfiguration.java

Lines changed: 72 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class RestClientConfiguration {
5959

6060
public RestClientConfiguration(String jasperReportsServerUrl) {
6161
this();
62+
6263
setJasperReportsServerUrl(jasperReportsServerUrl);
6364
}
6465

@@ -106,70 +107,6 @@ public void setRestrictedHttpMethods(Boolean restrictedHttpMethods) {
106107
this.restrictedHttpMethods = restrictedHttpMethods;
107108
}
108109

109-
public Boolean getLogHttp() {
110-
return logHttp;
111-
}
112-
113-
public void setLogHttp(Boolean logHttp) {
114-
this.logHttp = logHttp;
115-
}
116-
117-
public Boolean getLogHttpEntity() {
118-
return logHttpEntity;
119-
}
120-
121-
public void setLogHttpEntity(Boolean logHttpEntity) {
122-
this.logHttpEntity = logHttpEntity;
123-
}
124-
125-
public MimeType getContentMimeType() {
126-
return contentMimeType;
127-
}
128-
129-
public void setContentMimeType(MimeType contentMimeType) {
130-
this.contentMimeType = contentMimeType;
131-
}
132-
133-
public MimeType getAcceptMimeType() {
134-
return acceptMimeType;
135-
}
136-
137-
public void setAcceptMimeType(MimeType acceptMimeType) {
138-
this.acceptMimeType = acceptMimeType;
139-
}
140-
141-
public JRSVersion getJrsVersion() {
142-
return jrsVersion;
143-
}
144-
145-
public void setJrsVersion(JRSVersion jrsVersion) {
146-
this.jrsVersion = jrsVersion;
147-
}
148-
149-
public TrustManager[] getTrustManagers() {
150-
return trustManagers;
151-
}
152-
153-
public void setTrustManagers(TrustManager[] trustManagers) {
154-
this.trustManagers = trustManagers;
155-
}
156-
157-
public Integer getConnectionTimeout() {
158-
return connectionTimeout;
159-
}
160-
161-
public void setConnectionTimeout(Integer connectionTimeout) {
162-
this.connectionTimeout = connectionTimeout;
163-
}
164-
165-
public Integer getReadTimeout() {
166-
return readTimeout;
167-
}
168-
169-
public void setReadTimeout(Integer readTimeout) {
170-
this.readTimeout = readTimeout;
171-
}
172-
173110
public static RestClientConfiguration loadConfiguration(String path) {
174111
Properties properties = null;
175112
if (path != null) {
@@ -180,6 +117,11 @@ public static RestClientConfiguration loadConfiguration(String path) {
180117
return new RestClientConfiguration();
181118
}
182119

120+
return loadConfiguration(properties);
121+
}
122+
123+
public static RestClientConfiguration loadConfiguration(Properties properties) {
124+
183125
RestClientConfiguration configuration = new RestClientConfiguration();
184126
String url = properties.getProperty("url");
185127
if (isStringValid(url) && URL_PATTERN.matcher(url).matches()) {
@@ -205,7 +147,6 @@ public static RestClientConfiguration loadConfiguration(String path) {
205147
}
206148
}
207149

208-
209150
String authenticationType = properties.getProperty("authenticationType");
210151
if (isStringValid(authenticationType)) {
211152
try {
@@ -251,6 +192,7 @@ public static RestClientConfiguration loadConfiguration(String path) {
251192
return configuration;
252193
}
253194

195+
254196
private static Properties loadProperties(String path) {
255197
Properties properties = new Properties();
256198
try {
@@ -264,6 +206,70 @@ private static Properties loadProperties(String path) {
264206
}
265207

266208
private static Boolean isStringValid(String string) {
267-
return (string != null && string.length() > 0) ? true : false;
209+
return (string != null && string.length() > 0);
210+
}
211+
212+
public Boolean getLogHttp() {
213+
return logHttp;
214+
}
215+
216+
public void setLogHttp(Boolean logHttp) {
217+
this.logHttp = logHttp;
218+
}
219+
220+
public Boolean getLogHttpEntity() {
221+
return logHttpEntity;
222+
}
223+
224+
public void setLogHttpEntity(Boolean logHttpEntity) {
225+
this.logHttpEntity = logHttpEntity;
226+
}
227+
228+
public MimeType getContentMimeType() {
229+
return contentMimeType;
230+
}
231+
232+
public void setContentMimeType(MimeType contentMimeType) {
233+
this.contentMimeType = contentMimeType;
234+
}
235+
236+
public MimeType getAcceptMimeType() {
237+
return acceptMimeType;
238+
}
239+
240+
public void setAcceptMimeType(MimeType acceptMimeType) {
241+
this.acceptMimeType = acceptMimeType;
242+
}
243+
244+
public JRSVersion getJrsVersion() {
245+
return jrsVersion;
246+
}
247+
248+
public void setJrsVersion(JRSVersion jrsVersion) {
249+
this.jrsVersion = jrsVersion;
250+
}
251+
252+
public TrustManager[] getTrustManagers() {
253+
return trustManagers;
254+
}
255+
256+
public void setTrustManagers(TrustManager[] trustManagers) {
257+
this.trustManagers = trustManagers;
258+
}
259+
260+
public Integer getConnectionTimeout() {
261+
return connectionTimeout;
262+
}
263+
264+
public void setConnectionTimeout(Integer connectionTimeout) {
265+
this.connectionTimeout = connectionTimeout;
266+
}
267+
268+
public Integer getReadTimeout() {
269+
return readTimeout;
270+
}
271+
272+
public void setReadTimeout(Integer readTimeout) {
273+
this.readTimeout = readTimeout;
268274
}
269275
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public void before() {
7878
@Test(testName = "JasperserverRestClient_constructor")
7979
public void should_create_an_instance_of_JasperserverRestClient_with_proper_fields()
8080
throws IllegalAccessException {
81+
// Given
82+
doReturn("url").when(configurationMock).getJasperReportsServerUrl();
8183
// When
8284
final JasperserverRestClient client = new JasperserverRestClient(configurationMock);
8385

@@ -94,9 +96,16 @@ public void should_throw_an_exception_when_pass_null_param_to_the_constructor()
9496
new JasperserverRestClient(null);
9597
}
9698

99+
@Test(testName = "JasperserverRestClient_constructor",
100+
expectedExceptions = IllegalArgumentException.class)
101+
public void should_throw_an_exception_when_pass_empty_configuration_to_the_constructor() {
102+
new JasperserverRestClient(new RestClientConfiguration());
103+
}
104+
97105
@Test(testName = "JasperserverRestClient_authenticate")
98106
public void should_return_null_when_username_is_null() {
99107
// Given
108+
doReturn("url").when(configurationMock).getJasperReportsServerUrl();
100109
JasperserverRestClient jasperserverRestClient = new JasperserverRestClient(configurationMock);
101110
// When
102111
Session session = jasperserverRestClient.authenticate(null, PASSWORD);
@@ -106,6 +115,7 @@ public void should_return_null_when_username_is_null() {
106115
@Test(testName = "JasperserverRestClient_authenticate")
107116
public void should_return_null_when_username_is_empty() {
108117
// Given
118+
doReturn("url").when(configurationMock).getJasperReportsServerUrl();
109119
JasperserverRestClient jasperserverRestClient = new JasperserverRestClient(configurationMock);
110120
// When
111121
Session session = jasperserverRestClient.authenticate("", PASSWORD);
@@ -116,6 +126,7 @@ public void should_return_null_when_username_is_empty() {
116126
@Test(testName = "JasperserverRestClient_authenticate")
117127
public void should_return_null_when_password_is_null() {
118128
// Given
129+
doReturn("url").when(configurationMock).getJasperReportsServerUrl();
119130
JasperserverRestClient jasperserverRestClient = new JasperserverRestClient(configurationMock);
120131
// When
121132
Session session = jasperserverRestClient.authenticate(USER_NAME, null);
@@ -125,6 +136,7 @@ public void should_return_null_when_password_is_null() {
125136
@Test(testName = "JasperserverRestClient_authenticate")
126137
public void should_return_null_when_password_is_empty() {
127138
// Given
139+
doReturn("url").when(configurationMock).getJasperReportsServerUrl();
128140
JasperserverRestClient jasperserverRestClient = new JasperserverRestClient(configurationMock);
129141
// When
130142
Session session = jasperserverRestClient.authenticate(USER_NAME, "");
@@ -137,6 +149,7 @@ public void should_return_null_when_password_is_empty() {
137149
public void should_return_proper_Session_object() throws Exception {
138150

139151
// Given
152+
doReturn("url").when(configurationMock).getJasperReportsServerUrl();
140153
final JasperserverRestClient client = new JasperserverRestClient(configurationMock);
141154
final JasperserverRestClient spyClient = spy(client);
142155

@@ -164,6 +177,7 @@ public void should_return_proper_Session_object() throws Exception {
164177
public void should_involve_login_method_and_return_proper_session_object() throws Exception {
165178
// Given
166179
final URI location = new URI("location");
180+
doReturn("url").when(configurationMock).getJasperReportsServerUrl();
167181
final JasperserverRestClient client = new JasperserverRestClient(configurationMock);
168182

169183
whenNew(AuthenticationCredentials.class)
@@ -212,6 +226,7 @@ public void should_involve_login_method_and_return_proper_session_object() throw
212226
@Test
213227
public void should_involve_login_method_and_return_proper_session_object_within_basic_authorization() throws Exception {
214228
// Given
229+
doReturn("url").when(configurationMock).getJasperReportsServerUrl();
215230
final JasperserverRestClient client = new JasperserverRestClient(configurationMock);
216231

217232
whenNew(AuthenticationCredentials.class)
@@ -249,14 +264,14 @@ public void should_involve_login_method_and_return_proper_session_object_within_
249264
public void should_return_proper_anonymousSession_object() throws Exception {
250265

251266
// Given
252-
final JasperserverRestClient client = new JasperserverRestClient(configurationMock);
253-
267+
doReturn("url").when(configurationMock).getJasperReportsServerUrl();
254268
whenNew(SessionStorage.class)
255269
.withAnyArguments()
256270
.thenReturn(sessionStorageMock);
257271
whenNew(AnonymousSession.class)
258272
.withArguments(sessionStorageMock)
259273
.thenReturn(anonymousSessionMock);
274+
final JasperserverRestClient client = new JasperserverRestClient(configurationMock);
260275

261276
// When
262277
AnonymousSession retrieved = client.getAnonymousSession();

0 commit comments

Comments
 (0)