diff --git a/hawkbit-autoconfigure/src/main/java/org/eclipse/hawkbit/autoconfigure/repository/JpaRepositoryAutoConfiguration.java b/hawkbit-autoconfigure/src/main/java/org/eclipse/hawkbit/autoconfigure/repository/JpaRepositoryAutoConfiguration.java index 05249f017..245e4d57a 100644 --- a/hawkbit-autoconfigure/src/main/java/org/eclipse/hawkbit/autoconfigure/repository/JpaRepositoryAutoConfiguration.java +++ b/hawkbit-autoconfigure/src/main/java/org/eclipse/hawkbit/autoconfigure/repository/JpaRepositoryAutoConfiguration.java @@ -9,7 +9,6 @@ package org.eclipse.hawkbit.autoconfigure.repository; import org.eclipse.hawkbit.EnableJpaRepository; -import org.eclipse.hawkbit.repository.jpa.TimestampCalculator; import org.eclipse.hawkbit.repository.rsql.VirtualPropertyReplacer; import org.eclipse.hawkbit.repository.jpa.rsql.VirtualPropertyResolver; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -34,7 +33,7 @@ public class JpaRepositoryAutoConfiguration { @Bean @ConditionalOnMissingBean public VirtualPropertyReplacer virtualPropertyReplacer() { - return new VirtualPropertyResolver(new TimestampCalculator()); + return new VirtualPropertyResolver(); } } diff --git a/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/repository/jpa/TimestampCalculator.java b/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/repository/jpa/TimestampCalculator.java index ea467b316..303c9ea3a 100644 --- a/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/repository/jpa/TimestampCalculator.java +++ b/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/repository/jpa/TimestampCalculator.java @@ -22,7 +22,7 @@ import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationKey; * Therefore tenant specific configuration may be considered. * */ -public class TimestampCalculator { +public final class TimestampCalculator { /** * Calculates the overdue timestamp (overdue_ts) based on the @@ -36,20 +36,20 @@ public class TimestampCalculator { * @return overdue_ts in milliseconds since Unix epoch as long * value */ - public long calculateOverdueTimestamp() { + public static long calculateOverdueTimestamp() { return Instant.now().toEpochMilli() - getDurationForKey(TenantConfigurationKey.POLLING_TIME_INTERVAL).toMillis() - getDurationForKey(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL).toMillis(); } - private Duration getDurationForKey(TenantConfigurationKey key) { + private static Duration getDurationForKey(TenantConfigurationKey key) { return DurationHelper.formattedStringToDuration(getRawStringForKey(key)); } - private String getRawStringForKey(TenantConfigurationKey key) { + private static String getRawStringForKey(TenantConfigurationKey key) { return getTenantConfigurationManagement().getConfigurationValue(key, String.class).getValue(); } - protected TenantConfigurationManagement getTenantConfigurationManagement() { + public static TenantConfigurationManagement getTenantConfigurationManagement() { return TenantConfigurationManagementHolder.getInstance().getTenantConfigurationManagement(); } } diff --git a/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/repository/jpa/rsql/VirtualPropertyResolver.java b/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/repository/jpa/rsql/VirtualPropertyResolver.java index e0f1483aa..14f23b5be 100644 --- a/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/repository/jpa/rsql/VirtualPropertyResolver.java +++ b/hawkbit-repository/hawkbit-repository-core/src/main/java/org/eclipse/hawkbit/repository/jpa/rsql/VirtualPropertyResolver.java @@ -8,12 +8,17 @@ */ package org.eclipse.hawkbit.repository.jpa.rsql; +import java.time.Duration; import java.time.Instant; import org.apache.commons.lang3.text.StrLookup; import org.apache.commons.lang3.text.StrSubstitutor; +import org.eclipse.hawkbit.repository.TenantConfigurationManagement; import org.eclipse.hawkbit.repository.jpa.TimestampCalculator; +import org.eclipse.hawkbit.repository.jpa.model.helper.TenantConfigurationManagementHolder; import org.eclipse.hawkbit.repository.rsql.VirtualPropertyReplacer; +import org.eclipse.hawkbit.tenancy.configuration.DurationHelper; +import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationKey; /** * Adds macro capabilities to RSQL expressions that are used to filter for @@ -43,21 +48,8 @@ import org.eclipse.hawkbit.repository.rsql.VirtualPropertyReplacer; */ public class VirtualPropertyResolver extends StrLookup implements VirtualPropertyReplacer { - private final TimestampCalculator timestampCalculator; - private StrSubstitutor substitutor; - /** - * Instantiates a new virtual property resolver with the given timestamp - * calculator - * - * @param timestampCalculator - * calculates timestamps - */ - public VirtualPropertyResolver(TimestampCalculator timestampCalculator) { - this.timestampCalculator = timestampCalculator; - } - @Override public String lookup(String rhs) { String resolved = null; @@ -65,7 +57,7 @@ public class VirtualPropertyResolver extends StrLookup implements Virtua if ("now_ts".equalsIgnoreCase(rhs)) { resolved = String.valueOf(Instant.now().toEpochMilli()); } else if ("overdue_ts".equalsIgnoreCase(rhs)) { - resolved = String.valueOf(getTimestampCalculator().calculateOverdueTimestamp()); + resolved = String.valueOf(TimestampCalculator.calculateOverdueTimestamp()); } return resolved; } @@ -79,7 +71,4 @@ public class VirtualPropertyResolver extends StrLookup implements Virtua return substitutor.replace(input); } - TimestampCalculator getTimestampCalculator() { - return timestampCalculator; - } } diff --git a/hawkbit-repository/hawkbit-repository-jpa/pom.xml b/hawkbit-repository/hawkbit-repository-jpa/pom.xml index 9f7a0bbb7..44f74e97c 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/pom.xml +++ b/hawkbit-repository/hawkbit-repository-jpa/pom.xml @@ -141,6 +141,16 @@ fest-assert test + + org.powermock + powermock-module-junit4 + test + + + org.powermock + powermock-api-mockito + test + 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 9a412f9aa..0240f8458 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 @@ -309,7 +309,7 @@ public class JpaTargetManagement implements TargetManagement { } if (filterParams.getOverdueState() != null) { specList.add( - TargetSpecifications.isOverdue(new TimestampCalculator().calculateOverdueTimestamp())); + TargetSpecifications.isOverdue(TimestampCalculator.calculateOverdueTimestamp())); } if (filterParams.getFilterByDistributionId() != null) { specList.add( 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 20f5305c6..db5f04c8a 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 @@ -11,6 +11,7 @@ package org.eclipse.hawkbit.repository.jpa.rsql; import static org.junit.Assert.fail; import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; +import static org.powermock.api.mockito.PowerMockito.mockStatic; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; @@ -36,28 +37,27 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Spy; -import org.mockito.runners.MockitoJUnitRunner; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import ru.yandex.qatools.allure.annotations.Description; import ru.yandex.qatools.allure.annotations.Features; import ru.yandex.qatools.allure.annotations.Stories; -@RunWith(MockitoJUnitRunner.class) +@RunWith(PowerMockRunner.class) @Features("Component Tests - Repository") @Stories("RSQL search utility") +@PrepareForTest(TimestampCalculator.class) // TODO: fully document tests -> @Description for long text and reasonable // method name as short text public class RSQLUtilityTest { @Spy - private VirtualPropertyResolver macroResolver = new VirtualPropertyResolver(new TimestampCalculator()); + private VirtualPropertyResolver macroResolver = new VirtualPropertyResolver(); @Mock private TenantConfigurationManagement confMgmt; - @Mock - private TimestampCalculator timestampCalculator; - @Mock private Root baseSoftwareModuleRootMock; @@ -315,10 +315,10 @@ public class RSQLUtilityTest { Predicate result = 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 unknown and hence never replaced -> #lessThanOrEqualTo is invoked with the placeholder: - verify(criteriaBuilderMock, times(1)).lessThanOrEqualTo(eq(pathOfString(baseSoftwareModuleRootMock)), + verify(criteriaBuilderMock).lessThanOrEqualTo(eq(pathOfString(baseSoftwareModuleRootMock)), eq(overduePropPlaceholder)); } @@ -328,12 +328,8 @@ public class RSQLUtilityTest { when(confMgmt.getConfigurationValue(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL, String.class)) .thenReturn(TEST_POLLING_OVERDUE_TIME_INTERVAL); - when(macroResolver.getTimestampCalculator()).thenReturn(new TimestampCalculator() { - @Override - protected TenantConfigurationManagement getTenantConfigurationManagement() { - return confMgmt; - } - }); + mockStatic(TimestampCalculator.class); + when(TimestampCalculator.getTenantConfigurationManagement()).thenReturn(confMgmt); return macroResolver; } 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 d37a2d1ee..473b2dc3a 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 @@ -12,6 +12,8 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.when; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; +import static org.powermock.api.mockito.PowerMockito.mockStatic; + import java.time.Instant; import org.apache.commons.lang3.text.StrSubstitutor; @@ -25,19 +27,21 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Spy; -import org.mockito.runners.MockitoJUnitRunner; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import ru.yandex.qatools.allure.annotations.Description; import ru.yandex.qatools.allure.annotations.Features; import ru.yandex.qatools.allure.annotations.Stories; @Features("Unit Tests - Repository") @Stories("Placeholder resolution for virtual properties") -@RunWith(MockitoJUnitRunner.class) +@RunWith(PowerMockRunner.class) +@PrepareForTest(TimestampCalculator.class) public class VirtualPropertyResolverTest { @Spy - private VirtualPropertyResolver resolverUnderTest = new VirtualPropertyResolver(new TimestampCalculator()); + private VirtualPropertyResolver resolverUnderTest = new VirtualPropertyResolver(); @Mock private TenantConfigurationManagement confMgmt; @@ -59,12 +63,8 @@ public class VirtualPropertyResolverTest { when(confMgmt.getConfigurationValue(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL, String.class)) .thenReturn(TEST_POLLING_OVERDUE_TIME_INTERVAL); - when(resolverUnderTest.getTimestampCalculator()).thenReturn(new TimestampCalculator() { - @Override - protected TenantConfigurationManagement getTenantConfigurationManagement() { - return confMgmt; - } - }); + mockStatic(TimestampCalculator.class); + when(TimestampCalculator.getTenantConfigurationManagement()).thenReturn(confMgmt); substitutor = new StrSubstitutor(resolverUnderTest, StrSubstitutor.DEFAULT_PREFIX, diff --git a/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/TestConfiguration.java b/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/TestConfiguration.java index 9cca4341e..ac3ed6085 100644 --- a/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/TestConfiguration.java +++ b/hawkbit-repository/hawkbit-repository-test/src/main/java/org/eclipse/hawkbit/TestConfiguration.java @@ -16,7 +16,6 @@ import org.eclipse.hawkbit.api.PropertyBasedArtifactUrlHandler; import org.eclipse.hawkbit.cache.CacheConstants; import org.eclipse.hawkbit.cache.TenancyCacheManager; import org.eclipse.hawkbit.cache.TenantAwareCacheManager; -import org.eclipse.hawkbit.repository.jpa.TimestampCalculator; import org.eclipse.hawkbit.repository.jpa.model.helper.EventBusHolder; import org.eclipse.hawkbit.repository.jpa.rsql.VirtualPropertyResolver; import org.eclipse.hawkbit.repository.rsql.VirtualPropertyReplacer; @@ -138,7 +137,7 @@ public class TestConfiguration implements AsyncConfigurer { */ @Bean public VirtualPropertyReplacer virtualPropertyReplacer() { - return new VirtualPropertyResolver(new TimestampCalculator()); + return new VirtualPropertyResolver(); } } diff --git a/pom.xml b/pom.xml index f83b97624..62bdfac1c 100644 --- a/pom.xml +++ b/pom.xml @@ -133,7 +133,7 @@ 2.0M10 1.4.22 2.6.2 - 1.5.4 + 1.6.5 1.0.2 19.0 1.5.2 @@ -675,6 +675,18 @@ ${pl.pragmatists.version} test + + org.powermock + powermock-module-junit4 + ${org.powermock.version} + test + + + org.powermock + powermock-api-mockito + ${org.powermock.version} + test + org.mariadb.jdbc mariadb-java-client