Skip to content

Commit

Permalink
Closes kadai-io#162 - Fix some SonarCloud issues in Backend
Browse files Browse the repository at this point in the history
  • Loading branch information
CRoberto1926 committed Nov 18, 2024
1 parent 4cd05d5 commit 8eae041
Show file tree
Hide file tree
Showing 98 changed files with 971 additions and 1,222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,51 @@ private DockerContainerCreator() {
}

public static Optional<JdbcDatabaseContainer<?>> createDockerContainer(DB db) {
switch (db) {
case DB2:
return Optional.of(
return switch (db) {
case DB2 -> {
try (Db2Container db2Container =
new Db2Container(
DockerImageName.parse("taskana/db2:11.5")
.asCompatibleSubstituteFor("ibmcom/db2"))
.waitingFor(
new LogMessageWaitStrategy()
.withRegEx(".*DB2START processing was successful.*")
.withStartupTimeout(Duration.of(60, SECONDS)))
.withUsername("db2inst1")
.withPassword("db2inst1-pwd")
.withDatabaseName("TSKDB"));
case POSTGRES:
return Optional.of(
new PostgreSQLContainer<>(DockerImageName.parse("postgres:14.7"))
.withUsername("postgres")
.withPassword("postgres")
.withDatabaseName("postgres")
.withCommand(
"/bin/sh",
"-c",
"localedef -i de_DE -c -f UTF-8 -A /usr/share/locale/locale.alias de_DE.UTF-8 "
+ "&& export LANG=de_DE.UTF-8 "
+ "&& /usr/local/bin/docker-entrypoint.sh postgres -c fsync=off")
.waitingFor(
new LogMessageWaitStrategy()
.withRegEx(".*Datenbanksystem ist bereit, um Verbindungen anzunehmen.*\\s")
.withTimes(2)
.withStartupTimeout(Duration.of(60, SECONDS))));
default:
return Optional.empty();
}
DockerImageName.parse("taskana/db2:11.5")
.asCompatibleSubstituteFor("ibmcom/db2"))) {

yield Optional.of(
db2Container
.waitingFor(
new LogMessageWaitStrategy()
.withRegEx(".*DB2START processing was successful.*")
.withStartupTimeout(Duration.of(60, SECONDS)))
.withUsername("db2inst1")
.withPassword("db2inst1-pwd")
.withDatabaseName("TSKDB"));
}
}
case POSTGRES -> {
try (PostgreSQLContainer<?> selfPostgreSQLContainer =
new PostgreSQLContainer<>(DockerImageName.parse("postgres:14.7"))) {

yield Optional.of(
selfPostgreSQLContainer
.withUsername("postgres")
.withPassword("postgres")
.withDatabaseName("postgres")
.withCommand(
"/bin/sh",
"-c",
"""
localedef -i de_DE -c -f UTF-8 -A \
/usr/share/locale/locale.alias de_DE.UTF-8 \
&& export LANG=de_DE.UTF-8 \
&& /usr/local/bin/docker-entrypoint.sh postgres -c fsync=off""")
.waitingFor(
new LogMessageWaitStrategy()
.withRegEx(
".*Datenbanksystem ist bereit, um Verbindungen anzunehmen.*\\s")
.withTimes(2)
.withStartupTimeout(Duration.of(60, SECONDS))));
}
}
default -> Optional.empty();
};
}

public static DataSource createDataSource(JdbcDatabaseContainer<?> container) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ private static void persistDynamicContainerChildren(
Iterable<DynamicNode> nodes, Map<String, List<DynamicNode>> childrenMap) {
nodes.forEach(
node -> {
if (node instanceof DynamicContainer) {
DynamicContainer container = (DynamicContainer) node;
if (node instanceof DynamicContainer container) {
List<DynamicNode> children = container.getChildren().collect(Collectors.toList());
childrenMap.put(container.hashCode() + container.getDisplayName(), children);
persistDynamicContainerChildren(children, childrenMap);
Expand All @@ -81,8 +80,7 @@ private static void persistDynamicContainerChildren(

private static DynamicNode duplicateDynamicNode(
DynamicNode node, Map<String, List<DynamicNode>> lookupMap) {
if (node instanceof DynamicContainer) {
DynamicContainer container = (DynamicContainer) node;
if (node instanceof DynamicContainer container) {
Stream<DynamicNode> children =
lookupMap.get(node.hashCode() + node.getDisplayName()).stream()
.map(x -> duplicateDynamicNode(x, lookupMap));
Expand Down Expand Up @@ -191,17 +189,17 @@ public <T> T interceptTestFactoryMethod(

Iterable<DynamicNode> newChildrenForDynamicContainer;
// TestFactory must have one of the following return types. See link above for further details
if (factoryResult instanceof DynamicNode) {
newChildrenForDynamicContainer = Collections.singleton((DynamicNode) factoryResult);
} else if (factoryResult instanceof Stream) {
Stream<DynamicNode> nodes = (Stream<DynamicNode>) factoryResult;
if (factoryResult instanceof DynamicNode dynamicNode) {
newChildrenForDynamicContainer = Collections.singleton(dynamicNode);
} else if (factoryResult instanceof Stream<?> stream) {
Stream<DynamicNode> nodes = (Stream<DynamicNode>) stream;
newChildrenForDynamicContainer = nodes.toList();
} else if (factoryResult instanceof Iterable) {
newChildrenForDynamicContainer = (Iterable<DynamicNode>) factoryResult;
} else if (factoryResult instanceof Iterator) {
newChildrenForDynamicContainer = () -> (Iterator<DynamicNode>) factoryResult;
} else if (factoryResult instanceof DynamicNode[]) {
newChildrenForDynamicContainer = Arrays.asList((DynamicNode[]) factoryResult);
} else if (factoryResult instanceof Iterable<?> iterable) {
newChildrenForDynamicContainer = (Iterable<DynamicNode>) iterable;
} else if (factoryResult instanceof Iterator<?> iterator) {
newChildrenForDynamicContainer = () -> (Iterator<DynamicNode>) iterator;
} else if (factoryResult instanceof DynamicNode[] dynamicNodes) {
newChildrenForDynamicContainer = Arrays.asList(dynamicNodes);
} else {
throw new SystemException(
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,22 @@

import java.util.Objects;

public final class CustomHoliday {

private final Integer day;
private final Integer month;

public CustomHoliday(Integer day, Integer month) {
this.day = day;
this.month = month;
}
public record CustomHoliday(Integer day, Integer month) {

public static CustomHoliday of(Integer day, Integer month) {
return new CustomHoliday(day, month);
}

public Integer getDay() {
return day;
}

public Integer getMonth() {
return month;
}

@Override
public int hashCode() {
return Objects.hash(day, month);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof CustomHoliday)) {

if (!(obj instanceof CustomHoliday other)) {
return false;
}
CustomHoliday other = (CustomHoliday) obj;

return Objects.equals(day, other.day) && Objects.equals(month, other.month);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ public boolean run() throws SQLException {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(outWriter.toString());
}
if (!errorWriter.toString().trim().isEmpty()) {
LOGGER.error(errorWriter.toString());
if (!errorWriter.getBuffer().isEmpty()) {
String errorLogMessage = errorWriter.toString();
LOGGER.error(errorLogMessage);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,23 +269,18 @@ public int compareTo(Item item) {
return (value == 0) ? 0 : 1; // 1.0 == 1, 1.1 > 1
}

switch (item.getType()) {
case INT_ITEM:
return switch (item.getType()) {
case INT_ITEM -> {
int itemValue = ((IntItem) item).value;
return Integer.compare(value, itemValue);
case LONG_ITEM:
case BIGINTEGER_ITEM:
return -1;

case STRING_ITEM:
return 1; // 1.1 > 1-sp
yield Integer.compare(value, itemValue);
}
case LONG_ITEM, BIGINTEGER_ITEM -> -1;
case STRING_ITEM -> 1; // 1.1 > 1-sp

case LIST_ITEM:
return 1; // 1.1 > 1-1
case LIST_ITEM -> 1; // 1.1 > 1-1

default:
throw new IllegalStateException("invalid item: " + item.getClass());
}
default -> throw new IllegalStateException("invalid item: " + item.getClass());
};
}

@Override
Expand Down Expand Up @@ -406,23 +401,15 @@ public int compareTo(Item item) {
return BigInteger.ZERO.equals(value) ? 0 : 1; // 1.0 == 1, 1.1 > 1
}

switch (item.getType()) {
case INT_ITEM:
case LONG_ITEM:
return 1;
return switch (item.getType()) {
case INT_ITEM, LONG_ITEM -> 1;
case BIGINTEGER_ITEM -> value.compareTo(((BigIntegerItem) item).value);
case STRING_ITEM -> 1; // 1.1 > 1-sp

case BIGINTEGER_ITEM:
return value.compareTo(((BigIntegerItem) item).value);
case LIST_ITEM -> 1; // 1.1 > 1-1

case STRING_ITEM:
return 1; // 1.1 > 1-sp

case LIST_ITEM:
return 1; // 1.1 > 1-1

default:
throw new IllegalStateException("invalid item: " + item.getClass());
}
default -> throw new IllegalStateException("invalid item: " + item.getClass());
};
}

@Override
Expand Down Expand Up @@ -526,22 +513,15 @@ public int compareTo(Item item) {
// 1-rc < 1, 1-ga > 1
return comparableQualifier(value).compareTo(RELEASE_VERSION_INDEX);
}
switch (item.getType()) {
case INT_ITEM:
case LONG_ITEM:
case BIGINTEGER_ITEM:
return -1; // 1.any < 1.1 ?
return switch (item.getType()) {
case INT_ITEM, LONG_ITEM, BIGINTEGER_ITEM -> -1; // 1.any < 1.1 ?

case STRING_ITEM:
return comparableQualifier(value)
.compareTo(comparableQualifier(((StringItem) item).value));
case STRING_ITEM -> comparableQualifier(value)
.compareTo(comparableQualifier(((StringItem) item).value));
case LIST_ITEM -> -1; // 1.any < 1-1

case LIST_ITEM:
return -1; // 1.any < 1-1

default:
throw new IllegalStateException("invalid item: " + item.getClass());
}
default -> throw new IllegalStateException("invalid item: " + item.getClass());
};
}

@Override
Expand Down Expand Up @@ -596,9 +576,7 @@ public int compareTo(Item item) {
return first.compareTo(null);
}
switch (item.getType()) {
case INT_ITEM:
case LONG_ITEM:
case BIGINTEGER_ITEM:
case INT_ITEM, LONG_ITEM, BIGINTEGER_ITEM:
return -1; // 1-1 < 1.0.x

case STRING_ITEM:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,6 @@ private ZonedDateTime toZonedDateTime(Instant instant) {
return instant.atZone(zoneId);
}

private ZonedDateTime toZonedDateTime(LocalDateTime localDateTime) {
return localDateTime.atZone(zoneId);
}

private ZonedDateTime toZonedDateTime(LocalDate day, LocalTime time) {
return toZonedDateTime(LocalDateTime.of(day, time));
}

private LocalDate toLocalDate(Instant instant) {
return LocalDate.ofInstant(instant, zoneId);
Expand All @@ -271,6 +264,14 @@ public WorkSlot(LocalDate day, LocalTimeInterval interval) {
}
}

private ZonedDateTime toZonedDateTime(LocalDate day, LocalTime time) {
return toZonedDateTime(LocalDateTime.of(day, time));
}

private ZonedDateTime toZonedDateTime(LocalDateTime localDateTime) {
return localDateTime.atZone(zoneId);
}

private ZonedDateTime subtractWorkingTime(ZonedDateTime workStart, Duration workingTime) {
// _workStart_ might be outside the working hours. We need to adjust the end accordingly.
ZonedDateTime latestWorkEnd = min(workStart, end);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;

Expand All @@ -31,7 +30,7 @@ class CollectionUtilTest {
@Test
void should_SplitListIntoChunks_When_CallingPartitionBasedOnSize() {
List<Integer> listWith1000Entries =
IntStream.rangeClosed(1, 1000).boxed().collect(Collectors.toList());
IntStream.rangeClosed(1, 1000).boxed().toList();
assertThat(listWith1000Entries).hasSize(1000);
Collection<List<Integer>> partitions =
CollectionUtil.partitionBasedOnSize(listWith1000Entries, 100);
Expand Down
Loading

0 comments on commit 8eae041

Please sign in to comment.