Skip to content

Commit 3509667

Browse files
committed
ACC-2100 check problem details in tests
1 parent 3829839 commit 3509667

File tree

1 file changed

+54
-23
lines changed

1 file changed

+54
-23
lines changed

contentgrid-appserver-rest/src/test/java/com/contentgrid/appserver/rest/property/handler/RelationRequestHandlerTest.java

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.contentgrid.appserver.rest.property.handler;
22

3+
import static org.hamcrest.Matchers.is;
34
import static org.hamcrest.Matchers.not;
5+
import static org.hamcrest.Matchers.startsWith;
46
import static org.hamcrest.core.StringContains.containsString;
57
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
68
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
79
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
810
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
911
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.request;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
1013
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
14+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
1115
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
1216

1317
import com.contentgrid.appserver.application.model.Application;
@@ -376,21 +380,24 @@ static Stream<String> invalidContentType() {
376380
})
377381
void followRelationInvalidUrl(String url) throws Exception {
378382
mockMvc.perform(get(url))
379-
.andExpect(status().isNotFound());
383+
.andExpect(status().isNotFound())
384+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
380385
}
381386

382387
@Test
383388
void setRelationNoData() throws Exception {
384389
mockMvc.perform(put("/invoices/{sourceId}/previous-invoice", INVOICE_ID)
385390
.contentType("text/uri-list")
386391
.content("%n".formatted()))
387-
.andExpect(status().isBadRequest());
392+
.andExpect(status().isBadRequest())
393+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
388394
}
389395

390396
@Test
391397
void setRelationMissingContent() throws Exception {
392398
mockMvc.perform(put("/invoices/{sourceId}/previous-invoice", INVOICE_ID))
393-
.andExpect(status().isUnsupportedMediaType());
399+
.andExpect(status().isUnsupportedMediaType())
400+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
394401
}
395402

396403
@Test
@@ -401,7 +408,8 @@ void setRelationTooManyData() throws Exception {
401408
mockMvc.perform(put("/invoices/{sourceId}/previous-invoice", INVOICE_ID)
402409
.contentType("text/uri-list")
403410
.content("http://localhost/invoices/%s%nhttp://localhost/invoices/%s%n".formatted(target1, target2)))
404-
.andExpect(status().isBadRequest());
411+
.andExpect(status().isBadRequest())
412+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
405413
}
406414

407415
@ParameterizedTest
@@ -410,7 +418,8 @@ void setRelationInvalidUrl(String url) throws Exception {
410418
mockMvc.perform(put("/invoices/{sourceId}/previous-invoice", INVOICE_ID)
411419
.contentType("text/uri-list")
412420
.content(url))
413-
.andExpect(status().isBadRequest());
421+
.andExpect(status().isBadRequest())
422+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
414423
}
415424

416425
@ParameterizedTest
@@ -420,21 +429,25 @@ void setRelationInvalidMimeType(String contentType) throws Exception {
420429
mockMvc.perform(put("/invoices/{sourceId}/previous-invoice", INVOICE_ID)
421430
.contentType(contentType)
422431
.content("http://localhost/invoices/%s%n".formatted(targetId)))
423-
.andExpect(status().isUnsupportedMediaType());
432+
.andExpect(status().isUnsupportedMediaType())
433+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON))
434+
.andExpect(jsonPath("$.type").value(startsWith("https://contentgrid.cloud/problems/invalid-media-type")));
424435
}
425436

426437
@Test
427438
void addRelationNoData() throws Exception {
428439
mockMvc.perform(post("/persons/{sourceId}/invoices", PERSON_ID)
429440
.contentType("text/uri-list")
430441
.content("%n".formatted()))
431-
.andExpect(status().isBadRequest());
442+
.andExpect(status().isBadRequest())
443+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
432444
}
433445

434446
@Test
435447
void addRelationMissingContent() throws Exception {
436448
mockMvc.perform(post("/persons/{sourceId}/invoices", PERSON_ID))
437-
.andExpect(status().isUnsupportedMediaType());
449+
.andExpect(status().isUnsupportedMediaType())
450+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
438451
}
439452

440453
@ParameterizedTest
@@ -444,7 +457,9 @@ void addRelationInvalidMimeType(String contentType) throws Exception {
444457
mockMvc.perform(post("/persons/{sourceId}/invoices", PERSON_ID)
445458
.contentType(contentType)
446459
.content("http://localhost/invoices/%s%n".formatted(targetId)))
447-
.andExpect(status().isUnsupportedMediaType());
460+
.andExpect(status().isUnsupportedMediaType())
461+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON))
462+
.andExpect(jsonPath("$.type").value(startsWith("https://contentgrid.cloud/problems/invalid-media-type")));
448463
}
449464

450465
@ParameterizedTest
@@ -453,7 +468,8 @@ void addRelationInvalidUrl(String url) throws Exception {
453468
mockMvc.perform(post("/persons/{sourceId}/invoices", PERSON_ID)
454469
.contentType("text/uri-list")
455470
.content(url))
456-
.andExpect(status().isBadRequest());
471+
.andExpect(status().isBadRequest())
472+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
457473
}
458474

459475
static Stream<Arguments> unsupportedMethod() {
@@ -481,6 +497,8 @@ void unsupportedMethod(HttpMethod method, String url) throws Exception {
481497
}
482498
mockMvc.perform(requestBuilder)
483499
.andExpect(status().isMethodNotAllowed())
500+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON))
501+
.andExpect(jsonPath("$.type").value(is("https://contentgrid.cloud/problems/method-not-allowed")))
484502
.andExpect(header().exists(HttpHeaders.ALLOW))
485503
.andExpect(header().string(HttpHeaders.ALLOW, containsString(HttpMethod.GET.name())))
486504
.andExpect(header().string(HttpHeaders.ALLOW, not(containsString(method.name()))));
@@ -507,7 +525,8 @@ void unsupportedUrl(HttpMethod method, String url) throws Exception {
507525
.content("http://localhost/invoices/%s%n".formatted(UUID.randomUUID()));
508526
}
509527
mockMvc.perform(requestBuilder)
510-
.andExpect(status().isNotFound());
528+
.andExpect(status().isNotFound())
529+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
511530
}
512531

513532
}
@@ -525,7 +544,8 @@ void followToOneRelationSourceIdNotFound() throws Exception {
525544
.findRelationTarget(TestApplication.APPLICATION, TestApplication.INVOICE_PREVIOUS, INVOICE_ID);
526545

527546
mockMvc.perform(get("/invoices/{sourceId}/previous-invoice", INVOICE_ID))
528-
.andExpect(status().isNotFound());
547+
.andExpect(status().isNotFound())
548+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
529549
}
530550

531551
@Test
@@ -534,7 +554,8 @@ void followToOneRelationTargetIdNotFound() throws Exception {
534554
.findRelationTarget(TestApplication.APPLICATION, TestApplication.INVOICE_PREVIOUS, INVOICE_ID);
535555

536556
mockMvc.perform(get("/invoices/{sourceId}/previous-invoice", INVOICE_ID))
537-
.andExpect(status().isNotFound());
557+
.andExpect(status().isNotFound())
558+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
538559
}
539560

540561
@Test
@@ -543,7 +564,8 @@ void followToManyRelationSourceIdNotFound() throws Exception {
543564
.findById(TestApplication.APPLICATION, TestApplication.PERSON, PERSON_ID);
544565

545566
mockMvc.perform(get("/persons/{sourceId}/invoices", PERSON_ID))
546-
.andExpect(status().isNotFound());
567+
.andExpect(status().isNotFound())
568+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
547569
}
548570

549571
@Test
@@ -555,7 +577,8 @@ void followToManyRelationItemSourceIdOrTargetIdNotFound() throws Exception {
555577
.hasRelationTarget(TestApplication.APPLICATION, TestApplication.PERSON_INVOICES, PERSON_ID, targetId);
556578

557579
mockMvc.perform(get("/persons/{sourceId}/invoices/{targetId}", PERSON_ID, targetId))
558-
.andExpect(status().isNotFound());
580+
.andExpect(status().isNotFound())
581+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
559582
}
560583

561584
@Test
@@ -572,7 +595,8 @@ void setRelationEntityIdNotFound() throws Exception {
572595
mockMvc.perform(put("/invoices/{sourceId}/previous-invoice", INVOICE_ID)
573596
.contentType("text/uri-list")
574597
.content("http://localhost/invoices/%s%n".formatted(targetId)))
575-
.andExpect(status().isNotFound());
598+
.andExpect(status().isNotFound())
599+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
576600
}
577601

578602
@Test
@@ -589,7 +613,8 @@ void setRelationForeignKeyConstraintViolation() throws Exception {
589613
mockMvc.perform(put("/invoices/{sourceId}/previous-invoice", INVOICE_ID)
590614
.contentType("text/uri-list")
591615
.content("http://localhost/invoices/%s%n".formatted(targetId)))
592-
.andExpect(status().isBadRequest());
616+
.andExpect(status().isBadRequest())
617+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
593618
}
594619

595620
@Test
@@ -609,7 +634,8 @@ void addRelationEntityIdNotFound() throws Exception {
609634
.contentType("text/uri-list")
610635
.content("http://localhost/invoices/%s%nhttp://localhost/invoices/%s%n".formatted(invoice1,
611636
invoice2)))
612-
.andExpect(status().isNotFound());
637+
.andExpect(status().isNotFound())
638+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
613639
}
614640

615641
@Test
@@ -629,7 +655,8 @@ void addRelationForeignKeyConstraintViolation() throws Exception {
629655
.contentType("text/uri-list")
630656
.content("http://localhost/invoices/%s%nhttp://localhost/invoices/%s%n".formatted(invoice1,
631657
invoice2)))
632-
.andExpect(status().isBadRequest());
658+
.andExpect(status().isBadRequest())
659+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
633660
}
634661

635662
@Test
@@ -638,7 +665,8 @@ void clearRelationEntityIdNotFound() throws Exception {
638665
.deleteRelation(TestApplication.APPLICATION, TestApplication.INVOICE_PREVIOUS, INVOICE_ID);
639666

640667
mockMvc.perform(delete("/invoices/{sourceId}/previous-invoice", INVOICE_ID))
641-
.andExpect(status().isNotFound());
668+
.andExpect(status().isNotFound())
669+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
642670
}
643671

644672
@Test
@@ -647,7 +675,8 @@ void clearRelationForeignKeyRequired() throws Exception {
647675
.deleteRelation(TestApplication.APPLICATION, TestApplication.INVOICE_PREVIOUS, INVOICE_ID);
648676

649677
mockMvc.perform(delete("/invoices/{sourceId}/previous-invoice", INVOICE_ID))
650-
.andExpect(status().isBadRequest());
678+
.andExpect(status().isBadRequest())
679+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
651680
}
652681

653682
@Test
@@ -656,7 +685,8 @@ void removeRelationDataEntityIdNotFound() throws Exception {
656685
.removeRelationItem(TestApplication.APPLICATION, TestApplication.PERSON_INVOICES, PERSON_ID, INVOICE_ID);
657686

658687
mockMvc.perform(delete("/persons/{sourceId}/invoices/{targetId}", PERSON_ID, INVOICE_ID))
659-
.andExpect(status().isNotFound());
688+
.andExpect(status().isNotFound())
689+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
660690
}
661691

662692
@Test
@@ -665,7 +695,8 @@ void removeRelationDataForeignKeyRequired() throws Exception {
665695
.removeRelationItem(TestApplication.APPLICATION, TestApplication.PERSON_INVOICES, PERSON_ID, INVOICE_ID);
666696

667697
mockMvc.perform(delete("/persons/{sourceId}/invoices/{targetId}", PERSON_ID, INVOICE_ID))
668-
.andExpect(status().isBadRequest());
698+
.andExpect(status().isBadRequest())
699+
.andExpect(content().contentType(MediaType.APPLICATION_PROBLEM_JSON));
669700
}
670701

671702
}

0 commit comments

Comments
 (0)