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
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
SortOrderBy other = (SortOrderBy) obj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ protected void where(FilteredClause<?> statement, BooleanExpression expression,
* @param expression the {@link SimpleExpression} to build the IN-expression from.
* @param values the {@link List} of values for the IN-expression.
*/
protected <V> void whereIn(FilteredClause<?> statement, SimpleExpression<V> expression, List<V> values) {
public <V> void whereIn(FilteredClause<?> statement, SimpleExpression<V> expression, List<V> values) {

BooleanExpression inExpression = null;
int size = 0;
Expand All @@ -305,8 +305,10 @@ protected <V> void whereIn(FilteredClause<?> statement, SimpleExpression<V> expr
int end = start + MAX_IN_EXPRESSIONS;
partition = values.subList(start, end);
start = end;
rest -= MAX_IN_EXPRESSIONS;
} else {
partition = padList(values.subList(start, size));
rest = 0;
}
BooleanExpression newInExpr = expression.in(partition);
if (inExpression == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
@Transactional
public class UcDeleteTaskItem {

private static final Logger LOG = LoggerFactory.getLogger(UcDeleteTaskItem.class);
private static final Logger STATIC_LOG = LoggerFactory.getLogger(UcDeleteTaskItem.class);

Logger log = STATIC_LOG;

@Inject
TaskItemRepository taskItemRepository;
Expand All @@ -41,7 +43,7 @@ public void delete(TaskItemEto item) {

Long id = item.getId();
if (id == null) {
LOG.info("TaskItem {} ist transient und kann nicht gelöscht werden", item.getTitle());
log.info("TaskItem {} ist transient und kann nicht gelöscht werden", item.getTitle());
Copy link
Collaborator

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

}
this.taskItemRepository.deleteById(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quarkus Log

}
this.taskListRepository.deleteById(id);
}
Expand Down
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());
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Up @@ -129,7 +129,7 @@ void GivenAnExistingTaskListWithItems_WhenDeleted_ThenItShouldBeNotFindableById(

@Test
@TestTransaction
void GivenAnExistingTaskListWithItems_WhenDeleted_TthenItsItemsShouldBeDeletedAsWell() {
void GivenAnExistingTaskListWithItems_WhenDeleted_ThenItsItemsShouldBeDeletedAsWell() {

var idsOfTaskItems = TaskListRepositoryTest.this.classUnderTest.findById(EXISTING_TASK_LIST_ID).get().getItems()
.stream().map(TaskItemEntity::getId).collect(Collectors.toList());
Expand Down
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
}
}
Loading