forked from CodelyTV/java-ddd-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add backoffice courses repository cache
- Loading branch information
1 parent
ea63495
commit 9c6a54f
Showing
8 changed files
with
180 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...ackoffice/courses/infrastructure/persistence/InMemoryCacheBackofficeCourseRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package tv.codely.backoffice.courses.infrastructure.persistence; | ||
|
||
import tv.codely.backoffice.courses.domain.BackofficeCourse; | ||
import tv.codely.backoffice.courses.domain.BackofficeCourseRepository; | ||
import tv.codely.shared.domain.criteria.Criteria; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
public final class InMemoryCacheBackofficeCourseRepository implements BackofficeCourseRepository { | ||
private final BackofficeCourseRepository repository; | ||
private List<BackofficeCourse> courses = new ArrayList<>(); | ||
private HashMap<String, List<BackofficeCourse>> matchingCourses = new HashMap<>(); | ||
|
||
public InMemoryCacheBackofficeCourseRepository(BackofficeCourseRepository repository) { | ||
this.repository = repository; | ||
} | ||
|
||
@Override | ||
public void save(BackofficeCourse course) { | ||
repository.save(course); | ||
} | ||
|
||
@Override | ||
public List<BackofficeCourse> searchAll() { | ||
return courses.isEmpty() ? searchAndFillCache() : courses; | ||
} | ||
|
||
@Override | ||
public List<BackofficeCourse> matching(Criteria criteria) { | ||
return matchingCourses.containsKey(criteria.serialize()) | ||
? matchingCourses.get(criteria.serialize()) | ||
: searchMatchingAndFillCache(criteria); | ||
} | ||
|
||
private List<BackofficeCourse> searchMatchingAndFillCache(Criteria criteria) { | ||
List<BackofficeCourse> courses = repository.matching(criteria); | ||
|
||
this.matchingCourses.put(criteria.serialize(), courses); | ||
|
||
return courses; | ||
} | ||
|
||
private List<BackofficeCourse> searchAndFillCache() { | ||
List<BackofficeCourse> courses = repository.searchAll(); | ||
|
||
this.courses = courses; | ||
|
||
return courses; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...ice/courses/infrastructure/persistence/InMemoryCacheBackofficeCourseRepositoryShould.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package tv.codely.backoffice.courses.infrastructure.persistence; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import tv.codely.backoffice.BackofficeContextInfrastructureTestCase; | ||
import tv.codely.backoffice.courses.domain.BackofficeCourse; | ||
import tv.codely.backoffice.courses.domain.BackofficeCourseCriteriaMother; | ||
import tv.codely.backoffice.courses.domain.BackofficeCourseMother; | ||
import tv.codely.backoffice.courses.domain.BackofficeCourseRepository; | ||
import tv.codely.shared.domain.criteria.Criteria; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsInAnyOrder; | ||
import static org.mockito.Mockito.*; | ||
|
||
final class InMemoryCacheBackofficeCourseRepositoryShould extends BackofficeContextInfrastructureTestCase { | ||
private InMemoryCacheBackofficeCourseRepository repository; | ||
private BackofficeCourseRepository innerRepository; | ||
|
||
@BeforeEach | ||
protected void setUp() { | ||
innerRepository = mock(BackofficeCourseRepository.class); | ||
repository = new InMemoryCacheBackofficeCourseRepository(innerRepository); | ||
} | ||
|
||
@Test | ||
void save_a_course() { | ||
BackofficeCourse course = BackofficeCourseMother.random(); | ||
|
||
repository.save(course); | ||
|
||
shouldHaveSaved(course); | ||
} | ||
|
||
@Test | ||
void search_all_existing_courses() { | ||
BackofficeCourse course = BackofficeCourseMother.random(); | ||
BackofficeCourse anotherCourse = BackofficeCourseMother.random(); | ||
List<BackofficeCourse> courses = Arrays.asList(course, anotherCourse); | ||
|
||
shouldSearchAll(courses); | ||
|
||
assertThat(courses, containsInAnyOrder(repository.searchAll().toArray())); | ||
} | ||
|
||
@Test | ||
void search_all_existing_courses_without_hitting_the_inner_repository_the_second_time() { | ||
BackofficeCourse course = BackofficeCourseMother.random(); | ||
BackofficeCourse anotherCourse = BackofficeCourseMother.random(); | ||
List<BackofficeCourse> courses = Arrays.asList(course, anotherCourse); | ||
|
||
shouldSearchAll(courses); | ||
|
||
assertThat(courses, containsInAnyOrder(repository.searchAll().toArray())); | ||
assertThat(courses, containsInAnyOrder(repository.searchAll().toArray())); | ||
} | ||
|
||
@Test | ||
void search_courses_using_a_criteria() { | ||
BackofficeCourse matchingCourse = BackofficeCourseMother.create("DDD en Java", "3 days"); | ||
BackofficeCourse anotherMatchingCourse = BackofficeCourseMother.create("DDD en TypeScript", "2.5 days"); | ||
List<BackofficeCourse> matchingCourses = Arrays.asList(matchingCourse, anotherMatchingCourse); | ||
|
||
Criteria criteria = BackofficeCourseCriteriaMother.nameAndDurationContains("DDD", "days"); | ||
|
||
shouldSearchMatching(criteria, matchingCourses); | ||
|
||
assertThat(matchingCourses, containsInAnyOrder(repository.matching(criteria).toArray())); | ||
} | ||
|
||
@Test | ||
void search_courses_using_a_criteria_without_hitting_the_inner_repository_the_second_time() { | ||
BackofficeCourse matchingCourse = BackofficeCourseMother.create("DDD en Java", "3 days"); | ||
BackofficeCourse anotherMatchingCourse = BackofficeCourseMother.create("DDD en TypeScript", "2.5 days"); | ||
List<BackofficeCourse> matchingCourses = Arrays.asList(matchingCourse, anotherMatchingCourse); | ||
|
||
Criteria criteria = BackofficeCourseCriteriaMother.nameAndDurationContains("DDD", "days"); | ||
|
||
shouldSearchMatching(criteria, matchingCourses); | ||
|
||
assertThat(matchingCourses, containsInAnyOrder(repository.matching(criteria).toArray())); | ||
assertThat(matchingCourses, containsInAnyOrder(repository.matching(criteria).toArray())); | ||
} | ||
|
||
private void shouldSearchAll(List<BackofficeCourse> courses) { | ||
Mockito.when(innerRepository.searchAll()).thenReturn(courses); | ||
} | ||
|
||
private void shouldSearchMatching(Criteria criteria, List<BackofficeCourse> courses) { | ||
Mockito.when(innerRepository.matching(criteria)).thenReturn(courses); | ||
} | ||
|
||
private void shouldHaveSaved(BackofficeCourse course) { | ||
verify(innerRepository, atLeastOnce()).save(course); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters