From 89687952d3be151ba1d29f86771cbb59c57cf05c Mon Sep 17 00:00:00 2001 From: Dominik Herbst Date: Thu, 13 Oct 2016 08:29:01 +0200 Subject: [PATCH] Improved code readability. Signed-off-by: Dominik Herbst --- .../repository/jpa/JpaTargetManagement.java | 12 ++++++--- .../repository/jpa/rsql/RSQLUtilityTest.java | 12 ++++----- .../jpa/rsql/VirtualPropertyResolverTest.java | 25 ++++++++++--------- .../targettable/TargetBeanQuery.java | 5 +--- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaTargetManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaTargetManagement.java index 75e883c83..5eddbee0d 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaTargetManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaTargetManagement.java @@ -27,6 +27,7 @@ import javax.persistence.criteria.Order; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; +import org.apache.commons.lang3.StringUtils; import org.eclipse.hawkbit.repository.FilterParams; import org.eclipse.hawkbit.repository.TargetFields; import org.eclipse.hawkbit.repository.TargetManagement; @@ -68,7 +69,6 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; import org.springframework.validation.annotation.Validated; -import com.google.common.base.Strings; import com.google.common.collect.Lists; import com.google.common.eventbus.EventBus; @@ -315,17 +315,21 @@ public class JpaTargetManagement implements TargetManagement { TargetSpecifications .hasInstalledOrAssignedDistributionSet(filterParams.getFilterByDistributionId())); } - if (!Strings.isNullOrEmpty(filterParams.getFilterBySearchText())) { + if (StringUtils.isNotEmpty(filterParams.getFilterBySearchText())) { specList.add(TargetSpecifications.likeNameOrDescriptionOrIp(filterParams.getFilterBySearchText())); } - if (filterParams.getSelectTargetWithNoTag() != null && (filterParams.getSelectTargetWithNoTag() - || (filterParams.getFilterByTagNames() != null && filterParams.getFilterByTagNames().length > 0))) { + if (isHasTagsFilterActive(filterParams)) { specList.add(TargetSpecifications.hasTags(filterParams.getFilterByTagNames(), filterParams.getSelectTargetWithNoTag())); } return specList; } + private static boolean isHasTagsFilterActive(final FilterParams filterParams) { + return filterParams.getSelectTargetWithNoTag() != null && (filterParams.getSelectTargetWithNoTag() + || (filterParams.getFilterByTagNames() != null && filterParams.getFilterByTagNames().length > 0)); + } + private Slice findByCriteriaAPI(final Pageable pageable, final List> specList) { if (specList == null || specList.isEmpty()) { return convertPage(criteriaNoCountDao.findAll(pageable, JpaTarget.class), pageable); diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/rsql/RSQLUtilityTest.java b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/rsql/RSQLUtilityTest.java index b4270e732..20f5305c6 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/rsql/RSQLUtilityTest.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/rsql/RSQLUtilityTest.java @@ -50,13 +50,13 @@ import ru.yandex.qatools.allure.annotations.Stories; public class RSQLUtilityTest { @Spy - VirtualPropertyResolver macroResolver = new VirtualPropertyResolver(new TimestampCalculator()); + private VirtualPropertyResolver macroResolver = new VirtualPropertyResolver(new TimestampCalculator()); @Mock - TenantConfigurationManagement confMgmt; + private TenantConfigurationManagement confMgmt; @Mock - TimestampCalculator timestampCalculator; + private TimestampCalculator timestampCalculator; @Mock private Root baseSoftwareModuleRootMock; @@ -287,11 +287,11 @@ public class RSQLUtilityTest { .thenReturn(mock(Predicate.class)); // test - Predicate result = RSQLUtility.parse(correctRsql, TestFieldEnum.class, setupMacroLookup()) + RSQLUtility.parse(correctRsql, TestFieldEnum.class, setupMacroLookup()) .toPredicate(baseSoftwareModuleRootMock, criteriaQueryMock, criteriaBuilderMock); - // verfication - verify(macroResolver, times(1)).lookup(overdueProp); + // verification + verify(macroResolver).lookup(overdueProp); // the macro is already replaced when passed to #lessThanOrEqualTo -> the method is never invoked with the // placeholder: verify(criteriaBuilderMock, never()).lessThanOrEqualTo(eq(pathOfString(baseSoftwareModuleRootMock)), diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/rsql/VirtualPropertyResolverTest.java b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/rsql/VirtualPropertyResolverTest.java index 0deb88b53..d37a2d1ee 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/rsql/VirtualPropertyResolverTest.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/rsql/VirtualPropertyResolverTest.java @@ -9,9 +9,9 @@ package org.eclipse.hawkbit.repository.jpa.rsql; import static org.junit.Assert.*; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; - +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.not; import java.time.Instant; import org.apache.commons.lang3.text.StrSubstitutor; @@ -19,6 +19,7 @@ import org.eclipse.hawkbit.repository.TenantConfigurationManagement; import org.eclipse.hawkbit.repository.jpa.TimestampCalculator; import org.eclipse.hawkbit.repository.model.TenantConfigurationValue; import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationKey; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -36,14 +37,14 @@ import ru.yandex.qatools.allure.annotations.Stories; public class VirtualPropertyResolverTest { @Spy - VirtualPropertyResolver resolverUnderTest = new VirtualPropertyResolver(new TimestampCalculator()); + private VirtualPropertyResolver resolverUnderTest = new VirtualPropertyResolver(new TimestampCalculator()); @Mock - TenantConfigurationManagement confMgmt; + private TenantConfigurationManagement confMgmt; - StrSubstitutor substitutor; + private StrSubstitutor substitutor; - Long nowTestTime; + private Long nowTestTime; private static final TenantConfigurationValue TEST_POLLING_TIME_INTERVAL = TenantConfigurationValue . builder().value("00:05:00").build(); @@ -65,7 +66,7 @@ public class VirtualPropertyResolverTest { } }); - this.substitutor = new StrSubstitutor(resolverUnderTest, + substitutor = new StrSubstitutor(resolverUnderTest, StrSubstitutor.DEFAULT_PREFIX, StrSubstitutor.DEFAULT_SUFFIX, StrSubstitutor.DEFAULT_ESCAPE); } @@ -77,7 +78,7 @@ public class VirtualPropertyResolverTest { String testString = "lhs=lt=" + placeholder; String resolvedPlaceholders = substitutor.replace(testString); - assertFalse("NOW_TS has to be resolved!", resolvedPlaceholders.contains(placeholder)); + assertThat("NOW_TS has to be resolved!", resolvedPlaceholders, not(containsString(placeholder))); } @Test @@ -87,7 +88,7 @@ public class VirtualPropertyResolverTest { String testString = "lhs=lt=" + placeholder; String resolvedPlaceholders = substitutor.replace(testString); - assertFalse("OVERDUE_TS has to be resolved!", resolvedPlaceholders.contains(placeholder)); + assertThat("OVERDUE_TS has to be resolved!", resolvedPlaceholders, not(containsString(placeholder))); } @Test @@ -97,7 +98,7 @@ public class VirtualPropertyResolverTest { String testString = "lhs=lt=" + placeholder; String resolvedPlaceholders = substitutor.replace(testString); - assertFalse("overdue_ts has to be resolved!", resolvedPlaceholders.contains(placeholder)); + assertThat("overdue_ts has to be resolved!", resolvedPlaceholders, not(containsString(placeholder))); } @Test @@ -107,7 +108,7 @@ public class VirtualPropertyResolverTest { String testString = "lhs=lt=" + placeholder; String resolvedPlaceholders = substitutor.replace(testString); - assertTrue("unknown should not be resolved!", resolvedPlaceholders.contains(placeholder)); + assertThat("unknown should not be resolved!", resolvedPlaceholders, containsString(placeholder)); } @Test @@ -118,6 +119,6 @@ public class VirtualPropertyResolverTest { String testString = "lhs=lt=" + escaptedPlaceholder; String resolvedPlaceholders = substitutor.replace(testString); - assertTrue("Escaped OVERDUE_TS should not be resolved!", resolvedPlaceholders.contains(placeholder)); + assertThat("Escaped OVERDUE_TS should not be resolved!", resolvedPlaceholders, containsString(placeholder)); } } diff --git a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/management/targettable/TargetBeanQuery.java b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/management/targettable/TargetBeanQuery.java index ca75fc578..6a35b1c6f 100644 --- a/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/management/targettable/TargetBeanQuery.java +++ b/hawkbit-ui/src/main/java/org/eclipse/hawkbit/ui/management/targettable/TargetBeanQuery.java @@ -173,10 +173,7 @@ public class TargetBeanQuery extends AbstractBeanQuery { } private Boolean isOverdueFilterEnabled() { - if (Boolean.TRUE.equals(overdueState)) { - return Boolean.TRUE; - } - return Boolean.FALSE; + return Boolean.TRUE.equals(overdueState); } @Override