Skip to content

Commit

Permalink
[#1716] Add tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Kartheeswaran Kalidass <Kartheeswaran.Kalidass@bosch.io>
  • Loading branch information
kaniyan authored and sophokles73 committed Jan 30, 2020
1 parent d093305 commit 18d4109
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 4 deletions.
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
* Copyright (c) 2019, 2020 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
Expand Down Expand Up @@ -218,7 +218,12 @@ public void testDecodeResourceLimits() {
.put(TenantConstants.FIELD_EFFECTIVE_SINCE, "2019-04-25T14:30:00+02:00")
.put(TenantConstants.FIELD_PERIOD, new JsonObject()
.put(TenantConstants.FIELD_PERIOD_MODE, "days")
.put(TenantConstants.FIELD_PERIOD_NO_OF_DAYS, 90))));
.put(TenantConstants.FIELD_PERIOD_NO_OF_DAYS, 90)))
.put(TenantConstants.FIELD_CONNECTION_DURATION, new JsonObject()
.put(TenantConstants.FIELD_MAX_MINUTES, 20_000_000)
.put(TenantConstants.FIELD_EFFECTIVE_SINCE, "2019-04-25T14:30:00+02:00")
.put(TenantConstants.FIELD_PERIOD, new JsonObject()
.put(TenantConstants.FIELD_PERIOD_MODE, "monthly"))));

final Tenant tenant = tenantSpec.mapTo(Tenant.class);
assertNotNull(tenant);
Expand All @@ -236,6 +241,14 @@ public void testDecodeResourceLimits() {
assertNotNull(limits.getDataVolume().getPeriod());
assertEquals("days", limits.getDataVolume().getPeriod().getMode());
assertEquals(90, limits.getDataVolume().getPeriod().getNoOfDays());
assertNotNull(limits.getConnectionDuration());
assertEquals(
DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse("2019-04-25T14:30:00+02:00", OffsetDateTime::from)
.toInstant(),
limits.getConnectionDuration().getEffectiveSince());
assertEquals(20_000_000, limits.getConnectionDuration().getMaxMinutes());
assertNotNull(limits.getConnectionDuration().getPeriod());
assertEquals("monthly", limits.getConnectionDuration().getPeriod().getMode());
}

/**
Expand Down
Expand Up @@ -36,10 +36,12 @@

import org.eclipse.hono.cache.CacheProvider;
import org.eclipse.hono.cache.ExpiringValueCache;
import org.eclipse.hono.util.ConnectionDuration;
import org.eclipse.hono.util.Constants;
import org.eclipse.hono.util.DataVolume;
import org.eclipse.hono.util.DataVolumePeriod;
import org.eclipse.hono.util.ResourceLimits;
import org.eclipse.hono.util.ResourceLimitsPeriod;
import org.eclipse.hono.util.TenantObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -260,11 +262,11 @@ public void testMessageLimitNotExceededForMissingMetrics(final VertxTestContext
}

/**
* Verifies the calculated data volume for various scenarios.
* Verifies the effective resource limit calculation for various scenarios.
*
*/
@Test
public void testCalculateDataVolume() {
public void verifyEffectiveResourceLimitCalculation() {
final long maxBytes = 9300;

// Monthly mode
Expand Down Expand Up @@ -426,6 +428,93 @@ public void testMessageLimitStoresValueToCache(final VertxTestContext ctx) {
}));
}

/**
*
* Verifies that the connection duration limit check returns {@code false} if the limit is not exceeded.
*
* @param ctx The vert.x test context.
*/
@Test
public void testConnectionDurationLimitNotExceeded(final VertxTestContext ctx) {

givenDeviceConnectionDurationInMinutes(90);
final TenantObject tenant = TenantObject.from("tenant", true)
.setResourceLimits(new ResourceLimits()
.setConnectionDuration(new ConnectionDuration()
.setMaxDuration(100L)
.setEffectiveSince(Instant.parse("2019-01-03T14:30:00Z"))
.setPeriod(new ResourceLimitsPeriod()
.setMode("days")
.setNoOfDays(30))));
limitChecksImpl.isConnectionDurationLimitReached(tenant, mock(SpanContext.class))
.setHandler(ctx.succeeding(response -> {
ctx.verify(() -> {
assertFalse(response);
verify(webClient).get(eq(DEFAULT_PORT), eq(DEFAULT_HOST), anyString());
});
ctx.completeNow();
}));
}

/**
*
* Verifies that the connection duration limit check returns {@code true} if the limit is exceeded.
*
* @param ctx The vert.x test context.
*/
@Test
public void testConnectionDurationLimitExceeded(final VertxTestContext ctx) {

givenDeviceConnectionDurationInMinutes(100);
final TenantObject tenant = TenantObject.from("tenant", true)
.setResourceLimits(new ResourceLimits()
.setConnectionDuration(new ConnectionDuration()
.setMaxDuration(100L)
.setEffectiveSince(Instant.parse("2019-01-03T14:30:00Z"))
.setPeriod(new ResourceLimitsPeriod()
.setMode("days")
.setNoOfDays(30))));
limitChecksImpl.isConnectionDurationLimitReached(tenant, mock(SpanContext.class))
.setHandler(ctx.succeeding(response -> {
ctx.verify(() -> {
assertTrue(response);
verify(webClient).get(eq(DEFAULT_PORT), eq(DEFAULT_HOST), anyString());
});
ctx.completeNow();
}));
}

/**
* Verifies that the connection duration limit check returns {@code false} if no metrics are
* available (yet).
*
* @param ctx The vert.x test context.
*/
@Test
public void testConnectionDurationLimitNotExceededForMissingMetrics(final VertxTestContext ctx) {

givenDeviceConnectionDurationInMinutes(null);
final TenantObject tenant = TenantObject.from("tenant", true)
.setResourceLimits(new ResourceLimits()
.setConnectionDuration(new ConnectionDuration()
.setMaxDuration(100L)
.setEffectiveSince(Instant.parse("2019-01-03T14:30:00Z"))
.setPeriod(new ResourceLimitsPeriod()
.setMode("days")
.setNoOfDays(30))));
limitChecksImpl.isConnectionDurationLimitReached(tenant, mock(SpanContext.class))
.setHandler(ctx.succeeding(response -> {
ctx.verify(() -> {
// THEN the limit is not exceeded
assertFalse(response);
verify(webClient).get(eq(DEFAULT_PORT), eq(DEFAULT_HOST), anyString());
// AND the span is not marked as erroneous
verify(span).log(argThat((Map<String, ?> map) -> !"error".equals(map.get(Fields.EVENT))));
});
ctx.completeNow();
}));
}

private void givenCurrentConnections(final Integer currentConnections) {
givenResponseWithValue(currentConnections);
}
Expand All @@ -434,6 +523,10 @@ private void givenDataVolumeUsageInBytes(final Integer consumedBytes) {
givenResponseWithValue(consumedBytes);
}

private void givenDeviceConnectionDurationInMinutes(final Integer consumedMinutes) {
givenResponseWithValue(consumedMinutes);
}

@SuppressWarnings("unchecked")
private void givenResponseWithValue(final Integer value) {
doAnswer(invocation -> {
Expand Down

0 comments on commit 18d4109

Please sign in to comment.