Skip to content

Commit cdbf084

Browse files
authored
fix running of tests (#22)
1 parent 31ae0cd commit cdbf084

31 files changed

Lines changed: 173 additions & 679 deletions

File tree

Makefile

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,39 @@
1-
@PHONY: run-dev
1+
.PHONY: run-dev test test-all test-api-gateway test-document-service test-eureka test-hello-service
22

33
run-dev:
4-
docker compose up --build
4+
docker compose up --build
5+
6+
# Directories of all server microservices that contain a Gradle wrapper
7+
SERVER_SERVICES := \
8+
server/api-gateway \
9+
server/document-service \
10+
server/eureka \
11+
server/hello-service
12+
13+
# Default target to run the test-suite of every microservice
14+
# Usage: `make test`
15+
16+
test: test-all
17+
18+
# Iterate over every service directory and execute its Gradle test task
19+
# This keeps the output of each microservice separated for clarity.
20+
21+
test-all:
22+
@for dir in $(SERVER_SERVICES); do \
23+
echo "\n===== Running tests for $$dir ====="; \
24+
(cd $$dir && ./gradlew --quiet test); \
25+
done
26+
27+
# Convenience targets to run tests for an individual microservice
28+
29+
test-api-gateway:
30+
@$(MAKE) -C server/api-gateway test
31+
32+
test-document-service:
33+
@$(MAKE) -C server/document-service test
34+
35+
test-eureka:
36+
@$(MAKE) -C server/eureka test
37+
38+
test-hello-service:
39+
@$(MAKE) -C server/hello-service test
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package de.tum.team_sigma.api_gateway;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.test.web.servlet.MockMvc;
9+
10+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
11+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
13+
14+
@SpringBootTest
15+
@AutoConfigureMockMvc
16+
class GatewayControllerTest {
17+
18+
@Autowired
19+
private MockMvc mockMvc;
20+
21+
@Test
22+
@DisplayName("GET /api/gateway/health returns 200 and status UP")
23+
void healthEndpointShouldReturnUp() throws Exception {
24+
mockMvc.perform(get("/api/gateway/health"))
25+
.andExpect(status().isOk())
26+
.andExpect(jsonPath("$.status").value("UP"));
27+
}
28+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Disable Eureka discovery during tests
2+
eureka.client.enabled=false
3+
eureka.client.fetch-registry=false
4+
eureka.client.register-with-eureka=false
5+
spring.cloud.discovery.enabled=false

server/document-service/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ dependencyManagement {
6666

6767
tasks.named('test') {
6868
useJUnitPlatform()
69+
70+
// Exclude heavyweight container-based integration tests by default. They can
71+
// be run separately with `./gradlew integrationTest` or by removing this
72+
// exclusion.
73+
exclude '**/*ContainerIntegrationTest*'
6974
}
7075

7176
// Skip tests when building with -x test

server/document-service/src/main/java/de/tum/team_sigma/document_service/config/MinioConfig.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ public class MinioConfig {
2626
@Value("${minio.bucket-name}")
2727
private String bucketName;
2828

29+
/**
30+
* Allows tests to disable the network call that verifies/creates the bucket.
31+
* Defaults to <code>false</code> so production behaviour is unchanged.
32+
*/
33+
@Value("${minio.skip-check:false}")
34+
private boolean skipBucketCheck;
35+
2936
@Bean
3037
public MinioClient minioClient() {
3138
try {
@@ -34,8 +41,12 @@ public MinioClient minioClient() {
3441
.credentials(accessKey, secretKey)
3542
.build();
3643

37-
// Create bucket if it doesn't exist
38-
createBucketIfNotExists(client);
44+
// Optionally create bucket if it doesn't exist (skipped in tests)
45+
if (!skipBucketCheck) {
46+
createBucketIfNotExists(client);
47+
} else {
48+
logger.debug("Skipping MinIO bucket existence check (minio.skip-check=true)");
49+
}
3950

4051
return client;
4152
} catch (Exception e) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package de.tum.team_sigma.document_service.controller;
2+
3+
import de.tum.team_sigma.document_service.service.DocumentService;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
8+
import org.springframework.boot.test.mock.mockito.MockBean;
9+
import org.springframework.test.web.servlet.MockMvc;
10+
11+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
13+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
14+
15+
@WebMvcTest(DocumentController.class)
16+
class DocumentControllerHealthTest {
17+
18+
@Autowired
19+
private MockMvc mockMvc;
20+
21+
// Mock service to avoid full application context and external dependencies
22+
@MockBean
23+
private DocumentService documentService;
24+
25+
@Test
26+
@DisplayName("GET /api/documents/ returns health string")
27+
void healthEndpointShouldReturnRunningString() throws Exception {
28+
mockMvc.perform(get("/api/documents/"))
29+
.andExpect(status().isOk())
30+
.andExpect(content().string("Document Service is running!"));
31+
}
32+
}

server/document-service/src/test/resources/application-test.properties

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,15 @@ eureka.client.fetch-registry=false
1212

1313
# Disable other unnecessary services for integration tests
1414
spring.cloud.discovery.enabled=false
15-
management.endpoints.enabled=false
15+
management.endpoints.enabled=false
16+
17+
# ---------------- MinIO stub configuration ----------------
18+
# Using dummy credentials and endpoint as tests should not perform real network calls.
19+
minio.url=http://localhost:9000
20+
minio.access-key=minio
21+
minio.secret-key=miniopass
22+
minio.bucket-name=testbucket
23+
minio.skip-check=true
24+
25+
# ---------------- Weaviate stub configuration ----------------
26+
weaviate.url=localhost:8081
-17 Bytes
Binary file not shown.
-31.3 KB
Binary file not shown.
-77.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)