-
Notifications
You must be signed in to change notification settings - Fork 14
Adding testcases #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: solution/complete
Are you sure you want to change the base?
Adding testcases #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,8 @@ | |
| @Transactional | ||
| public class UcDeleteTaskList { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(UcDeleteTaskList.class); | ||
| private static final Logger STATIC_LOG = LoggerFactory.getLogger(UcDeleteTaskList.class); | ||
| Logger log = STATIC_LOG; | ||
|
|
||
| @Inject | ||
| TaskListRepository taskListRepository; | ||
|
|
@@ -41,7 +42,7 @@ public void delete(TaskListEto list) { | |
|
|
||
| Long id = list.getId(); | ||
| if (id == null) { | ||
| LOG.info("TaskItem {} ist transient und kann nicht gelöscht werden", list.getTitle()); | ||
| log.info("TaskItem {} ist transient und kann nicht gelöscht werden", list.getTitle()); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quarkus Log |
||
| } | ||
| this.taskListRepository.deleteById(id); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package org.example.app.general.common.search; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class LikePatternSyntaxTest { | ||
| @Test | ||
| void testConvertGlobToSql() { | ||
| String pattern = "*abc?def*"; | ||
| String expected = "%abc_def%"; | ||
| String result = LikePatternSyntax.SQL.convert(pattern, LikePatternSyntax.GLOB, false); | ||
| assertEquals(expected, result); | ||
| } | ||
|
|
||
| @Test | ||
| void testConvertSqlToGlob() { | ||
| String pattern = "%abc_def%"; | ||
| String expected = "*abc?def*"; | ||
| String result = LikePatternSyntax.GLOB.convert(pattern, LikePatternSyntax.SQL, false); | ||
| assertEquals(expected, result); | ||
| } | ||
|
|
||
| @Test | ||
| void testConvertWithMatchSubstringTrue() { | ||
| String pattern = "abc"; | ||
| String expected = "%abc%"; | ||
| String result = LikePatternSyntax.SQL.convert(pattern, LikePatternSyntax.SQL, true); | ||
| assertEquals(expected, result); | ||
| } | ||
|
|
||
| @Test | ||
| void testConvertNullPattern() { | ||
| String result = LikePatternSyntax.SQL.convert(null, LikePatternSyntax.SQL, false); | ||
| assertNull(result); | ||
| } | ||
|
|
||
| @Test | ||
| void testConvertEmptyPatternWithMatchSubstringTrue() { | ||
| String result = LikePatternSyntax.SQL.convert("", LikePatternSyntax.SQL, true); | ||
| assertEquals("%", result); | ||
| } | ||
|
|
||
| @Test | ||
| void testAutoDetectGlob() { | ||
| assertEquals(LikePatternSyntax.GLOB, LikePatternSyntax.autoDetect("file-*.txt")); | ||
| } | ||
|
|
||
| @Test | ||
| void testAutoDetectSql() { | ||
| assertEquals(LikePatternSyntax.SQL, LikePatternSyntax.autoDetect("user_%_2024")); | ||
| } | ||
|
|
||
| @Test | ||
| void testAutoDetectNoWildcards() { | ||
| assertNull(LikePatternSyntax.autoDetect("plainText")); | ||
| } | ||
|
|
||
| @Test | ||
| void testEscapedCharactersInPattern() { | ||
| String pattern = "*abc\\*def*"; | ||
| String result = LikePatternSyntax.SQL.convert(pattern, LikePatternSyntax.GLOB, false); | ||
| assertTrue(result.contains("\\%")); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package org.example.app.general.common.search; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
|
|
||
| public class SortOrderByTest { | ||
| @Test | ||
| void testGetDirectionDefaultsToAsc() { | ||
| SortOrderBy sortOrderBy = new SortOrderBy(); | ||
| assertEquals(SortOrderDirection.ASC, sortOrderBy.getDirection()); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to use AssertJ |
||
| } | ||
|
|
||
| @Test | ||
| void testEqualsAndHashCode() { | ||
| SortOrderBy a = new SortOrderBy("status", SortOrderDirection.DESC); | ||
| SortOrderBy b = new SortOrderBy("status", SortOrderDirection.DESC); | ||
| SortOrderBy c = new SortOrderBy("priority", SortOrderDirection.ASC); | ||
|
|
||
| assertEquals(a, b); | ||
| assertEquals(a.hashCode(), b.hashCode()); | ||
|
|
||
| assertNotEquals(a, c); | ||
| } | ||
|
|
||
| @Test | ||
| void testToStringFormat() { | ||
| SortOrderBy orderBy = new SortOrderBy("priority", SortOrderDirection.ASC); | ||
| assertEquals("priority ASC", orderBy.toString()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| package org.example.app.general.dataaccess; | ||
|
|
||
| import com.querydsl.core.FilteredClause; | ||
| import com.querydsl.core.types.dsl.BooleanExpression; | ||
| import com.querydsl.core.types.dsl.Expressions; | ||
| import com.querydsl.core.types.dsl.SimpleExpression; | ||
| import com.querydsl.core.types.dsl.StringExpression; | ||
| import com.querydsl.jpa.impl.JPAQuery; | ||
| import org.example.app.general.common.search.*; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.ArgumentCaptor; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| public class ApplicationQueryFragmentTest { | ||
| private TestableQueryFragment fragment; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| fragment = new TestableQueryFragment(); | ||
| } | ||
|
|
||
| @Test | ||
| void testNewStringClause_nullValue_returnsNullOrNotNullClause() { | ||
| StringExpression expression = Expressions.stringPath("testField"); | ||
|
|
||
| assertThat(fragment.newStringClause(expression, null, StringSearchOperator.EQ, null, false, false)) | ||
| .isEqualTo(expression.isNull()); | ||
|
|
||
| assertThat(fragment.newStringClause(expression, null, StringSearchOperator.NE, null, false, false)) | ||
| .isEqualTo(expression.isNotNull()); | ||
|
|
||
| assertThrows(IllegalArgumentException.class, () -> | ||
| fragment.newStringClause(expression, null, StringSearchOperator.LT, null, false, false)); | ||
| } | ||
|
|
||
| @Test | ||
| void testNewStringClause_emptyValue_returnsIsEmptyOrNotEmpty() { | ||
| StringExpression expression = Expressions.stringPath("testField"); | ||
|
|
||
| assertThat(fragment.newStringClause(expression, "", StringSearchOperator.EQ, null, false, false)) | ||
| .isEqualTo(expression.isEmpty()); | ||
|
|
||
| assertThat(fragment.newStringClause(expression, "", StringSearchOperator.NE, null, false, false)) | ||
| .isEqualTo(expression.isNotEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| void testNewStringClause_ignoreCase_appliesUpperCase() { | ||
| StringExpression mockExpression = mock(StringExpression.class); | ||
| StringExpression upperMock = mock(StringExpression.class); | ||
| when(mockExpression.upper()).thenReturn(upperMock); | ||
| when(upperMock.like(anyString())).thenReturn(mock(BooleanExpression.class)); | ||
|
|
||
| fragment.newStringClause(mockExpression, "foo", StringSearchOperator.LIKE, LikePatternSyntax.SQL, true, false); | ||
|
|
||
| verify(mockExpression).upper(); | ||
| verify(upperMock).like(anyString()); | ||
| } | ||
|
|
||
| @Test | ||
| void testNewLikeClause_autoDetectSyntax() { | ||
| StringExpression expr = Expressions.stringPath("field"); | ||
| BooleanExpression result = fragment.newLikeClause(expr, "abc*", null, false, false); | ||
|
|
||
| assertThat(result).isNotNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void testWhereIn_withShortList() { | ||
| SimpleExpression<String> expr = Expressions.stringPath("field"); | ||
| FilteredClause<?> clause = mock(FilteredClause.class); | ||
|
|
||
| // Call the method with a short list | ||
| fragment.whereIn(clause, expr, List.of("a", "b", "c")); | ||
|
|
||
| // Capture the BooleanExpression argument passed to where() | ||
| ArgumentCaptor<BooleanExpression> captor = ArgumentCaptor.forClass(BooleanExpression.class); | ||
| verify(clause).where(captor.capture()); | ||
|
|
||
| // Assert that the captured BooleanExpression is not null | ||
| BooleanExpression capturedExpression = captor.getValue(); | ||
| assertThat(capturedExpression).isNotNull(); | ||
|
|
||
| // Optionally, assert that the captured expression contains the expected "in" clause | ||
| assertThat(capturedExpression.toString()).contains("in"); | ||
| assertThat(capturedExpression.toString()).contains("a"); | ||
| assertThat(capturedExpression.toString()).contains("b"); | ||
| assertThat(capturedExpression.toString()).contains("c"); | ||
|
|
||
| // If duplicates might exist, verify that the expression has no duplicate values | ||
| List<String> values = List.of("a", "b", "c"); | ||
| assertThat(capturedExpression.toString()).contains(String.join(", ", values)); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| @Test | ||
| void testWhereIn_withEmptyList_logsAndAddsFalseClause() { | ||
| SimpleExpression<String> expr = Expressions.stringPath("field"); | ||
| FilteredClause<?> clause = mock(FilteredClause.class); | ||
|
|
||
| fragment.whereIn(clause, expr, Collections.emptyList()); | ||
|
|
||
| verify(clause).where(eq(Expressions.ONE.eq(Expressions.ZERO))); | ||
| } | ||
|
|
||
| @Test | ||
| void testWhereIn_withLargeList_partitionsCorrectly() { | ||
| SimpleExpression<String> expr = Expressions.stringPath("field"); | ||
| FilteredClause<?> clause = mock(FilteredClause.class); | ||
|
|
||
| List<String> largeList = Collections.nCopies(1050, "val"); | ||
|
|
||
| fragment.whereIn(clause, expr, largeList); | ||
|
|
||
| ArgumentCaptor<BooleanExpression> captor = ArgumentCaptor.forClass(BooleanExpression.class); | ||
| verify(clause).where(captor.capture()); | ||
|
|
||
| assertThat(captor.getValue()).isNotNull(); // not testing deep equality of OR-ed clauses | ||
| } | ||
|
|
||
| @Test | ||
| void testFindPaginated_withoutTotal() { | ||
| JPAQuery<String> query = mock(JPAQuery.class); | ||
| when(query.fetch()).thenReturn(List.of("item1", "item2")); | ||
|
|
||
| SearchCriteria criteria = new TestSearchCriteria(); | ||
| criteria.setDetermineTotal(false); | ||
|
|
||
| var result = fragment.findPaginated(criteria, query); | ||
|
|
||
| assertThat(result.getContent()).hasSize(2); | ||
| assertThat(result.getTotalElements()).isEqualTo(2); | ||
| } | ||
|
|
||
| // Internal testable subclass for access | ||
| static class TestableQueryFragment extends ApplicationQueryFragment { | ||
| // no-op: expose protected methods for testing | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package org.example.app.general.dataaccess; | ||
|
|
||
| import org.example.app.general.common.search.SearchCriteria; | ||
|
|
||
| class TestSearchCriteria extends SearchCriteria { | ||
| private boolean determineTotal; | ||
|
|
||
| @Override | ||
| public boolean isDetermineTotal() { | ||
| return determineTotal; | ||
| } | ||
|
|
||
| public void setDetermineTotal(boolean determineTotal) { | ||
| this.determineTotal = determineTotal; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package org.example.app.task.logic; | ||
|
|
||
| import org.example.app.task.common.TaskItemEto; | ||
| import org.example.app.task.dataaccess.TaskItemEntity; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mockito; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static java.util.Collections.emptyList; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.mockito.Mockito.doReturn; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| public class TaskItemMapperTest { | ||
| @Test | ||
| void toEtos_shouldReturnMappedList() { | ||
| // Given | ||
| TaskItemEntity entity1 = new TaskItemEntity(); | ||
| TaskItemEntity entity2 = new TaskItemEntity(); | ||
| TaskItemEto eto1 = new TaskItemEto(); | ||
| TaskItemEto eto2 = new TaskItemEto(); | ||
|
|
||
| TaskItemMapper mapper = Mockito.spy(TaskItemMapper.class); | ||
|
|
||
| doReturn(eto1).when(mapper).toEto(entity1); | ||
| doReturn(eto2).when(mapper).toEto(entity2); | ||
|
|
||
| // When | ||
| List<TaskItemEto> result = mapper.toEtos(List.of(entity1, entity2)); | ||
|
|
||
| // Then | ||
| assertEquals(List.of(eto1, eto2), result); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are using AssertJ in the training |
||
| verify(mapper).toEto(entity1); | ||
| verify(mapper).toEto(entity2); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use Quarkus Log instead, no need to define a static logger then