Skip to content

Commit

Permalink
made sure one can run platform tests, fixed/added tests
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Jun 21, 2024
1 parent beabf1f commit 2d2499b
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 42 deletions.
13 changes: 10 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ dependencies {
}

test {
// Discover and execute JUnit4-based EventCountsTest
useJUnit()
// Discover and execute JUnit5-based tests
// Discover and execute all other JUnit5-based tests
useJUnitPlatform()
afterSuite { desc, result ->
if (!desc.parent)
Expand All @@ -67,6 +65,15 @@ test {
}
}

tasks.register('platformTests', Test) {
// Discover and execute JUnit4-based EventCountsTest
useJUnit()
description = 'Runs the platform tests.'
group = 'verification'
outputs.upToDateWhen { false }
mustRunAfter test
}

configurations {
implementation {
exclude group: 'org.slf4j', module: 'slf4j-api'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.Map;
import java.util.Objects;

import static com.redhat.devtools.intellij.telemetry.core.util.TimeUtils.isToday;

@Service
@State(
name = " com.redhat.devtools.intellij.telemetry.core.configuration.limits.EventCounts",
Expand Down Expand Up @@ -123,12 +125,12 @@ private int toTotal(String value) {

String toString(@NotNull Count count) {
long epochSecond = count.lastOccurrence.toEpochSecond(ZonedDateTime.now().getOffset());
return epochSecond + COUNT_VALUES_SEPARATOR + count.total;
return epochSecond + COUNT_VALUES_SEPARATOR + count.dailyTotal;
}

public static class Count {
private final LocalDateTime lastOccurrence;
private final int total;
private final int dailyTotal;

Count() {
this(LocalDateTime.now(), 1);
Expand All @@ -139,29 +141,33 @@ public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Count)) return false;
Count count = (Count) o;
return total == count.total && Objects.equals(lastOccurrence, count.lastOccurrence);
return dailyTotal == count.dailyTotal && Objects.equals(lastOccurrence, count.lastOccurrence);
}

@Override
public int hashCode() {
return Objects.hash(lastOccurrence, total);
return Objects.hash(lastOccurrence, dailyTotal);
}

Count(LocalDateTime lastOccurrence, int total) {
Count(LocalDateTime lastOccurrence, int dailyTotal) {
this.lastOccurrence = lastOccurrence;
this.total = total;
this.dailyTotal = dailyTotal;
}

public LocalDateTime getLastOccurrence() {
return lastOccurrence;
}

public int getTotal() {
return total;
public int getDailyTotal() {
if (isToday(lastOccurrence)) {
return dailyTotal;
} else {
return 0;
}
}

public Count newOccurrence() {
return new Count(LocalDateTime.now(), total + 1);
return new Count(LocalDateTime.now(), getDailyTotal() + 1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.text.StringUtil;
import com.redhat.devtools.intellij.telemetry.core.service.Event;
import com.redhat.devtools.intellij.telemetry.core.util.TimeUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.nio.file.attribute.FileTime;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
Expand Down Expand Up @@ -76,16 +76,9 @@ public void wasSent(Event event) {
counts.put(event);
}

private boolean canSend(Event event, EventCounts counts, PluginLimits limits) {
if (limits == null) {
return true;
}
return limits.canSend(event, getApplicableTotal(counts.get(event)));
}

private int getApplicableTotal(Count count) {
if (occurredToday(count)) {
return count.getTotal();
return count.getDailyTotal();
} else {
return 0;
}
Expand All @@ -94,7 +87,7 @@ private int getApplicableTotal(Count count) {
private static boolean occurredToday(Count count) {
return count != null
&& count.getLastOccurrence() != null
&& LocalDate.now().atStartOfDay().isBefore(count.getLastOccurrence());
&& TimeUtils.isToday(count.getLastOccurrence());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -75,4 +78,16 @@ public static Duration toDuration(String hoursMinutesSeconds) {
return duration;
}

/**
* Returns {@code true} if the given {@link LocalDate} is today.
* Returns {@code false} otherwise.
*
* @param dateTime the date/time to check whether it's today.
*
* @return true if the given date/time is today.
*/
public static boolean isToday(LocalDateTime dateTime) {
return ChronoUnit.DAYS.between(dateTime.toLocalDate(), LocalDate.now()) == 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.temporal.ChronoUnit;

import static com.redhat.devtools.intellij.telemetry.core.configuration.limits.EventCounts.Count;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
Expand Down Expand Up @@ -116,46 +118,74 @@ public void test_get_should_return_null_if_event_was_not_put_beforehand() {

public void test_put_should_create_new_count_if_it_doesnt_exist_yet() {
// given
Event event = new Event(Event.Type.USER, "event");
EventCounts counts = new EventCounts();
Count existing = counts.get(event);
Count existing = counts.get(event1);
assertThat(existing).isNull();
// when
counts.put(event);
counts.put(event1);
// then
existing = counts.get(event);
existing = counts.get(event1);
assertThat(existing).isNotNull();
}

public void test_put_should_create_new_count_with_total_of_1() {
// given
Event event = new Event(Event.Type.USER, "event");
EventCounts counts = new EventCounts();
Count existing = counts.get(event);
Count existing = counts.get(event1);
assertThat(existing).isNull();
// when
counts.put(event);
counts.put(event1);
// then
existing = counts.get(event);
assertThat(existing.getTotal()).isEqualTo(1);
existing = counts.get(event1);
assertThat(existing.getDailyTotal()).isEqualTo(1);
}

public void test_put_should_update_existing_count_if_it_already_existed() throws InterruptedException {
// given
Event event = new Event(Event.Type.USER, "event");
EventCounts counts = new EventCounts();
counts.put(event);
Count count = counts.get(event);
counts.put(event1);
Count count = counts.get(event1);
assertThat(count).isNotNull();
LocalDateTime previousOccurrence = count.getLastOccurrence();
int previousTotal = count.getTotal();
int previousTotal = count.getDailyTotal();
Thread.sleep(1000); // wait for 1s, timestamp is in seconds only
// when
counts.put(event);
counts.put(event1);
// then
count = counts.get(event);
count = counts.get(event1);
assertThat(count.getLastOccurrence()).isAfter(previousOccurrence);
assertThat(count.getTotal()).isEqualTo(previousTotal + 1);
assertThat(count.getDailyTotal()).isEqualTo(previousTotal + 1);
}

public void test_put_should_reset_count_to_0_existing_count_was_not_today() {
// given
EventCounts counts = new EventCounts();
int previousTotal = 42;
LocalDateTime previousOccurrence = LocalDateTime.now().minus(Period.ofDays(1));
Count count = new Count(previousOccurrence, previousTotal);
counts.put(event1, count);
// when
counts.put(event1);
// then
count = counts.get(event1);
assertThat(count.getLastOccurrence()).isAfter(previousOccurrence);
assertThat(count.getDailyTotal()).isEqualTo(1);
}

public void test_put_of_different_event_should_not_affect_existing_count_for_event() {
// given
EventCounts counts = new EventCounts();
counts.put(event1);
Count count = counts.get(event1);
assertThat(count).isNotNull();
LocalDateTime existingLastOccurrence = count.getLastOccurrence();
int existingTotal = count.getDailyTotal();
// when
counts.put(event2);
// then
count = counts.get(event1);
assertThat(count.getLastOccurrence()).isEqualTo(existingLastOccurrence);
assertThat(count.getDailyTotal()).isEqualTo(existingTotal);
}

private Document toDocument(String string) throws JDOMException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
class TelemetryMessageBuilderIntegrationTest {

private static final String EXTENSION_NAME = "com.redhat.devtools.intellij.telemetry";
private static final String EXTENSION_VERSION = "1.0.0.44";
private static final String EXTENSION_VERSION = "1.2.0-SNAPSHOT";
private static final String APPLICATION_VERSION = "1.0.0";
private static final String APPLICATION_NAME = TelemetryMessageBuilderIntegrationTest.class.getSimpleName();
private static final String PLATFORM_NAME = "smurfOS";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,55 @@ class TelemetryServiceTest {
private IEventLimits limits;
private SegmentBroker broker;
private MessageBusConnection bus;
private TelemetryConfiguration configuration;
private IService service;
private Event event;
private TelemetryNotifications notifications;

@BeforeEach
void before() {
this.limits = mock(IEventLimits.class);
this.limits = createEventLimits();
this.broker = createSegmentBroker();
this.bus = createMessageBusConnection();
this.notifications = createTelemetryNotifications();
TelemetryConfiguration configuration = telemetryConfiguration(true, true);
this.configuration = telemetryConfiguration(true, true);
this.service = new TelemetryService(configuration, limits, broker, bus, notifications);
this.event = new Event(null, "Testing Telemetry", null);
}

@Test
void send_should_send_if_is_enabled() {
void send_should_send_if_is_enabled_and_limits_allow_it() {
// given
// when
service.send(event);
// then
verify(broker, atLeastOnce()).send(any(Event.class));
}

@Test
void send_should_NOT_send_if_is_enabled_but_limits_DONT_allow_it() {
// given
doReturn(false)
.when(limits).canSend(any());
// when
service.send(event);
// then
verify(broker, never()).send(any(Event.class));
}

@Test
void send_should_NOT_send_if_is_NOT_enabled_but_limits_allow_it() {
// given
doReturn(false)
.when(configuration).isEnabled();
doReturn(true)
.when(limits).canSend(any());
// when
service.send(event);
// then
verify(broker, never()).send(any(Event.class));
}

@Test
void send_should_NOT_send_if_is_NOT_configured() {
// given
Expand Down Expand Up @@ -159,6 +184,13 @@ void send_should_NOT_send_if_limits_DONT_allow_it() {
verify(broker, never()).send(event);
}

private IEventLimits createEventLimits() {
IEventLimits mock = mock(IEventLimits.class);
doReturn(true)
.when(mock).canSend(any());
return mock;
}

private SegmentBroker createSegmentBroker() {
return mock(SegmentBroker.class);
}
Expand Down

0 comments on commit 2d2499b

Please sign in to comment.