ChildSearch { public List<Child> getRecordsForFirstPage() throws JSONException { List<Child> children = repository.getFirstPageOfChildrenMatchingString(searchKey); return filterChildrenWithRegularExpression(children, highlightedFields); } ChildSearch(String searchKey, ChildRepository repository, List<FormField> highlightedFields); List<Child> getRecordsForFirstPage(); List<Child> getRecordsForNextPage(int currentPageNumber, int nextPageNumber);  }
@Test public void shouldReturnMatchedChildRecordsWithAccentedCharacters() throws JSONException { Child child1 = new Child("id1", "user1", "{ 'name' : 'child1', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }"); Child child2 = new Child("id2", "user2", "{ 'name' : 'child2', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }"); Child child3 = new Child("id3", "user3", "{ 'name' : 'chåld3', 'test2' : 'child1', 'test3' : [ '1', 2, '3' ], \"x\": \"y\" }"); Child child4 = new Child("child1", "user4", "{ 'name' : 'chåld4', 'test2' : 'test2', 'test3' : [ '1', 2, '3' ] }"); Child child5 = new Child("child2", "user5", "{ 'name' : 'child4 developer', 'test2' : 'test2', 'test3' : [ '1', 2, '3' ] }"); repository.createOrUpdate(child1); repository.createOrUpdate(child2); repository.createOrUpdate(child3); repository.createOrUpdate(child4); repository.createOrUpdate(child5); childSearch = new ChildSearch("chåld", repository, highlightedFormFields); List<Child> children = childSearch.getRecordsForFirstPage(); assertEquals(2, children.size()); }
@Test public void shouldMatchOnlyShortId() throws JSONException, IOException { String childId = "abcdefghijklmnop"; String childShortId = "jklmnop"; Child child1 = new Child(childId, "user1", "{ 'name' : 'first second', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }"); repository.createOrUpdate(child1); childSearch = new ChildSearch(childId, repository, highlightedFormFields); List<Child> children = childSearch.getRecordsForFirstPage(); assertEquals(0, children.size()); childSearch = new ChildSearch(childShortId, repository, highlightedFormFields); children = childSearch.getRecordsForFirstPage(); assertEquals(1, children.size()); }
@Test public void shouldReturnAllMatchedChildRecordsBasedOnId() throws Exception { Child child1 = new Child("id1", "user1", "{ 'name' : 'child1', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }"); Child child2 = new Child("id2", "user2", "{ 'name' : 'child2', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }"); Child child3 = new Child("id3", "user3", "{ 'name' : 'child3', 'test2' : 'child1', 'test3' : [ '1', 2, '3' ], \"x\": \"y\" }"); Child child4 = new Child("child1", "user4", "{ 'name' : 'child4', 'test2' : 'test2', 'test3' : [ '1', 2, '3' ] }"); repository.createOrUpdate(child1); repository.createOrUpdate(child2); repository.createOrUpdate(child3); repository.createOrUpdate(child4); childSearch = new ChildSearch("hiLd1", repository, highlightedFormFields); List<Child> children = childSearch.getRecordsForFirstPage(); assertEquals(2, children.size()); }
@Test public void shouldReturnMatchedChildRecordsBasedOnHighlightedFields() throws JSONException, IOException { Child child1 = new Child("id1", "user1", "{ 'name' : 'child1', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }"); Child child2 = new Child("id2", "user2", "{ 'name' : 'child2', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }"); Child child3 = new Child("id3", "user3", "{ 'name' : 'child3', 'test2' : 'child1', 'test3' : [ '1', 2, '3' ], \"x\": \"y\" }"); Child child4 = new Child("child1", "user4", "{ 'name' : 'child4', 'test2' : 'test2', 'test3' : [ '1', 2, '3' ] }"); Child child5 = new Child("child2", "user5", "{ 'name' : 'child4 developer', 'test2' : 'test2', 'test3' : [ '1', 2, '3' ] }"); repository.createOrUpdate(child1); repository.createOrUpdate(child2); repository.createOrUpdate(child3); repository.createOrUpdate(child4); repository.createOrUpdate(child5); childSearch = new ChildSearch("child3", repository, highlightedFormFields); assertEquals(1, childSearch.getRecordsForFirstPage().size()); childSearch = new ChildSearch("hiLd", repository, highlightedFormFields); assertEquals(5, childSearch.getRecordsForFirstPage().size()); childSearch = new ChildSearch("hiLd1", repository, highlightedFormFields); assertEquals(2, childSearch.getRecordsForFirstPage().size()); childSearch = new ChildSearch("developer", repository, highlightedFormFields); assertEquals(1, childSearch.getRecordsForFirstPage().size()); }
@Test public void shouldMatchOfSearchTermInCorrectOrder() throws JSONException, IOException { Child child1 = new Child("id1", "user1", "{ 'name' : 'first second', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }"); Child child2 = new Child("id2", "user1", "{ 'name' : 'john smith', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }"); repository.createOrUpdate(child1); repository.createOrUpdate(child2); childSearch = new ChildSearch("first second", repository, highlightedFormFields); List<Child> children = childSearch.getRecordsForFirstPage(); assertEquals(1, children.size()); }
@Test public void shouldReturnAllRecordsMatchingOfSearchTerm() throws JSONException, IOException { Child child1 = new Child("id1", "user1", "{ 'name' : 'first second', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }"); Child child2 = new Child("id2", "user1", "{ 'name' : 'john smith', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }"); repository.createOrUpdate(child1); repository.createOrUpdate(child2); childSearch = new ChildSearch("first john", repository, highlightedFormFields); List<Child> children = childSearch.getRecordsForFirstPage(); assertEquals(2, children.size()); }
@Test public void shouldMatchSearchTermInReverseOrder() throws JSONException, IOException { Child child1 = new Child("id1", "user1", "{ 'name' : 'first second', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }"); Child child2 = new Child("id2", "user1", "{ 'name' : 'john smith', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }"); repository.createOrUpdate(child1); repository.createOrUpdate(child2); childSearch = new ChildSearch("second first", repository, highlightedFormFields); List<Child> children = childSearch.getRecordsForFirstPage(); assertEquals(1, children.size()); }
@Test public void shouldNotMatchDifferentWithSearchTerm() throws JSONException, IOException { Child child1 = new Child("id1", "user1", "{ 'name' : 'first second', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }"); Child child2 = new Child("id2", "user1", "{ 'name' : 'john smith', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }"); repository.createOrUpdate(child1); repository.createOrUpdate(child2); childSearch = new ChildSearch("sam", repository, highlightedFormFields); List<Child> children = childSearch.getRecordsForFirstPage(); assertEquals(0, children.size()); }