master- Sync Repo
This commit is contained in:
13
src/main/java/de/kreuzwerker/cdc/userservice/Friend.java
Normal file
13
src/main/java/de/kreuzwerker/cdc/userservice/Friend.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package de.kreuzwerker.cdc.userservice;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class Friend {
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package de.kreuzwerker.cdc.userservice;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ControllerAdvice
|
||||
public class GlobalControllerExceptionHandler {
|
||||
|
||||
@ExceptionHandler(NotFoundException.class)
|
||||
@ResponseStatus(HttpStatus.NOT_FOUND)
|
||||
public void handleNotFound() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package de.kreuzwerker.cdc.userservice;
|
||||
|
||||
public class NotFoundException extends RuntimeException {
|
||||
|
||||
}
|
||||
22
src/main/java/de/kreuzwerker/cdc/userservice/User.java
Normal file
22
src/main/java/de/kreuzwerker/cdc/userservice/User.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package de.kreuzwerker.cdc.userservice;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.Singular;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class User {
|
||||
|
||||
private String id;
|
||||
private String legacyId;
|
||||
private String name;
|
||||
private UserRole role;
|
||||
private Date lastLogin;
|
||||
@Singular
|
||||
private List<Friend> friends;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package de.kreuzwerker.cdc.userservice;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
public UserController(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@GetMapping("/users/{userId}")
|
||||
public User getUser(@PathVariable String userId) {
|
||||
return userService.findUser(userId);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package de.kreuzwerker.cdc.userservice;
|
||||
|
||||
public enum UserRole {
|
||||
|
||||
ADMIN,
|
||||
|
||||
USER;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package de.kreuzwerker.cdc.userservice;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
public User findUser(String userId) {
|
||||
return User.builder()
|
||||
.id(userId)
|
||||
.legacyId(UUID.randomUUID().toString())
|
||||
.name("Beth")
|
||||
.role(UserRole.ADMIN)
|
||||
.lastLogin(new Date())
|
||||
.friend(Friend.builder().id("2").name("Ronald Smith").build())
|
||||
.friend(Friend.builder().id("3").name("Matt Spencer").build())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package de.kreuzwerker.cdc.userservice;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class UserServiceApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(UserServiceApplication.class, args);
|
||||
}
|
||||
}
|
||||
8
src/main/resources/application.yml
Normal file
8
src/main/resources/application.yml
Normal file
@@ -0,0 +1,8 @@
|
||||
server:
|
||||
port: 8090
|
||||
|
||||
spring:
|
||||
jackson:
|
||||
serialization:
|
||||
write-dates-as-timestamps: false
|
||||
date-format: yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
|
||||
@@ -0,0 +1,41 @@
|
||||
package de.kreuzwerker.cdc.userservice;
|
||||
|
||||
import au.com.dius.pact.provider.junit.Provider;
|
||||
import au.com.dius.pact.provider.junit.State;
|
||||
import au.com.dius.pact.provider.junit.loader.PactFolder;
|
||||
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 org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.TestTemplate;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
|
||||
@Provider("user-service")
|
||||
@PactFolder("pacts")
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@Disabled
|
||||
public class ContractTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@BeforeEach
|
||||
void before(PactVerificationContext context) {
|
||||
context.setTarget(new HttpTestTarget("localhost", port));
|
||||
}
|
||||
|
||||
@TestTemplate
|
||||
@ExtendWith(PactVerificationInvocationContextProvider.class)
|
||||
void pactVerificationTestTemplate(PactVerificationContext context) {
|
||||
context.verifyInteraction();
|
||||
}
|
||||
@State("User 1 exists")
|
||||
public void user1Exists() {
|
||||
// nothing to do, real service is used
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package de.kreuzwerker.cdc.userservice;
|
||||
|
||||
import au.com.dius.pact.provider.junit.Provider;
|
||||
import au.com.dius.pact.provider.junit.State;
|
||||
import au.com.dius.pact.provider.junit.loader.PactBroker;
|
||||
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 org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.TestTemplate;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@Provider("user-service")
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
//pact_broker is the service name in docker-compose
|
||||
@PactBroker(host = "pact_broker", tags = "${pactbroker.tags:prod}")
|
||||
public class GenericStateWithParameterContractTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@BeforeEach
|
||||
void before(PactVerificationContext context) {
|
||||
context.setTarget(new HttpTestTarget("localhost", port));
|
||||
}
|
||||
|
||||
@TestTemplate
|
||||
@ExtendWith(PactVerificationInvocationContextProvider.class)
|
||||
void pactVerificationTestTemplate(PactVerificationContext context) {
|
||||
context.verifyInteraction();
|
||||
}
|
||||
|
||||
@MockBean
|
||||
private UserService userService;
|
||||
|
||||
@State("default")
|
||||
public void toDefaultState(Map<String, Object> params) {
|
||||
final boolean userExists = (boolean) params.get("userExists");
|
||||
if (userExists) {
|
||||
when(userService.findUser(any())).thenReturn(User.builder()
|
||||
.id("1")
|
||||
.legacyId(UUID.randomUUID().toString())
|
||||
.name("Beth")
|
||||
.role(UserRole.ADMIN)
|
||||
.lastLogin(new Date())
|
||||
.friend(Friend.builder().id("2").name("Ronald Smith").build())
|
||||
.friend(Friend.builder().id("3").name("Matt Spencer").build())
|
||||
.build());
|
||||
} else {
|
||||
when(userService.findUser(any())).thenThrow(NotFoundException.class);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package de.kreuzwerker.cdc.userservice;
|
||||
|
||||
import au.com.dius.pact.provider.junit.Provider;
|
||||
import au.com.dius.pact.provider.junit.State;
|
||||
import au.com.dius.pact.provider.junit.loader.PactFolder;
|
||||
import au.com.dius.pact.provider.junit5.HttpTestTarget;
|
||||
import au.com.dius.pact.provider.junit5.PactVerificationContext;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@Provider("user-service")
|
||||
@PactFolder("pacts")
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@Disabled
|
||||
public class MockedUserServiceContractTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@BeforeEach
|
||||
void before(PactVerificationContext context) {
|
||||
context.setTarget(new HttpTestTarget("localhost", port));
|
||||
}
|
||||
|
||||
@MockBean
|
||||
private UserService userService;
|
||||
|
||||
@State("User 1 exists")
|
||||
public void user1Exists() {
|
||||
when(userService.findUser(any())).thenReturn(User.builder()
|
||||
.id("1")
|
||||
.legacyId(UUID.randomUUID().toString())
|
||||
.name("Beth")
|
||||
.role(UserRole.ADMIN)
|
||||
.lastLogin(new Date())
|
||||
.friend(Friend.builder().id("2").name("Ronald Smith").build())
|
||||
.friend(Friend.builder().id("3").name("Matt Spencer").build())
|
||||
.build());
|
||||
}
|
||||
|
||||
@State("User 2 does not exist")
|
||||
public void user2DoesNotExist() {
|
||||
when(userService.findUser(any())).thenThrow(NotFoundException.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package de.kreuzwerker.cdc.userservice;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
@SpringBootTest
|
||||
public class UserServiceApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user