88import io .securecodebox .persistence .defectdojo .config .Config ;
99import io .securecodebox .persistence .defectdojo .model .User ;
1010import io .securecodebox .persistence .defectdojo .model .UserProfile ;
11- import org .junit .jupiter .api .BeforeEach ;
1211import org .junit .jupiter .api .Test ;
1312import org .springframework .http .MediaType ;
1413import org .springframework .test .web .client .MockRestServiceServer ;
1514
1615import java .net .URISyntaxException ;
1716import java .util .Arrays ;
17+ import java .util .List ;
1818
1919import static org .junit .jupiter .api .Assertions .assertIterableEquals ;
2020import static org .springframework .test .web .client .match .MockRestRequestMatchers .requestTo ;
2121import static org .springframework .test .web .client .response .MockRestResponseCreators .withSuccess ;
2222
2323
24- // This test is special because the defectdojo api does not return a list,
25- // but the generic code assumes every endpoint returns a list
24+ /**
25+ * Tests for UserProfileService
26+ * <p>
27+ * This test is special because the defectdojo api does not return a list, but the generic code assumes every endpoint
28+ * returns a list.
29+ * </p>
30+ */
2631class UserProfileServiceTest {
27-
28- private Config config ;
29- private UserProfileService underTest ;
30- private MockRestServiceServer mockServer ;
31-
32- // This string does not contain every field of the api response as those are not implemented
33- private String apiResponse = """
32+ /**
33+ * This string does not contain every field of the api response as those are not implemented
34+ */
35+ private static final String API_RESPONSE = """
3436 {
3537 "user": {
3638 "id": 0,
37- "username": "GdqmXprK.j7R+OYE49SzL3mM2U6I0DyLRHnDg87i9It0AfP-kxvswW3qOI2i+31-@0 ",
38- "first_name": "string ",
39- "last_name": "string ",
39+ "username": "username ",
40+ "first_name": "first_name ",
41+ "last_name": "last_name ",
4042 "email": "user@example.com",
4143 "last_login": "2022-11-01T16:20:19.373Z",
4244 "is_active": true,
@@ -46,24 +48,18 @@ class UserProfileServiceTest {
4648 }
4749 """ ;
4850
49- @ BeforeEach
50- void setup () {
51- config = new Config ("https://defectdojo.example.com" , "abc" , 42 );
52- underTest = new UserProfileService (config );
53- mockServer = MockRestServiceServer .createServer (underTest .getRestTemplate ());
54- }
51+ private final Config config = new Config ("https://defectdojo.example.com" , "abc" , 42 );
52+ private final UserProfileService sut = new UserProfileService (config );
53+ private final MockRestServiceServer server = MockRestServiceServer .createServer (sut .getRestTemplate ());
5554
5655 @ Test
57- void testSearch () throws JsonProcessingException , URISyntaxException {
58- var url = config . getUrl () + " /api/v2/" + underTest . getUrlPath () + " /?offset=0&limit=100" ;
59- mockServer .expect (requestTo (url )).andRespond (withSuccess (apiResponse , MediaType .APPLICATION_JSON ));
56+ void search () throws JsonProcessingException , URISyntaxException {
57+ final var url = String . format ( "%s /api/v2/%s /?offset=0&limit=100", config . getUrl (), sut . getUrlPath ()) ;
58+ server .expect (requestTo (url )).andRespond (withSuccess (API_RESPONSE , MediaType .APPLICATION_JSON ));
6059
61- var user = new User (0L , "GdqmXprK.j7R+OYE49SzL3mM2U6I0DyLRHnDg87i9It0AfP-kxvswW3qOI2i+31-@0" , "string" , "string" );
62- var userProfile = new UserProfile (user );
63- var expected = Arrays .asList (userProfile );
64- var actual = underTest .search ();
60+ final var expected = new UserProfile (new User (0L , "username" , "first_name" , "last_name" ));
6561
66- mockServer . verify ( );
67- assertIterableEquals ( expected , actual );
62+ assertIterableEquals ( List . of ( expected ), sut . search () );
63+ server . verify ( );
6864 }
6965}
0 commit comments