Refactored TimestampCalculator to be static and adjusted tests using PowerMock.

Signed-off-by: Dominik Herbst <dominik.herbst@bosch-si.com>
This commit is contained in:
Dominik Herbst
2016-10-14 10:32:10 +02:00
parent 36656826f8
commit d81619a1a0
9 changed files with 57 additions and 52 deletions

View File

@@ -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 (<em>overdue_ts</em>) based on the
@@ -36,20 +36,20 @@ public class TimestampCalculator {
* @return <em>overdue_ts</em> 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();
}
}

View File

@@ -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<String> 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<String> 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<String> implements Virtua
return substitutor.replace(input);
}
TimestampCalculator getTimestampCalculator() {
return timestampCalculator;
}
}