Skip to content

Commit 9056c2b

Browse files
committed
Improve test coverage
1 parent a045a43 commit 9056c2b

File tree

2 files changed

+65
-14
lines changed

2 files changed

+65
-14
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/customization/processors/EventStreamSharedEventProcessor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ public void preprocess(ServiceModel serviceModel) {
6868

6969
if (eventMemberShape == null || !eventMemberShape.isEvent()) {
7070
throw new IllegalStateException(
71-
String.format("Error: %s must be an Event shape when processing "
71+
String.format("Error: [%s] must be an Event shape when processing "
7272
+ "customization config duplicateAndRenameSharedEvents.%s",
7373
eventEntry.getKey(), eventStreamName));
7474
}
7575

7676
String newShapeName = eventEntry.getValue();
7777
if (serviceModel.getShapes().containsKey(newShapeName)) {
7878
throw new IllegalStateException(
79-
String.format("Error: %s is already in the model when processing "
79+
String.format("Error: [%s] is already in the model when processing "
8080
+ "customization config duplicateAndRenameSharedEvents.%s",
8181
newShapeName, eventStreamName));
8282
}
@@ -96,7 +96,7 @@ private static void validateIsEventStream(Shape shape, String name) {
9696
}
9797
if (!shape.isEventstream()) {
9898
throw new IllegalStateException(
99-
String.format("Error: %s must be an EventStream when processing "
99+
String.format("Error: [%s] must be an EventStream when processing "
100100
+ "customization config duplicateAndRenameSharedEvents.%s", name, name));
101101
}
102102
}
@@ -115,7 +115,7 @@ public void postprocess(IntermediateModel intermediateModel) {
115115
if (seenEvents.containsKey(memberShape.getShapeName())
116116
&& !seenEvents.get(memberShape.getShapeName()).equals(shapeModel.getShapeName())) {
117117
throw new IllegalStateException(
118-
String.format("Event %s is shared between multiple EventStreams. Apply the "
118+
String.format("Event shape `%s` is shared between multiple EventStreams. Apply the "
119119
+ "duplicateAndRenameSharedEvents customization to resolve the issue.",
120120
memberShape.getShapeName()));
121121
}

codegen/src/test/java/software/amazon/awssdk/codegen/customization/processors/EventStreamSharedEventProcessorTest.java

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,33 @@
2020
import static org.junit.jupiter.api.Assertions.assertNotNull;
2121

2222
import java.io.File;
23-
import org.junit.jupiter.api.Test;
23+
import java.util.Map;
24+
import org.junit.Before;
25+
import org.junit.Test;
2426
import software.amazon.awssdk.codegen.C2jModels;
2527
import software.amazon.awssdk.codegen.IntermediateModelBuilder;
2628
import software.amazon.awssdk.codegen.model.config.customization.CustomizationConfig;
2729
import software.amazon.awssdk.codegen.model.service.ServiceModel;
2830
import software.amazon.awssdk.codegen.model.service.Shape;
2931
import software.amazon.awssdk.codegen.utils.ModelLoaderUtils;
32+
import software.amazon.awssdk.utils.ImmutableMap;
3033

3134
public class EventStreamSharedEventProcessorTest {
3235
private static final String RESOURCE_ROOT = "/software/amazon/awssdk/codegen/customization/processors"
3336
+ "/eventstreamsharedeventprocessor/";
3437

35-
@Test
36-
public void duplicatesAndRenamesSharedEvent() {
38+
private ServiceModel serviceModel;
39+
40+
@Before
41+
public void setUp() {
3742
File serviceModelFile =
3843
new File(EventStreamSharedEventProcessorTest.class.getResource(RESOURCE_ROOT + "service-2.json").getFile());
39-
ServiceModel serviceModel = ModelLoaderUtils.loadModel(ServiceModel.class, serviceModelFile);
40-
File customizationConfigFile =
44+
serviceModel = ModelLoaderUtils.loadModel(ServiceModel.class, serviceModelFile);
45+
}
46+
47+
@Test
48+
public void duplicatesAndRenamesSharedEvent() {
49+
File customizationConfigFile =
4150
new File(EventStreamSharedEventProcessorTest.class.getResource(RESOURCE_ROOT + "customization.config").getFile());
4251
CustomizationConfig config = ModelLoaderUtils.loadModel(CustomizationConfig.class, customizationConfigFile);
4352

@@ -55,10 +64,6 @@ public void duplicatesAndRenamesSharedEvent() {
5564

5665
@Test
5766
public void modelWithSharedEvents_raises() {
58-
File serviceModelFile =
59-
new File(EventStreamSharedEventProcessorTest.class.getResource(RESOURCE_ROOT + "service-2.json").getFile());
60-
ServiceModel serviceModel = ModelLoaderUtils.loadModel(ServiceModel.class, serviceModelFile);
61-
6267
CustomizationConfig emptyConfig = CustomizationConfig.create();
6368

6469
assertThatThrownBy(() -> new IntermediateModelBuilder(
@@ -67,6 +72,52 @@ public void modelWithSharedEvents_raises() {
6772
.customizationConfig(emptyConfig)
6873
.build()).build())
6974
.isInstanceOf(IllegalStateException.class)
70-
.hasMessageContaining("Event Payload is shared between multiple EventStreams");
75+
.hasMessageContaining("Event shape `Payload` is shared between multiple EventStreams");
76+
}
77+
78+
@Test
79+
public void invalidCustomization_missingShape() {
80+
Map<String, Map<String, String>> duplicateAndRenameSharedEvents = ImmutableMap.of("MissingShape", null);
81+
82+
EventStreamSharedEventProcessor processor =
83+
new EventStreamSharedEventProcessor(duplicateAndRenameSharedEvents);
84+
assertThatThrownBy(() -> processor.preprocess(serviceModel))
85+
.isInstanceOf(IllegalStateException.class)
86+
.hasMessageContaining("Cannot find eventstream shape [MissingShape]");
87+
}
88+
89+
@Test
90+
public void invalidCustomization_notEventStream() {
91+
Map<String, Map<String, String>> duplicateAndRenameSharedEvents = ImmutableMap.of("Payload", null);
92+
93+
EventStreamSharedEventProcessor processor =
94+
new EventStreamSharedEventProcessor(duplicateAndRenameSharedEvents);
95+
assertThatThrownBy(() -> processor.preprocess(serviceModel))
96+
.isInstanceOf(IllegalStateException.class)
97+
.hasMessageContaining("Error: [Payload] must be an EventStream");
98+
}
99+
100+
@Test
101+
public void invalidCustomization_invalidMember() {
102+
Map<String, Map<String, String>> duplicateAndRenameSharedEvents = ImmutableMap.of(
103+
"StreamB", ImmutableMap.of("InvalidMember", "Payload"));
104+
105+
EventStreamSharedEventProcessor processor =
106+
new EventStreamSharedEventProcessor(duplicateAndRenameSharedEvents);
107+
assertThatThrownBy(() -> processor.preprocess(serviceModel))
108+
.isInstanceOf(IllegalStateException.class)
109+
.hasMessageContaining("Cannot find event member [InvalidMember] in the eventstream [StreamB]");
110+
}
111+
112+
@Test
113+
public void invalidCustomization_shapeAlreadyExists() {
114+
Map<String, Map<String, String>> duplicateAndRenameSharedEvents = ImmutableMap.of(
115+
"StreamB", ImmutableMap.of("Payload", "Payload"));
116+
117+
EventStreamSharedEventProcessor processor =
118+
new EventStreamSharedEventProcessor(duplicateAndRenameSharedEvents);
119+
assertThatThrownBy(() -> processor.preprocess(serviceModel))
120+
.isInstanceOf(IllegalStateException.class)
121+
.hasMessageContaining("Error: [Payload] is already in the model");
71122
}
72123
}

0 commit comments

Comments
 (0)