Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/test/java/io/spring/UtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.spring;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

public class UtilTest {

@Test
public void should_return_true_for_null() {
assertThat(Util.isEmpty(null), is(true));
}

@Test
public void should_return_true_for_empty_string() {
assertThat(Util.isEmpty(""), is(true));
}

@Test
public void should_return_false_for_non_empty_string() {
assertThat(Util.isEmpty("hello"), is(false));
}

@Test
public void should_return_false_for_whitespace_string() {
assertThat(Util.isEmpty(" "), is(false));
}
}
77 changes: 77 additions & 0 deletions src/test/java/io/spring/application/CursorPageParameterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.spring.application;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

public class CursorPageParameterTest {

@Test
public void should_create_with_valid_parameters() {
CursorPageParameter<String> param =
new CursorPageParameter<>("cursor", 10, CursorPager.Direction.NEXT);

assertThat(param.getCursor(), is("cursor"));
assertThat(param.getLimit(), is(10));
assertThat(param.getDirection(), is(CursorPager.Direction.NEXT));
}

@Test
public void should_return_true_for_next_direction() {
CursorPageParameter<String> param =
new CursorPageParameter<>("cursor", 10, CursorPager.Direction.NEXT);

assertThat(param.isNext(), is(true));
}

@Test
public void should_return_false_for_prev_direction() {
CursorPageParameter<String> param =
new CursorPageParameter<>("cursor", 10, CursorPager.Direction.PREV);

assertThat(param.isNext(), is(false));
}

@Test
public void should_return_query_limit_as_limit_plus_one() {
CursorPageParameter<String> param =
new CursorPageParameter<>("cursor", 10, CursorPager.Direction.NEXT);

assertThat(param.getQueryLimit(), is(11));
}

@Test
public void should_cap_limit_at_max() {
CursorPageParameter<String> param =
new CursorPageParameter<>("cursor", 2000, CursorPager.Direction.NEXT);

assertThat(param.getLimit(), is(1000));
assertThat(param.getQueryLimit(), is(1001));
}

@Test
public void should_ignore_negative_limit() {
CursorPageParameter<String> param =
new CursorPageParameter<>("cursor", -5, CursorPager.Direction.NEXT);

assertThat(param.getLimit(), is(20));
}

@Test
public void should_ignore_zero_limit() {
CursorPageParameter<String> param =
new CursorPageParameter<>("cursor", 0, CursorPager.Direction.NEXT);

assertThat(param.getLimit(), is(20));
}

@Test
public void should_accept_null_cursor() {
CursorPageParameter<String> param =
new CursorPageParameter<>(null, 10, CursorPager.Direction.NEXT);

assertThat(param.getCursor(), nullValue());
}
}
110 changes: 110 additions & 0 deletions src/test/java/io/spring/application/CursorPagerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package io.spring.application;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import io.spring.application.data.ArticleData;
import io.spring.application.data.ProfileData;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;

public class CursorPagerTest {

@Test
public void should_indicate_has_next_when_direction_is_next_and_has_extra() {
List<ArticleData> data = createArticleDataList(2);
CursorPager<ArticleData> pager = new CursorPager<>(data, CursorPager.Direction.NEXT, true);

assertThat(pager.hasNext(), is(true));
assertThat(pager.hasPrevious(), is(false));
}

@Test
public void should_not_indicate_has_next_when_direction_is_next_and_no_extra() {
List<ArticleData> data = createArticleDataList(2);
CursorPager<ArticleData> pager = new CursorPager<>(data, CursorPager.Direction.NEXT, false);

assertThat(pager.hasNext(), is(false));
assertThat(pager.hasPrevious(), is(false));
}

@Test
public void should_indicate_has_previous_when_direction_is_prev_and_has_extra() {
List<ArticleData> data = createArticleDataList(2);
CursorPager<ArticleData> pager = new CursorPager<>(data, CursorPager.Direction.PREV, true);

assertThat(pager.hasNext(), is(false));
assertThat(pager.hasPrevious(), is(true));
}

@Test
public void should_not_indicate_has_previous_when_direction_is_prev_and_no_extra() {
List<ArticleData> data = createArticleDataList(2);
CursorPager<ArticleData> pager = new CursorPager<>(data, CursorPager.Direction.PREV, false);

assertThat(pager.hasNext(), is(false));
assertThat(pager.hasPrevious(), is(false));
}

@Test
public void should_return_start_cursor_from_first_element() {
List<ArticleData> data = createArticleDataList(3);
CursorPager<ArticleData> pager = new CursorPager<>(data, CursorPager.Direction.NEXT, false);

assertThat(pager.getStartCursor(), notNullValue());
assertThat(pager.getStartCursor().toString(), is(data.get(0).getCursor().toString()));
}

@Test
public void should_return_end_cursor_from_last_element() {
List<ArticleData> data = createArticleDataList(3);
CursorPager<ArticleData> pager = new CursorPager<>(data, CursorPager.Direction.NEXT, false);

assertThat(pager.getEndCursor(), notNullValue());
assertThat(pager.getEndCursor().toString(), is(data.get(2).getCursor().toString()));
}

@Test
public void should_return_null_cursors_for_empty_data() {
List<ArticleData> data = new ArrayList<>();
CursorPager<ArticleData> pager = new CursorPager<>(data, CursorPager.Direction.NEXT, false);

assertThat(pager.getStartCursor(), nullValue());
assertThat(pager.getEndCursor(), nullValue());
}

@Test
public void should_return_data_list() {
List<ArticleData> data = createArticleDataList(2);
CursorPager<ArticleData> pager = new CursorPager<>(data, CursorPager.Direction.NEXT, false);

assertThat(pager.getData().size(), is(2));
}

private List<ArticleData> createArticleDataList(int count) {
List<ArticleData> list = new ArrayList<>();
for (int i = 0; i < count; i++) {
DateTime time = new DateTime().plusMinutes(i);
ArticleData articleData =
new ArticleData(
"id-" + i,
"slug-" + i,
"title-" + i,
"desc-" + i,
"body-" + i,
false,
0,
time,
time,
Arrays.asList("tag"),
new ProfileData("user-" + i, "username-" + i, "bio", "image", false));
list.add(articleData);
}
return list;
}
}
55 changes: 55 additions & 0 deletions src/test/java/io/spring/application/DateTimeCursorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.spring.application;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.jupiter.api.Test;

public class DateTimeCursorTest {

@Test
public void should_return_millis_as_string() {
DateTime dateTime = new DateTime(1000000L, DateTimeZone.UTC);
DateTimeCursor cursor = new DateTimeCursor(dateTime);

assertThat(cursor.toString(), is("1000000"));
}

@Test
public void should_return_data() {
DateTime dateTime = new DateTime(1000000L, DateTimeZone.UTC);
DateTimeCursor cursor = new DateTimeCursor(dateTime);

assertThat(cursor.getData(), is(dateTime));
}

@Test
public void should_parse_valid_cursor_string() {
DateTime result = DateTimeCursor.parse("1000000");

assertThat(result, notNullValue());
assertThat(result.getMillis(), is(1000000L));
assertThat(result.getZone(), is(DateTimeZone.UTC));
}

@Test
public void should_return_null_for_null_cursor() {
DateTime result = DateTimeCursor.parse(null);

assertThat(result, nullValue());
}

@Test
public void should_roundtrip_through_toString_and_parse() {
DateTime original = new DateTime(DateTimeZone.UTC);
DateTimeCursor cursor = new DateTimeCursor(original);
String serialized = cursor.toString();
DateTime parsed = DateTimeCursor.parse(serialized);

assertThat(parsed.getMillis(), is(original.getMillis()));
}
}
60 changes: 60 additions & 0 deletions src/test/java/io/spring/application/PageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.spring.application;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

public class PageTest {

@Test
public void should_use_default_values() {
Page page = new Page(0, 0);

assertThat(page.getOffset(), is(0));
assertThat(page.getLimit(), is(20));
}

@Test
public void should_set_valid_offset_and_limit() {
Page page = new Page(10, 50);

assertThat(page.getOffset(), is(10));
assertThat(page.getLimit(), is(50));
}

@Test
public void should_cap_limit_at_max() {
Page page = new Page(0, 200);

assertThat(page.getLimit(), is(100));
}

@Test
public void should_ignore_negative_offset() {
Page page = new Page(-5, 20);

assertThat(page.getOffset(), is(0));
}

@Test
public void should_ignore_negative_limit() {
Page page = new Page(0, -10);

assertThat(page.getLimit(), is(20));
}

@Test
public void should_accept_limit_at_boundary() {
Page page = new Page(0, 100);

assertThat(page.getLimit(), is(100));
}

@Test
public void should_accept_limit_of_one() {
Page page = new Page(0, 1);

assertThat(page.getLimit(), is(1));
}
}
Loading