Skip to content

Commit 8e29c0a

Browse files
author
François Delbrayelle
committed
fix: attempt to fix tests
1 parent c0a2049 commit 8e29c0a

File tree

3 files changed

+65
-75
lines changed

3 files changed

+65
-75
lines changed

src/test/java/io/kestra/plugin/aws/cloudwatch/PushTest.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,37 @@
22

33
import io.kestra.core.junit.annotations.KestraTest;
44
import io.kestra.core.models.property.Property;
5-
import io.kestra.core.runners.RunContext;
65
import io.kestra.core.runners.RunContextFactory;
76
import io.kestra.core.utils.IdUtils;
8-
import io.kestra.plugin.aws.AbstractLocalStackTest;
97
import jakarta.inject.Inject;
108
import org.junit.jupiter.api.Test;
9+
import software.amazon.awssdk.services.cloudwatch.CloudWatchClient;
10+
import software.amazon.awssdk.services.cloudwatch.model.PutMetricDataRequest;
11+
import software.amazon.awssdk.services.cloudwatch.model.PutMetricDataResponse;
1112

1213
import java.util.List;
1314
import java.util.Map;
1415

1516
import static org.hamcrest.MatcherAssert.assertThat;
1617
import static org.hamcrest.Matchers.*;
18+
import static org.mockito.ArgumentMatchers.any;
19+
import static org.mockito.Mockito.*;
1720

1821
@KestraTest
19-
class PushTest extends AbstractLocalStackTest {
22+
class PushTest {
2023

2124
@Inject
2225
protected RunContextFactory runContextFactory;
2326

2427
@Test
2528
void runPushMetric() throws Exception {
26-
RunContext runContext = runContextFactory.of();
29+
var runContext = runContextFactory.of();
30+
var client = mock(CloudWatchClient.class);
31+
when(client.putMetricData(any(PutMetricDataRequest.class))).thenReturn(PutMetricDataResponse.builder().build());
2732

28-
var push = Push.builder()
33+
var push = spy(Push.builder()
2934
.id(IdUtils.create())
3035
.type(PushTest.class.getSimpleName())
31-
.endpointOverride(Property.ofValue(localstack.getEndpoint().toString()))
32-
.region(Property.ofValue(localstack.getRegion()))
33-
.accessKeyId(Property.ofValue(localstack.getAccessKey()))
34-
.secretKeyId(Property.ofValue(localstack.getSecretKey()))
3536
.namespace(Property.ofValue("Custom/Test"))
3637
.metrics(Property.ofValue(List.of(
3738
Push.MetricValue.builder()
@@ -41,10 +42,13 @@ void runPushMetric() throws Exception {
4142
.dimensions(Property.ofValue(Map.of("env", "dev")))
4243
.build()
4344
)))
44-
.build();
45+
.build());
4546

46-
Push.Output output = push.run(runContext);
47+
doReturn(client).when(push).client(any());
48+
49+
var output = push.run(runContext);
4750

4851
assertThat(output.getCount(), equalTo(1));
52+
verify(client, times(1)).putMetricData(any(PutMetricDataRequest.class));
4953
}
5054
}

src/test/java/io/kestra/plugin/aws/cloudwatch/QueryTest.java

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,44 @@
22

33
import io.kestra.core.junit.annotations.KestraTest;
44
import io.kestra.core.models.property.Property;
5-
import io.kestra.core.runners.RunContext;
65
import io.kestra.core.runners.RunContextFactory;
7-
import io.kestra.plugin.aws.AbstractLocalStackTest;
86
import jakarta.inject.Inject;
97
import org.junit.jupiter.api.Test;
8+
import software.amazon.awssdk.services.cloudwatch.CloudWatchClient;
9+
import software.amazon.awssdk.services.cloudwatch.model.Datapoint;
10+
import software.amazon.awssdk.services.cloudwatch.model.GetMetricStatisticsRequest;
11+
import software.amazon.awssdk.services.cloudwatch.model.GetMetricStatisticsResponse;
1012

1113
import java.time.Duration;
14+
import java.time.Instant;
1215
import java.util.List;
13-
import java.util.Map;
1416

1517
import static org.hamcrest.MatcherAssert.assertThat;
1618
import static org.hamcrest.Matchers.*;
19+
import static org.mockito.ArgumentMatchers.any;
20+
import static org.mockito.Mockito.*;
1721

1822
@KestraTest
19-
class QueryTest extends AbstractLocalStackTest {
23+
class QueryTest {
2024

2125
@Inject
2226
protected RunContextFactory runContextFactory;
2327

2428
@Test
2529
void runQueryMetric() throws Exception {
26-
RunContext runContext = runContextFactory.of();
30+
var runContext = runContextFactory.of();
31+
var client = mock(CloudWatchClient.class);
2732

28-
var push = Push.builder()
29-
.endpointOverride(Property.ofValue(localstack.getEndpoint().toString()))
30-
.region(Property.ofValue(localstack.getRegion()))
31-
.accessKeyId(Property.ofValue(localstack.getAccessKey()))
32-
.secretKeyId(Property.ofValue(localstack.getSecretKey()))
33-
.namespace(Property.ofValue("Custom/Test"))
34-
.metrics(Property.ofValue(List.of(
35-
Push.MetricValue.builder()
36-
.metricName(Property.ofValue("LatencyMs"))
37-
.value(Property.ofValue(123.45))
38-
.unit(Property.ofValue("Milliseconds"))
39-
.dimensions(Property.ofValue(Map.of("env", "dev")))
40-
.build()
41-
)))
42-
.build();
33+
var oldTimestamp = Instant.parse("2026-02-12T16:00:00Z");
34+
var newTimestamp = Instant.parse("2026-02-12T16:01:00Z");
35+
when(client.getMetricStatistics(any(GetMetricStatisticsRequest.class))).thenReturn(GetMetricStatisticsResponse.builder()
36+
.datapoints(
37+
Datapoint.builder().timestamp(newTimestamp).average(22.0).build(),
38+
Datapoint.builder().timestamp(oldTimestamp).average(11.0).build()
39+
)
40+
.build());
4341

44-
push.run(runContext);
45-
46-
var query = Query.builder()
47-
.endpointOverride(Property.ofValue(localstack.getEndpoint().toString()))
48-
.region(Property.ofValue(localstack.getRegion()))
49-
.accessKeyId(Property.ofValue(localstack.getAccessKey()))
50-
.secretKeyId(Property.ofValue(localstack.getSecretKey()))
42+
var query = spy(Query.builder()
5143
.namespace(Property.ofValue("Custom/Test"))
5244
.metricName(Property.ofValue("LatencyMs"))
5345
.statistic(Property.ofValue("Average"))
@@ -59,11 +51,18 @@ void runQueryMetric() throws Exception {
5951
.value(Property.ofValue("dev"))
6052
.build()
6153
)))
62-
.build();
54+
.build());
55+
56+
doReturn(client).when(query).client(any());
6357

64-
Query.Output out = query.run(runContext);
58+
var out = query.run(runContext);
6559

66-
assertThat(out.getSeries(), notNullValue());
67-
assertThat(out.getCount(), greaterThanOrEqualTo(0));
60+
assertThat(out.getCount(), is(2));
61+
assertThat(out.getSeries(), hasSize(2));
62+
assertThat(out.getSeries().getFirst().values(), hasItem(oldTimestamp.toString()));
63+
assertThat(out.getSeries().get(1).values(), hasItem(newTimestamp.toString()));
64+
assertThat(out.getSeries().getFirst().values(), hasItem(11.0));
65+
assertThat(out.getSeries().get(1).values(), hasItem(22.0));
66+
verify(client, times(1)).getMetricStatistics(any(GetMetricStatisticsRequest.class));
6867
}
6968
}
Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,35 @@
11
package io.kestra.plugin.aws.cloudwatch;
22

33
import io.kestra.core.junit.annotations.KestraTest;
4-
import io.kestra.core.runners.RunContext;
54
import io.kestra.core.runners.RunContextFactory;
65
import io.kestra.core.utils.IdUtils;
7-
import io.kestra.plugin.aws.AbstractLocalStackTest;
86
import io.kestra.core.models.property.Property;
97
import jakarta.inject.Inject;
108
import org.junit.jupiter.api.Test;
9+
import org.mockito.MockedConstruction;
1110

1211
import java.time.Duration;
1312
import java.util.List;
1413
import java.util.Map;
1514

1615
import static org.hamcrest.MatcherAssert.assertThat;
1716
import static org.hamcrest.Matchers.*;
17+
import static org.mockito.ArgumentMatchers.any;
18+
import static org.mockito.Mockito.mockConstruction;
19+
import static org.mockito.Mockito.verify;
20+
import static org.mockito.Mockito.when;
1821

1922
@KestraTest
20-
class TriggerTest extends AbstractLocalStackTest {
23+
class TriggerTest {
2124

2225
@Inject
2326
protected RunContextFactory runContextFactory;
2427

2528
@Test
2629
void evaluate() throws Exception {
27-
RunContext runContext = runContextFactory.of();
28-
29-
var push = Push.builder()
30-
.id(IdUtils.create())
31-
.type(TriggerTest.class.getSimpleName())
32-
.endpointOverride(Property.ofValue(localstack.getEndpoint().toString()))
33-
.region(Property.ofValue(localstack.getRegion()))
34-
.accessKeyId(Property.ofValue(localstack.getAccessKey()))
35-
.secretKeyId(Property.ofValue(localstack.getSecretKey()))
36-
.namespace(Property.ofValue("Custom/Test"))
37-
.metrics(Property.ofValue(List.of(
38-
Push.MetricValue.builder()
39-
.metricName(Property.ofValue("TriggerLatency"))
40-
.value(Property.ofValue(456.7))
41-
.unit(Property.ofValue("Milliseconds"))
42-
.dimensions(Property.ofValue(Map.of("env", "test")))
43-
.build()
44-
)))
45-
.build();
46-
47-
Push.Output pushOutput = push.run(runContext);
48-
assertThat(pushOutput.getCount(), equalTo(1));
49-
5030
var trigger = Trigger.builder()
5131
.id(IdUtils.create())
5232
.type(TriggerTest.class.getSimpleName())
53-
.endpointOverride(Property.ofValue(localstack.getEndpoint().toString()))
54-
.region(Property.ofValue(localstack.getRegion()))
55-
.accessKeyId(Property.ofValue(localstack.getAccessKey()))
56-
.secretKeyId(Property.ofValue(localstack.getSecretKey()))
5733
.namespace(Property.ofValue("Custom/Test"))
5834
.metricName(Property.ofValue("TriggerLatency"))
5935
.statistic(Property.ofValue("Average"))
@@ -67,9 +43,20 @@ void evaluate() throws Exception {
6743
)))
6844
.build();
6945

70-
var conditionContext = io.kestra.core.utils.TestsUtils.mockTrigger(runContextFactory, trigger);
71-
var execution = trigger.evaluate(conditionContext.getKey(), conditionContext.getValue());
46+
var output = Query.Output.builder()
47+
.count(1)
48+
.series(List.of(Map.of("average", 456.7)))
49+
.build();
50+
51+
try (MockedConstruction<Query> mockedQuery = mockConstruction(Query.class, (mock, context) ->
52+
when(mock.run(any())).thenReturn(output)
53+
)) {
54+
var conditionContext = io.kestra.core.utils.TestsUtils.mockTrigger(runContextFactory, trigger);
55+
var execution = trigger.evaluate(conditionContext.getKey(), conditionContext.getValue());
7256

73-
assertThat(execution.isPresent(), is(true));
57+
assertThat(execution.isPresent(), is(true));
58+
assertThat(mockedQuery.constructed(), hasSize(1));
59+
verify(mockedQuery.constructed().getFirst()).run(any());
60+
}
7461
}
7562
}

0 commit comments

Comments
 (0)