|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2021 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.
|
|
40 | 40 |
|
41 | 41 | import static org.assertj.core.api.Assertions.assertThat;
|
42 | 42 |
|
| 43 | +/** |
| 44 | + * Tests for {@link SampleSessionMongoApplication}. |
| 45 | + * |
| 46 | + * @author Angel L. Villalain |
| 47 | + */ |
43 | 48 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
44 | 49 | @Testcontainers(disabledWithoutDocker = true)
|
45 |
| -public class SampleHttpSessionMongoApplicationTests { |
| 50 | +public class SampleSessionMongoApplicationTests { |
46 | 51 |
|
47 |
| - static final String USERNAME = "user"; |
48 |
| - static final String PASSWORD = "password"; |
49 |
| - static final String ROOT = "/"; |
| 52 | + private static final String USERNAME = "user"; |
| 53 | + |
| 54 | + private static final String PASSWORD = "password"; |
| 55 | + |
| 56 | + @Autowired |
| 57 | + private TestRestTemplate restTemplate; |
50 | 58 |
|
51 | 59 | @Container
|
52 | 60 | static MongoDBContainer mongo = new MongoDBContainer(DockerImageNames.mongo()).withStartupAttempts(3)
|
53 | 61 | .withStartupTimeout(Duration.ofMinutes(2));
|
54 | 62 |
|
55 |
| - @Autowired |
56 |
| - private TestRestTemplate template; |
57 |
| - |
58 | 63 | @DynamicPropertySource
|
59 | 64 | static void applicationProperties(DynamicPropertyRegistry registry) {
|
60 |
| - registry.add("spring.security.user.name", () -> USERNAME); |
61 |
| - registry.add("spring.security.user.password", () -> PASSWORD); |
62 | 65 | registry.add("spring.data.mongodb.uri", mongo::getReplicaSetUrl);
|
63 | 66 | }
|
64 | 67 |
|
65 | 68 | @Test
|
66 | 69 | @SuppressWarnings("unchecked")
|
67 | 70 | void sessionsEndpointShouldReturnUserSessions() {
|
68 | 71 | createSession();
|
69 |
| - ResponseEntity<Map<String, Object>> response = this.getSessions(); |
| 72 | + ResponseEntity<Map<String, Object>> response = getSessions(); |
70 | 73 | assertThat(response).isNotNull();
|
71 | 74 | assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
72 | 75 | List<Map<String, Object>> sessions = (List<Map<String, Object>>) response.getBody().get("sessions");
|
73 | 76 | assertThat(sessions.size()).isEqualTo(1);
|
74 | 77 | }
|
75 | 78 |
|
76 | 79 | private void createSession() {
|
77 |
| - URI uri = URI.create(ROOT); |
| 80 | + URI uri = URI.create("/"); |
78 | 81 | HttpHeaders headers = new HttpHeaders();
|
79 | 82 | headers.setBasicAuth(USERNAME, PASSWORD);
|
80 | 83 | RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
|
81 |
| - this.template.exchange(request, String.class); |
| 84 | + this.restTemplate.exchange(request, String.class); |
82 | 85 | }
|
83 | 86 |
|
84 | 87 | @SuppressWarnings("unchecked")
|
85 | 88 | private ResponseEntity<Map<String, Object>> getSessions() {
|
86 |
| - return (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.template.withBasicAuth(USERNAME, PASSWORD) |
87 |
| - .getForEntity("/actuator/sessions?username=" + USERNAME, Map.class); |
| 89 | + return (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.restTemplate |
| 90 | + .withBasicAuth(USERNAME, PASSWORD).getForEntity("/actuator/sessions?username=" + USERNAME, Map.class); |
88 | 91 | }
|
89 | 92 |
|
90 | 93 | }
|
0 commit comments