Skip to content

Commit cbff333

Browse files
committed
test: add unit tests for OutputFormat, OutputFormatMixin, and @enabled annotation on extension interfaces
1 parent 5fd4fad commit cbff333

3 files changed

Lines changed: 271 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright (c) The original authors
4+
*
5+
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
6+
*/
7+
package io.streamthoughts.jikkou.client.command;
8+
9+
import org.junit.jupiter.api.Assertions;
10+
import org.junit.jupiter.api.Test;
11+
12+
class OutputFormatMixinTest {
13+
14+
@Test
15+
void shouldReturnFormat() {
16+
// Given
17+
OutputFormatMixin mixin = new OutputFormatMixin();
18+
mixin.format = OutputFormat.JSON;
19+
20+
// When / Then
21+
Assertions.assertEquals(OutputFormat.JSON, mixin.format());
22+
}
23+
24+
@Test
25+
void shouldDefaultToNullWhenNotInitialized() {
26+
// Given
27+
OutputFormatMixin mixin = new OutputFormatMixin();
28+
29+
// When / Then
30+
Assertions.assertNull(mixin.format());
31+
}
32+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright (c) The original authors
4+
*
5+
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
6+
*/
7+
package io.streamthoughts.jikkou.client.command;
8+
9+
import java.io.ByteArrayOutputStream;
10+
import java.io.IOException;
11+
import java.nio.charset.StandardCharsets;
12+
import java.util.List;
13+
import java.util.Map;
14+
import org.junit.jupiter.api.Assertions;
15+
import org.junit.jupiter.api.Test;
16+
17+
class OutputFormatTest {
18+
19+
@Test
20+
void shouldSerializeToJson() throws IOException {
21+
// Given
22+
Map<String, String> data = Map.of("key", "value");
23+
ByteArrayOutputStream os = new ByteArrayOutputStream();
24+
25+
// When
26+
OutputFormat.JSON.serialize(data, os);
27+
28+
// Then
29+
String result = os.toString(StandardCharsets.UTF_8);
30+
Assertions.assertTrue(result.contains("\"key\""));
31+
Assertions.assertTrue(result.contains("\"value\""));
32+
}
33+
34+
@Test
35+
void shouldSerializeToYaml() throws IOException {
36+
// Given
37+
Map<String, String> data = Map.of("key", "value");
38+
ByteArrayOutputStream os = new ByteArrayOutputStream();
39+
40+
// When
41+
OutputFormat.YAML.serialize(data, os);
42+
43+
// Then
44+
String result = os.toString(StandardCharsets.UTF_8);
45+
Assertions.assertTrue(result.contains("key:"));
46+
Assertions.assertTrue(result.contains("value"));
47+
}
48+
49+
@Test
50+
void shouldThrowUnsupportedOperationExceptionWhenSerializingAsTable() {
51+
// Given
52+
Map<String, String> data = Map.of("key", "value");
53+
ByteArrayOutputStream os = new ByteArrayOutputStream();
54+
55+
// When / Then
56+
Assertions.assertThrows(UnsupportedOperationException.class,
57+
() -> OutputFormat.TABLE.serialize(data, os));
58+
}
59+
60+
@Test
61+
void shouldSerializeListToJson() throws IOException {
62+
// Given
63+
List<String> data = List.of("a", "b", "c");
64+
ByteArrayOutputStream os = new ByteArrayOutputStream();
65+
66+
// When
67+
OutputFormat.JSON.serialize(data, os);
68+
69+
// Then
70+
String result = os.toString(StandardCharsets.UTF_8);
71+
Assertions.assertTrue(result.contains("\"a\""));
72+
Assertions.assertTrue(result.contains("\"b\""));
73+
Assertions.assertTrue(result.contains("\"c\""));
74+
}
75+
76+
@Test
77+
void shouldSerializeListToYaml() throws IOException {
78+
// Given
79+
List<String> data = List.of("a", "b", "c");
80+
ByteArrayOutputStream os = new ByteArrayOutputStream();
81+
82+
// When
83+
OutputFormat.YAML.serialize(data, os);
84+
85+
// Then
86+
String result = os.toString(StandardCharsets.UTF_8);
87+
Assertions.assertTrue(result.contains("- \"a\""));
88+
Assertions.assertTrue(result.contains("- \"b\""));
89+
Assertions.assertTrue(result.contains("- \"c\""));
90+
}
91+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright (c) The original authors
4+
*
5+
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
6+
*/
7+
package io.streamthoughts.jikkou.core.extension;
8+
9+
import io.streamthoughts.jikkou.core.ReconciliationContext;
10+
import io.streamthoughts.jikkou.core.action.Action;
11+
import io.streamthoughts.jikkou.core.action.ExecutionResultSet;
12+
import io.streamthoughts.jikkou.core.annotation.Enabled;
13+
import io.streamthoughts.jikkou.core.config.Configuration;
14+
import io.streamthoughts.jikkou.core.converter.Converter;
15+
import io.streamthoughts.jikkou.core.health.Health;
16+
import io.streamthoughts.jikkou.core.health.HealthIndicator;
17+
import io.streamthoughts.jikkou.core.models.HasMetadata;
18+
import io.streamthoughts.jikkou.core.models.ResourceList;
19+
import io.streamthoughts.jikkou.core.models.ResourceType;
20+
import io.streamthoughts.jikkou.core.models.change.ResourceChange;
21+
import io.streamthoughts.jikkou.core.reconciler.ChangeExecutor;
22+
import io.streamthoughts.jikkou.core.reconciler.ChangeResult;
23+
import io.streamthoughts.jikkou.core.reconciler.Collector;
24+
import io.streamthoughts.jikkou.core.reconciler.Controller;
25+
import io.streamthoughts.jikkou.core.selector.Selector;
26+
import java.time.Duration;
27+
import java.util.Collection;
28+
import java.util.List;
29+
import org.jetbrains.annotations.NotNull;
30+
import org.junit.jupiter.api.Assertions;
31+
import org.junit.jupiter.api.Test;
32+
33+
class EnabledAnnotationOnExtensionInterfacesTest {
34+
35+
@Test
36+
void shouldDetectEnabledOnActionInterface() {
37+
Assertions.assertTrue(
38+
DefaultExtensionDescriptorFactory.isEnabled(TestAction.class));
39+
}
40+
41+
@Test
42+
void shouldDetectEnabledOnConverterInterface() {
43+
Assertions.assertTrue(
44+
DefaultExtensionDescriptorFactory.isEnabled(TestConverter.class));
45+
}
46+
47+
@Test
48+
void shouldDetectEnabledOnControllerInterface() {
49+
Assertions.assertTrue(
50+
DefaultExtensionDescriptorFactory.isEnabled(TestController.class));
51+
}
52+
53+
@Test
54+
void shouldDetectEnabledOnCollectorInterface() {
55+
Assertions.assertTrue(
56+
DefaultExtensionDescriptorFactory.isEnabled(TestCollector.class));
57+
}
58+
59+
@Test
60+
void shouldDetectEnabledOnHealthIndicatorInterface() {
61+
Assertions.assertTrue(
62+
DefaultExtensionDescriptorFactory.isEnabled(TestHealthIndicator.class));
63+
}
64+
65+
@Test
66+
void shouldNotDetectEnabledWhenExtensionHasNoEnabledAnnotation() {
67+
Assertions.assertFalse(
68+
DefaultExtensionDescriptorFactory.isEnabled(PlainExtension.class));
69+
}
70+
71+
@Test
72+
void shouldHaveEnabledAnnotationOnActionInterface() {
73+
Assertions.assertTrue(Action.class.isAnnotationPresent(Enabled.class));
74+
}
75+
76+
@Test
77+
void shouldHaveEnabledAnnotationOnConverterInterface() {
78+
Assertions.assertTrue(Converter.class.isAnnotationPresent(Enabled.class));
79+
}
80+
81+
// -- test stubs
82+
83+
static class TestAction implements Action<HasMetadata> {
84+
@Override
85+
public boolean canAccept(@NotNull ResourceType type) {
86+
return false;
87+
}
88+
89+
@Override
90+
public @NotNull ExecutionResultSet<HasMetadata> execute(@NotNull Configuration configuration) {
91+
return null;
92+
}
93+
}
94+
95+
static class TestConverter implements Converter<HasMetadata, HasMetadata> {
96+
@Override
97+
public boolean canAccept(@NotNull ResourceType type) {
98+
return false;
99+
}
100+
101+
@Override
102+
public @NotNull List<HasMetadata> apply(@NotNull HasMetadata resource) {
103+
return List.of();
104+
}
105+
}
106+
107+
static class TestController implements Controller<HasMetadata> {
108+
@Override
109+
public boolean canAccept(@NotNull ResourceType type) {
110+
return false;
111+
}
112+
113+
@Override
114+
public List<ResourceChange> plan(@NotNull Collection<HasMetadata> resources,
115+
@NotNull ReconciliationContext context) {
116+
return List.of();
117+
}
118+
119+
@Override
120+
public List<ChangeResult> execute(@NotNull ChangeExecutor executor,
121+
@NotNull ReconciliationContext context) {
122+
return List.of();
123+
}
124+
}
125+
126+
static class TestCollector implements Collector<HasMetadata> {
127+
@Override
128+
public boolean canAccept(@NotNull ResourceType type) {
129+
return false;
130+
}
131+
132+
@Override
133+
public ResourceList<HasMetadata> listAll(@NotNull Configuration configuration,
134+
@NotNull Selector selector) {
135+
return null;
136+
}
137+
}
138+
139+
static class TestHealthIndicator implements HealthIndicator {
140+
@Override
141+
public Health getHealth(Duration timeout) {
142+
return null;
143+
}
144+
}
145+
146+
static class PlainExtension implements Extension {
147+
}
148+
}

0 commit comments

Comments
 (0)