This repository was archived by the owner on Apr 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUpdateUserStatusApplicationTests.java
More file actions
83 lines (64 loc) · 2.63 KB
/
UpdateUserStatusApplicationTests.java
File metadata and controls
83 lines (64 loc) · 2.63 KB
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
package scratches.boot.cloud.user;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClients;
import org.bson.types.ObjectId;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import java.io.IOException;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static scratches.boot.cloud.user.LocalServerTestSupport.startServer;
import static scratches.boot.cloud.user.UserStatus.ACTIVE;
import static scratches.boot.cloud.user.UserStatus.DORMANT;
@Testcontainers
class UpdateUserStatusApplicationTests {
@Container
static final MongoDBContainer MONGO_DB_CONTAINER = new MongoDBContainer(DockerImageName.parse("mongo"));
private static MongoTemplate template;
private static User user;
@BeforeAll
static void setup() {
template = setupMongo();
user = template.insert(new User(ACTIVE));
}
@Test
@DisplayName("User status will be updated based on provided value in message")
void contextLoads() throws IOException, InterruptedException {
var restTemplate = new TestRestTemplate();
var message = messageWithNewUserStatus();
try(var process = startServer(UpdateUserStatusApplication.class, MONGO_DB_CONTAINER)) {
restTemplate.postForObject("http://localhost:8080/", message, String.class);
}
var updatedUser = findById(user.getId());
assertThat(updatedUser.getStatus()).isEqualTo(DORMANT);
}
private static MongoTemplate setupMongo() {
var client = MongoClients.create(
MongoClientSettings.builder()
.applyConnectionString(
new ConnectionString(MONGO_DB_CONTAINER.getReplicaSetUrl())
)
.build()
);
return new MongoTemplate(client, "test");
}
private User findById(ObjectId id) {
return template.findById(id, User.class);
}
private PubSubMessage messageWithNewUserStatus() {
var message = new PubSubMessage();
message.setAttributes(Map.of(
"id", user.getId().toString(),
"status", DORMANT.name()
));
return message;
}
}