Slide 1

Slide 1 text

Decoupling in Java

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

$ curl http:!//localhost:8080/commits/abcdef HTTP/1.1 200 OK Content-Type: application/json Content-Length: 66 Commit[ message=Commit from git, filenames=[ filea.txt, fileb.txt, ] ]

Slide 4

Slide 4 text

$ curl http:!//localhost:8080/commits/abcdef HTTP/1.1 200 OK Content-Type: application/json Content-Length: 66 Commit[ message=Commit from local, filenames=[ filea.txt, fileb.txt, ] ]

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

@Path("commits") public class CommitsEndpoint { private final CommitsService service; public CommitsEndpoint( final CommitsService service) { this.service = service; } @GET @Path("{sha}") @Produces(MediaType.APPLICATION_JSON) public String getCommit( @PathParam("sha") final String sha) { return service.commitBySha(sha).map(Record!::toString).orElse("NO COMMIT"); } }

Slide 7

Slide 7 text

public record CommitsService( Repository localRepository, Repository githubRepository) { Optional commitBySha( final String sha) { return localRepository().commitBySha(sha) .or(() !-> githubRepository().commitBySha(sha)); } }

Slide 8

Slide 8 text

public interface Repository { Optional commitBySha(String sha); List commitsByRepo(String repository); }

Slide 9

Slide 9 text

public class GithubRepository implements Repository { @Override public Optional commitBySha( final String sha) { return Optional.of( new Commit( "Commit from git", List.of("filea.txt,", "fileb.txt") ) ); } @Override public List commitsByRepo( final String repository) { return List.of( new Commit( "Commit 1 from git", List.of("filea.txt,", "fileb.txt") ), new Commit( "Commit 2 from git", List.of("filec.txt,", "filed.txt") ) ); } }

Slide 10

Slide 10 text

public class LocalRepository implements Repository { @Override public Optional commitBySha( final String sha) { if (new Random().nextBoolean()) { return Optional.empty(); } return Optional.of( new Commit( "Commit from local", List.of("filea.txt,", "fileb.txt") ) ); } @Override public List commitsByRepo( final String repository) { return List.of( new Commit( "Commit 1 from local", List.of("filea.txt,", "fileb.txt") ), new Commit( "Commit 2 from local", List.of("filec.txt,", "filed.txt") ) ); } }

Slide 11

Slide 11 text

@Test @DisplayName("Should return from github when local is empty") void shouldReturnFromGithubWhenLocalIsEmpty( @Mock final Repository localRepository, @Mock final Repository gitHubRepository) throws Exception { !// Given when(localRepository.commitBySha(anyString())) .thenReturn(Optional.empty()); when(gitHubRepository.commitBySha(anyString())) .thenReturn(Optional.of(new Commit("git", List.of("git.txt")))); !// When final var commit = new CommitsService(localRepository, gitHubRepository).commitBySha("sha"); !// Then assertThat(commit).contains(new Commit("git", List.of("git.txt"))); verify(localRepository, times(1)).commitBySha(anyString()); verify(gitHubRepository, times(1)).commitBySha(anyString()); }

Slide 12

Slide 12 text

Service LocalRepository GithubRepository

Slide 13

Slide 13 text

Service LocalRepository GithubRepository Repository Repository

Slide 14

Slide 14 text

public class GithubRepository implements Repository { @Override public Optional commitBySha( final String sha) { … } … } public interface Repository { Optional commitBySha(String sha); List commitsByRepo(String repository); } @Test @DisplayName("Should return from github when local is empty") void shouldReturnFromGithubWhenLocalIsEmpty( @Mock final Repository localRepository, @Mock final Repository gitHubRepository) throws Exception { !// Given when(localRepository.commitBySha(anyString())) .thenReturn(Optional.empty()); when(gitHubRepository.commitBySha(anyString())) .thenReturn(Optional.of(new Commit("git", List.of("git.txt")))); !// When final var commit = new CommitsService(localRepository, gitHubRepository).commitBy !// Then assertThat(commit).contains(new Commit("git", List.of("git.txt"))); verify(localRepository, times(1)).commitBySha(anyString()); verify(gitHubRepository, times(1)).commitBySha(anyString()); } public class LocalRepository implements Repository { @Override public Optional commitBySha( final String sha) { … } … } public record CommitsService( Repository localRepository, Repository githubRepository) { Optional commitBySha( final String sha) { return localRepository().commitBySha(sha) .or(() !-> githubRepository().commitBySha(sha)); } }

Slide 15

Slide 15 text

public interface Repository { Optional commitBySha(String sha); List commitsByRepo(String repository); } public class GithubRepository implements Repository { @Override public Optional commitBySha( final String sha) { … } … } public class LocalRepository implements Repository { @Override public Optional commitBySha( final String sha) { … } … } @Test @DisplayName("Should return from github when local is empty") void shouldReturnFromGithubWhenLocalIsEmpty( @Mock final Repository localRepository, @Mock final Repository gitHubRepository) throws Exception { !// Given when(localRepository.commitBySha(anyString())) .thenReturn(Optional.empty()); when(gitHubRepository.commitBySha(anyString())) .thenReturn(Optional.of(new Commit("git", List.of("git.txt")))); !// When final var commit = new CommitsService(localRepository, gitHubRepository).commitBy !// Then assertThat(commit).contains(new Commit("git", List.of("git.txt"))); verify(localRepository, times(1)).commitBySha(anyString()); verify(gitHubRepository, times(1)).commitBySha(anyString()); } public record CommitsService( Repository localRepository, Repository githubRepository) { Optional commitBySha( final String sha) { return localRepository().commitBySha(sha) .or(() !-> githubRepository().commitBySha(sha)); } }

Slide 16

Slide 16 text

public interface Repository { Optional commitBySha(String sha); List commitsByRepo(String repository); } public class GithubRepository implements Repository { @Override public Optional commitBySha( final String sha) { … } … } public class LocalRepository implements Repository { @Override public Optional commitBySha( final String sha) { … } … } @Test @DisplayName("Should return from github when local is empty") void shouldReturnFromGithubWhenLocalIsEmpty( @Mock final Repository localRepository, @Mock final Repository gitHubRepository) throws Exception { !// Given when(localRepository.commitBySha(anyString())) .thenReturn(Optional.empty()); when(gitHubRepository.commitBySha(anyString())) .thenReturn(Optional.of(new Commit("git", List.of("git.txt")))); !// When final var commit = new CommitsService(localRepository, gitHubRepository).commitBy !// Then assertThat(commit).contains(new Commit("git", List.of("git.txt"))); verify(localRepository, times(1)).commitBySha(anyString()); verify(gitHubRepository, times(1)).commitBySha(anyString()); } public record CommitsService( Repository localRepository, Repository githubRepository) { Optional commitBySha( final String sha) { return localRepository().commitBySha(sha) .or(() !-> githubRepository().commitBySha(sha)); } }