-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBookResourceIntegrationTest.java
369 lines (297 loc) · 12.1 KB
/
BookResourceIntegrationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package com.programmingskillz.samplejerseywebapp.web;
import static com.programmingskillz.samplejerseywebapp.util.Utils.deleteDir;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import com.programmingskillz.SampleApplication;
import com.programmingskillz.samplejerseywebapp.business.domain.Book;
import com.programmingskillz.samplejerseywebapp.config.providers.SampleObjectMapperProvider;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.message.GZipEncoder;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.TestProperties;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.time.Instant;
import java.util.Base64;
import java.util.List;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
/**
* @author Durim Kryeziu
*/
public class BookResourceIntegrationTest extends JerseyTest {
private String bookId;
private String authHeaderValue;
@Override
protected Application configure() {
enable(TestProperties.LOG_TRAFFIC);
return new SampleApplication();
}
@Override
protected void configureClient(ClientConfig config) {
config.register(SampleObjectMapperProvider.class);
config.register(JacksonFeature.class);
config.register(GZipEncoder.class);
config.property(LoggingFeature.LOGGING_FEATURE_LOGGER_LEVEL_CLIENT, "INFO");
config.property(
LoggingFeature.LOGGING_FEATURE_VERBOSITY_CLIENT,
LoggingFeature.Verbosity.HEADERS_ONLY
);
}
@Before
public void addBook() throws Exception {
Book book = new Book();
book.setTitle("How to Win Friends & Influence People");
book.setAuthor("Dale Carnegie");
book.setIsbn("067142517X");
book.setPages(299);
book.setPublished(Instant.now());
String username = "durimkryeziu";
String password = "password";
String encodedString = Base64.getEncoder()
.encodeToString(String.format("%s:%s", username, password).getBytes());
authHeaderValue = String.format("Basic %s", encodedString);
Entity<Book> bookEntity = Entity.entity(book, MediaType.APPLICATION_JSON);
Response response = target("books")
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.post(bookEntity);
Book bookResponse = response.readEntity(Book.class);
this.bookId = bookResponse.getId();
}
@Test
public void testAddBookWithNecessaryFields() throws Exception {
Book book = new Book();
book.setTitle("How to Win Friends & Influence People");
book.setAuthor("Dale Carnegie");
book.setIsbn("067142517X");
book.setPages(299);
Entity<Book> bookEntity = Entity.entity(book, MediaType.APPLICATION_JSON);
Response response = target("books")
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.post(bookEntity);
assertEquals(201, response.getStatus());
assertNotNull(response.getHeaderString("Location"));
Book bookResponse = response.readEntity(Book.class);
assertEquals("How to Win Friends & Influence People", bookResponse.getTitle());
assertEquals("Dale Carnegie", bookResponse.getAuthor());
assertEquals("067142517X", bookResponse.getIsbn());
assertEquals(299, bookResponse.getPages().intValue());
assertEquals(204, cleanUp(bookResponse.getId()).getStatus());
}
@Test
public void testAddBookFull() throws Exception {
Book book = new Book();
book.setTitle("The Clean Coder: A Code of Conduct for Professional Programmers");
book.setAuthor("Robert C. Martin");
book.setDescription("In The Clean Coder: A Code of Conduct for Professional Programmers, " +
"legendary software expert Robert C. Martin introduces the disciplines, techniques, " +
"tools, and practices of true software craftsmanship. This book is packed with " +
"practical advice–about everything from estimating and coding to refactoring and " +
"testing.");
book.setIsbn("9780137081073");
book.setPages(256);
book.setPublisher("Prentice Hall");
Instant date = Instant.parse("2011-05-23T00:00:00.00Z");
book.setPublished(date);
Entity<Book> bookEntity = Entity.entity(book, MediaType.APPLICATION_JSON);
Response response = target("books")
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.post(bookEntity);
assertEquals(201, response.getStatus());
assertNotNull(response.getHeaderString("Location"));
Book bookResponse = response.readEntity(Book.class);
assertEquals("The Clean Coder: A Code of Conduct for Professional Programmers",
bookResponse.getTitle());
assertEquals("Robert C. Martin", bookResponse.getAuthor());
assertEquals("In The Clean Coder: A Code of Conduct for Professional Programmers, " +
"legendary software expert Robert C. Martin introduces the disciplines, techniques, " +
"tools, and practices of true software craftsmanship. This book is packed with " +
"practical advice–about everything from estimating and coding to refactoring and " +
"testing.", bookResponse.getDescription());
assertEquals("9780137081073", bookResponse.getIsbn());
assertEquals(256, bookResponse.getPages().intValue());
assertEquals("Prentice Hall", bookResponse.getPublisher());
assertEquals(date, bookResponse.getPublished());
assertEquals(204, cleanUp(bookResponse.getId()).getStatus());
}
@Test
public void testGetOneBook() throws Exception {
Response response = target("books")
.path(bookId)
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.get();
assertEquals(200, response.getStatus());
Book book = response.readEntity(Book.class);
assertNotNull(book);
assertEquals(bookId, book.getId());
}
@Test
public void testGetAllBooks() throws Exception {
Response response = target("books")
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.get();
assertEquals(200, response.getStatus());
List<Book> books = response.readEntity(new GenericType<List<Book>>() {
});
assertNotNull(books);
assertTrue(books.size() > 0);
assertTrue(books.stream().anyMatch(b -> b.getId().equals(bookId)));
}
@Test
public void testUpdateBook() throws Exception {
Book book = target("books")
.path(bookId)
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.get(Book.class);
book.setDescription("Description is updated");
Response response = target("books")
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.put(Entity.entity(book, MediaType.APPLICATION_JSON));
assertEquals(200, response.getStatus());
Book updatedBook = target("books")
.path(bookId)
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.get(Book.class);
assertEquals(book, updatedBook);
}
@Test
public void testDeleteOneBook() throws Exception {
Response response = target("books")
.path(bookId)
.request()
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.delete();
assertEquals(204, response.getStatus());
Response notFoundResponse = target("books")
.path(bookId)
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.get();
assertEquals(404, notFoundResponse.getStatus());
}
@Test
public void testRequestBodyNull() throws Exception {
Response response = target("books")
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.post(null);
assertEquals(400, response.getStatus());
assertEquals(MediaType.APPLICATION_JSON, response.getHeaderString("Content-Type"));
String entity = response.readEntity(String.class);
assertTrue(entity.contains("Request body does not exist."));
}
@Test
public void testUpdateWithoutId() throws Exception {
Book book = new Book();
book.setTitle("Effective Java (2nd Edition)");
book.setAuthor("Joshua Bloch");
book.setIsbn("9780321356680");
book.setPages(346);
Entity<Book> bookEntity = Entity.entity(book, MediaType.APPLICATION_JSON);
Response response = target("books")
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.put(bookEntity);
assertEquals(400, response.getStatus());
assertEquals(MediaType.APPLICATION_JSON, response.getHeaderString("Content-Type"));
String entity = response.readEntity(String.class);
assertTrue(entity.contains("Id cannot be null when you want to update the Book"));
}
@Test
public void testUriBasedContentNegotiation() throws Exception {
Response jsonResponse = target("books")
.path(".json")
.request()
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.get();
assertEquals(MediaType.APPLICATION_JSON, jsonResponse.getHeaderString("Content-Type"));
Response xmlResponse = target("books")
.path(".xml")
.request()
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.get();
assertEquals(MediaType.APPLICATION_XML, xmlResponse.getHeaderString("Content-Type"));
}
@Test
public void testPoweredByHeader() throws Exception {
Response response = target("books")
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.get();
assertNotNull(response.getHeaderString("X-Powered-By"));
assertEquals("Jersey Framework", response.getHeaderString("X-Powered-By"));
}
@Test
public void shouldNotBeAllowedWithoutBasicAuth() throws Exception {
Response response = target("books")
.request(MediaType.APPLICATION_JSON)
.get();
assertEquals(401, response.getStatus());
assertEquals("Basic", response.getHeaderString("WWW-Authenticate"));
}
@Test
public void shouldNotBeAllowedWithoutBasicPrefix() throws Exception {
String username = "durimkryeziu";
String password = "password";
String encodedString = Base64.getEncoder()
.encodeToString(String.format("%s:%s", username, password).getBytes());
Response response = target("books")
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, encodedString)
.get();
assertEquals(401, response.getStatus());
assertEquals("Basic", response.getHeaderString("WWW-Authenticate"));
}
@Test
public void shouldNotBeAllowedWithWrongUsernameOrPassword() throws Exception {
String username = "john";
String password = "doe";
String encodedString = Base64.getEncoder()
.encodeToString(String.format("%s:%s", username, password).getBytes());
String authHeaderValue = String.format("Basic %s", encodedString);
Response response = target("books")
.request(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.get();
assertEquals(401, response.getStatus());
assertEquals("Basic", response.getHeaderString("WWW-Authenticate"));
}
@After
public void deleteBook() throws Exception {
cleanUp(this.bookId);
}
private Response cleanUp(String id) {
return target("books")
.path(id)
.request()
.header(HttpHeaders.AUTHORIZATION, authHeaderValue)
.delete();
}
/**
* Cleans up the logs folder created in this directory while doing integration tests when
* CATALINA_HOME environment variables is missing during build time.
*/
@AfterClass
public static void cleanUp() throws Exception {
File logs = new File("${env:CATALINA_HOME}");
deleteDir(logs);
}
}