From a2ed74ea9dd142bbf19d314c97ceea99cb390dcf Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 11 Oct 2025 15:58:42 +0200 Subject: [PATCH] test: Implemented a test for the pact with the frontend --- .gitmodules | 4 + pom.xml | 7 ++ .../chat/backend/api/PactProviderTest.java | 77 +++++++++++++++++++ src/test/resources/pacts | 1 + 4 files changed, 89 insertions(+) create mode 100644 .gitmodules create mode 100644 src/test/java/de/juplo/kafka/chat/backend/api/PactProviderTest.java create mode 160000 src/test/resources/pacts diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..3a79d78f --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "src/test/resources/pacts"] + path = src/test/resources/pacts + url = ./ + branch = pacts diff --git a/pom.xml b/pom.xml index 50310cf3..4bcc5638 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,7 @@ 17 1.0.8 0.3.3 + 4.6.17 @@ -112,6 +113,12 @@ ${inject-resources-junit-jupiter.version} test + + au.com.dius.pact.provider + junit5 + ${pact.version} + test + diff --git a/src/test/java/de/juplo/kafka/chat/backend/api/PactProviderTest.java b/src/test/java/de/juplo/kafka/chat/backend/api/PactProviderTest.java new file mode 100644 index 00000000..5c152056 --- /dev/null +++ b/src/test/java/de/juplo/kafka/chat/backend/api/PactProviderTest.java @@ -0,0 +1,77 @@ +package de.juplo.kafka.chat.backend.api; + +import au.com.dius.pact.provider.junit5.HttpTestTarget; +import au.com.dius.pact.provider.junit5.PactVerificationContext; +import au.com.dius.pact.provider.junit5.PactVerificationInvocationContextProvider; +import au.com.dius.pact.provider.junitsupport.Provider; +import au.com.dius.pact.provider.junitsupport.State; +import au.com.dius.pact.provider.junitsupport.loader.PactFolder; +import de.juplo.kafka.chat.backend.domain.ChatHomeService; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.TestTemplate; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.util.UUID; + + +@Provider("ChatBackendController") +@PactFolder("src/test/resources/pacts") +@ExtendWith(SpringExtension.class) +@SpringBootTest( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = { + "chat.backend.inmemory.storage-strategy=none", + "chat.backend.inmemory.sharding-strategy=kafkalike", + "chat.backend.inmemory.num-shards=10", + "chat.backend.inmemory.owned-shards=2" }) +@Slf4j +public class PactProviderTest +{ + + @Autowired + ChatHomeService chatHomeService; + + @LocalServerPort + int port; + + @BeforeEach + void setUp(PactVerificationContext context) { + context.setTarget(new HttpTestTarget("localhost", port)); + } + + @TestTemplate + @ExtendWith(PactVerificationInvocationContextProvider.class) + void verifyPact(PactVerificationContext context) { + context.verifyInteraction(); + } + + @State("there are 10 shards") + void stateThereAre10Shards() {} + + @State("the server is responsible for shard 2") + void stateTheServerOwnsShard2() {} + + @State("chatroom 5c73531c-6fc4-426c-adcb-afc5c140a0f7 exists in shard 2") + void stateChatroomExistsInOwnedShard() { + chatHomeService.createChatRoom(UUID.fromString("5c73531c-6fc4-426c-adcb-afc5c140a0f7"), "FOO"); + } + + @State("chatroom 7f59ec77-832e-4a17-8d22-55ef46242c17 does not exists in shard 2") + void stateChatroomDosNotExistsInOwnedShard() {} + + @State("there are no chatrooms available at all in shard 2") + void stateNoChatroomsAvailableInOwnedShard() { + chatHomeService + .getChatRoomInfo() + .subscribe(chatRoomInfo -> + { + log.debug("Removing chatroom {}", chatRoomInfo); + chatHomeService.removeChatRoom(chatRoomInfo.getId()); + }); + } +} diff --git a/src/test/resources/pacts b/src/test/resources/pacts new file mode 160000 index 00000000..b2bf161f --- /dev/null +++ b/src/test/resources/pacts @@ -0,0 +1 @@ +Subproject commit b2bf161fa71acd4128b040ebb81af65fc1509cac -- 2.39.5