33import com .springqprobackend .springqpro .domain .entity .TaskEntity ;
44import com .springqprobackend .springqpro .enums .TaskStatus ;
55import com .springqprobackend .springqpro .repository .TaskRepository ;
6+ import com .springqprobackend .springqpro .security .dto .AuthResponse ;
67import com .springqprobackend .springqpro .testcontainers .IntegrationTestBase ;
78import org .junit .jupiter .api .BeforeEach ;
8- import org .junit .jupiter .api .Disabled ;
9- import org .junit .jupiter .api .Tag ;
109import org .springframework .beans .factory .annotation .Autowired ;
11- import org .springframework .boot .test .autoconfigure . data . redis . DataRedisTest ;
12- import org .springframework .boot . test . web . client . TestRestTemplate ;
13- import org .springframework .http .ResponseEntity ;
10+ import org .springframework .boot .test .context . SpringBootTest ;
11+ import org .springframework .http . HttpHeaders ;
12+ import org .springframework .http .MediaType ;
1413import org .junit .jupiter .api .Test ;
15- import org .testcontainers . junit . jupiter . Testcontainers ;
14+ import org .springframework . test . web . reactive . server . WebTestClient ;
1615import org .testcontainers .shaded .org .awaitility .Awaitility ;
1716
1817import java .time .Duration ;
1918import java .util .Map ;
2019import java .util .Optional ;
20+ import java .util .concurrent .atomic .AtomicReference ;
2121
2222import static org .assertj .core .api .Assertions .*;
2323
@@ -55,10 +55,12 @@ public void sweepQueuedTasks() {
5555*/
5656//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
5757//@Tag("disable_temp")
58- class CreateAndProcessTaskIntegrationTest extends IntegrationTestBase {
59- @ Autowired
60- private TestRestTemplate rest ;
61-
58+ @ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT )
59+ class CreateAndProcessTaskIntegrationTest extends AbstractAuthenticatedIntegrationTest {
60+ //@Autowired
61+ //private TestRestTemplate rest;
62+ //@Autowired
63+ //private WebTestClient webTestClient;
6264 @ Autowired
6365 private TaskRepository taskRepository ;
6466
@@ -67,16 +69,88 @@ void cleanDb() {
6769 taskRepository .deleteAll ();
6870 }
6971
70- @ Disabled ("Outdated architecture — will fix later" )
72+ // HELPER METHODS - CreateAndProcessTaskIntegrationTest-specific:
73+ // [1] - GraphQL task creation mutation template function:
74+ private String createTaskAs (String email , String password , String payload ) {
75+ AuthResponse auth = registerAndLogin (email , password );
76+ String mutation = """
77+ mutation {
78+ createTask(input: {
79+ payload: "%s"
80+ type: EMAIL
81+ }) {
82+ id
83+ status
84+ createdBy
85+ }
86+ }
87+ """ .formatted (payload );
88+
89+ AtomicReference <String > taskIdRef = new AtomicReference <>();
90+ graphQLWithToken (auth .accessToken (), mutation )
91+ .expectStatus ().isOk ()
92+ .expectBody ()
93+ .jsonPath ("$.data.createTask.id" )
94+ .value (id -> taskIdRef .set ((String ) id ));
95+ return taskIdRef .get ();
96+ }
97+
98+ // TEST(S):
99+ @ Test
100+ void authenticatedUser_canCreateTask_andTaskIsEventuallyProcessed () {
101+ String email = "worker@test.com" ;
102+ String taskId = createTaskAs (email , "secure-pass" , "integration-test" );
103+
104+ // Assert persistence + ownership
105+ Awaitility .await ()
106+ .atMost (Duration .ofSeconds (3 ))
107+ .untilAsserted (() -> {
108+ TaskEntity task = taskRepository .findById (taskId ).orElseThrow ();
109+ assertThat (task .getCreatedBy ()).isEqualTo (email );
110+ assertThat (task .getStatus ()).isEqualTo (TaskStatus .QUEUED );
111+ });
112+
113+ // Assert eventual processing
114+ Awaitility .await ()
115+ .atMost (Duration .ofSeconds (5 ))
116+ .untilAsserted (() -> {
117+ TaskEntity task = taskRepository .findById (taskId ).orElseThrow ();
118+ assertThat (task .getStatus ())
119+ .isIn (TaskStatus .COMPLETED , TaskStatus .FAILED );
120+ });
121+ }
122+
123+ @ Test
124+ void createTask_setsCreatedByToAuthenticatedUser () {
125+ String taskId = createTaskAs ("a@test.com" , "pw" , "ownership-test" );
126+
127+ TaskEntity task = taskRepository .findById (taskId ).orElseThrow ();
128+ assertThat (task .getCreatedBy ()).isEqualTo ("a@test.com" );
129+ }
130+
131+ @ Test
132+ void createdTask_doesNotRemainQueuedForever () {
133+ String taskId = createTaskAs ("queue@test.com" , "pw" , "queue-drain-test" );
134+
135+ Awaitility .await ()
136+ .atMost (Duration .ofSeconds (5 ))
137+ .untilAsserted (() -> {
138+ TaskEntity task = taskRepository .findById (taskId ).orElseThrow ();
139+ assertThat (task .getStatus ()).isNotEqualTo (TaskStatus .QUEUED );
140+ });
141+ }
142+
143+ // OLD STUFF:
144+ /*@Disabled("Outdated architecture")
71145 @Test
72146 void createTask_isPersisted_andEventuallyProcessed() {
73147 // Starting w/ creating Task via REST endpoint (my ProducerController's POST /enqueue):
74148 Map<String, Object> req = Map.of("payload", "integration-test", "type", "EMAIL");
75149 ResponseEntity<Map> response = rest.postForEntity("/api/enqueue", req, Map.class);
76150 assertThat(response.getStatusCode().is2xxSuccessful());
77151
78- /* After the Request is enqueued, that Task is saved to the DataBase and sent to the in-memory QueueService pool.
79- To make sure that the Task was persisted, we can check the most recent row in our DB: * /
152+ / After the Request is enqueued, that Task is saved to the DataBase and sent to the in-memory QueueService pool.
153+ To make sure that the Task was persisted, we can check the most recent row in our DB: /
80154 Awaitility.await()
81155 .atMost(Duration.ofSeconds(5))
82156 .pollInterval(Duration.ofMillis(200))
@@ -96,5 +170,5 @@ void createTask_isPersisted_andEventuallyProcessed() {
96170 Optional<TaskEntity> refreshed = taskRepository.findById(entity.getId());
97171 return refreshed.map(e -> e.getStatus() == TaskStatus.COMPLETED || e.getStatus() == TaskStatus.FAILED).orElse(false);
98172 });
99- }
173+ }*/
100174}
0 commit comments