1
1
/*
2
- * Copyright 2013-2019 the original author or authors.
2
+ * Copyright 2013-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package org .springsource .restbucks ;
17
17
18
- import static org .assertj .core .api .Assertions .*;
19
-
20
- import lombok .RequiredArgsConstructor ;
18
+ import lombok .SneakyThrows ;
21
19
22
20
import java .util .Locale ;
23
- import java .util .Optional ;
24
21
22
+ import org .assertj .core .api .Condition ;
25
23
import org .junit .jupiter .api .BeforeEach ;
26
24
import org .springframework .beans .factory .annotation .Autowired ;
27
25
import org .springframework .boot .test .context .SpringBootTest ;
28
- import org .springframework .hateoas .Link ;
29
26
import org .springframework .hateoas .LinkRelation ;
30
27
import org .springframework .hateoas .client .LinkDiscoverer ;
31
28
import org .springframework .hateoas .client .LinkDiscoverers ;
32
29
import org .springframework .mock .web .MockHttpServletResponse ;
33
- import org .springframework .test .web .servlet .MockMvc ;
34
- import org .springframework .test .web .servlet .MvcResult ;
35
- import org .springframework .test .web .servlet .ResultMatcher ;
30
+ import org .springframework .test .web .servlet .assertj .AssertableMockMvc ;
31
+ import org .springframework .test .web .servlet .assertj .AssertableMvcResult ;
36
32
import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
37
33
import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
34
+ import org .springframework .util .Assert ;
38
35
import org .springframework .web .context .WebApplicationContext ;
39
36
40
37
/**
@@ -48,66 +45,43 @@ public abstract class AbstractWebIntegrationTest {
48
45
@ Autowired WebApplicationContext context ;
49
46
@ Autowired LinkDiscoverers links ;
50
47
51
- protected MockMvc mvc ;
48
+ protected AssertableMockMvc mvc ;
52
49
53
50
@ BeforeEach
54
51
void setUp () {
55
52
56
- mvc = MockMvcBuilders .webAppContextSetup (context ).//
53
+ this . mvc = AssertableMockMvc . create ( MockMvcBuilders .webAppContextSetup (context ).//
57
54
defaultRequest (MockMvcRequestBuilders .get ("/" ).locale (Locale .US )).//
58
- build ();
55
+ build ()) ;
59
56
}
60
57
61
58
/**
62
- * Creates a {@link ResultMatcher} that checks for the presence of a link with the given rel.
59
+ * Creates a AssertJ {@link Condition} that checks for the presence of a {@link Link} with the given
60
+ * {@link LinkRelation}.
63
61
*
64
- * @param rel
65
- * @return
62
+ * @param rel must not be {@literal null}.
63
+ * @return will never be {@literal null}.
66
64
*/
67
- protected ResultMatcher linkWithRelIsPresent (LinkRelation rel ) {
68
- return new LinkWithRelMatcher (rel , true );
69
- }
65
+ protected Condition <AssertableMvcResult > linkWithRel (LinkRelation rel ) {
70
66
71
- /**
72
- * Creates a {@link ResultMatcher} that checks for the non-presence of a link with the given rel.
73
- *
74
- * @param rel
75
- * @return
76
- */
77
- protected ResultMatcher linkWithRelIsNotPresent (LinkRelation rel ) {
78
- return new LinkWithRelMatcher (rel , false );
67
+ Assert .notNull (rel , "LinkRelation must not be null!" );
68
+
69
+ return new Condition <>(it -> hasLink (it , rel ), "Expected to find link with relation %s!" , rel );
79
70
}
80
71
72
+ @ SuppressWarnings ("null" )
81
73
protected LinkDiscoverer getDiscovererFor (MockHttpServletResponse response ) {
82
74
return links .getRequiredLinkDiscovererFor (response .getContentType ());
83
75
}
84
76
85
- @ RequiredArgsConstructor
86
- private class LinkWithRelMatcher implements ResultMatcher {
87
-
88
- private final LinkRelation rel ;
89
- private final boolean present ;
90
-
91
- /*
92
- * (non-Javadoc)
93
- * @see org.springframework.test.web.servlet.ResultMatcher#match(org.springframework.test.web.servlet.MvcResult)
94
- */
95
- @ Override
96
- public void match (MvcResult result ) throws Exception {
97
-
98
- MockHttpServletResponse response = result .getResponse ();
99
- String content = response .getContentAsString ();
100
- LinkDiscoverer discoverer = links .getRequiredLinkDiscovererFor (response .getContentType ());
101
-
102
- Optional <Link > link = discoverer .findLinkWithRel (rel , content );
103
-
104
- assertThat (link ).matches (it -> it .isPresent () == present , getMessage (link ));
105
- }
77
+ @ SneakyThrows
78
+ @ SuppressWarnings ("null" )
79
+ private boolean hasLink (AssertableMvcResult result , LinkRelation rel ) {
106
80
107
- private String getMessage (Optional <Link > link ) {
81
+ var response = result .getResponse ();
82
+ var content = response .getContentAsString ();
83
+ var discoverer = links .getRequiredLinkDiscovererFor (response .getContentType ());
108
84
109
- return String .format ("Expected to %s link with relation %s, but found %s!" ,
110
- present ? "find" : "not find" , rel , present ? link .get () : "none" );
111
- }
85
+ return discoverer .findLinkWithRel (rel , content ).isPresent ();
112
86
}
113
87
}
0 commit comments