Skip to content

Commit fb4b36f

Browse files
committed
Review comments
1 parent d24e4bf commit fb4b36f

2 files changed

Lines changed: 94 additions & 2 deletions

File tree

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import java.util.Map;
2323
import org.slf4j.Logger;
2424
import org.slf4j.LoggerFactory;
25+
import software.amazon.awssdk.annotations.SdkTestInternalApi;
2526
import software.amazon.awssdk.codegen.customization.CodegenCustomizationProcessor;
2627
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
2728
import software.amazon.awssdk.codegen.model.intermediate.OperationModel;
29+
import software.amazon.awssdk.codegen.model.intermediate.Protocol;
2830
import software.amazon.awssdk.codegen.model.service.ServiceModel;
2931

3032
// TODO: Remove this when the long polling trait is formalized as a c2j trait.
@@ -48,6 +50,17 @@ public class LongPollingOperationProcessor implements CodegenCustomizationProces
4850
SERVICE_ID_TO_OPERATIONS_MAP = Collections.unmodifiableMap(serviceIdToOperationsMap);
4951
}
5052

53+
private final Map<String, List<String>> serviceIdToOperations;
54+
55+
public LongPollingOperationProcessor() {
56+
this(SERVICE_ID_TO_OPERATIONS_MAP);
57+
}
58+
59+
@SdkTestInternalApi
60+
LongPollingOperationProcessor(Map<String, List<String>> serviceIdToOperations) {
61+
this.serviceIdToOperations = serviceIdToOperations;
62+
}
63+
5164
@Override
5265
public void preprocess(ServiceModel serviceModel) {
5366
// no-op
@@ -56,15 +69,24 @@ public void preprocess(ServiceModel serviceModel) {
5669
@Override
5770
public void postprocess(IntermediateModel intermediateModel) {
5871
String serviceId = intermediateModel.getMetadata().getServiceId();
59-
List<String> longPollingOperations = SERVICE_ID_TO_OPERATIONS_MAP.getOrDefault(serviceId, Collections.emptyList());
72+
73+
if (!serviceIdToOperations.containsKey(serviceId)) {
74+
return;
75+
}
76+
77+
if (intermediateModel.getMetadata().getProtocol() != Protocol.AWS_JSON) {
78+
throw new IllegalArgumentException("Currently only AWS-JSON services can use the longPoll trait");
79+
}
80+
81+
List<String> longPollingOperations = serviceIdToOperations.getOrDefault(serviceId, Collections.emptyList());
6082

6183
for (String longPollingOperation : longPollingOperations) {
6284
OperationModel opModel = intermediateModel.getOperation(longPollingOperation);
6385
if (opModel != null) {
6486
log.info("Setting the longPoll trait for {}#{}", serviceId, longPollingOperation);
6587
opModel.setLongPolling(true);
6688
} else {
67-
log.warn("Did not find operation {}#{}", serviceId, longPollingOperation);
89+
throw new RuntimeException("Operation " + longPollingOperation + " not found for service " + serviceId);
6890
}
6991
}
7092
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.codegen.customization.processors;
17+
18+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
19+
20+
import java.util.Collections;
21+
import java.util.HashMap;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.stream.Stream;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.MethodSource;
28+
import software.amazon.awssdk.codegen.C2jModels;
29+
import software.amazon.awssdk.codegen.IntermediateModelBuilder;
30+
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
31+
import software.amazon.awssdk.codegen.model.intermediate.Protocol;
32+
import software.amazon.awssdk.codegen.model.service.ServiceMetadata;
33+
import software.amazon.awssdk.codegen.poet.ClientTestModels;
34+
35+
public class LongPollingOperationProcessTest {
36+
37+
@ParameterizedTest
38+
@MethodSource("nonJsonProtocols")
39+
void postprocess_serviceInMap_serviceNotJson_throws(Protocol protocol) {
40+
C2jModels c2jModels = ClientTestModels.awsJsonServiceC2jModels();
41+
42+
ServiceMetadata metadata = c2jModels.serviceModel().getMetadata();
43+
metadata.setProtocols(Collections.singletonList(protocol.getValue()));
44+
45+
IntermediateModel intermediateModel = new IntermediateModelBuilder(c2jModels).build();
46+
47+
Map<String, List<String>> serviceToOperations = new HashMap<>();
48+
serviceToOperations.put(metadata.getServiceId(), Collections.emptyList());
49+
LongPollingOperationProcessor processor = new LongPollingOperationProcessor(serviceToOperations);
50+
51+
assertThatThrownBy(() -> processor.postprocess(intermediateModel))
52+
.hasMessage("Currently only AWS-JSON services can use the longPoll trait");
53+
}
54+
55+
@Test
56+
void postprocess_operationNotFound_throws() {
57+
IntermediateModel intermediateModel = ClientTestModels.awsJsonServiceModels();
58+
59+
Map<String, List<String>> serviceToOperations = new HashMap<>();
60+
serviceToOperations.put(intermediateModel.getMetadata().getServiceId(), Collections.singletonList("SomeOperation"));
61+
LongPollingOperationProcessor processor = new LongPollingOperationProcessor(serviceToOperations);
62+
63+
assertThatThrownBy(() -> processor.postprocess(intermediateModel))
64+
.hasMessage("Operation SomeOperation not found for service Json Service");
65+
}
66+
67+
private static Stream<Protocol> nonJsonProtocols() {
68+
return Stream.of(Protocol.values()).filter(p -> p != Protocol.AWS_JSON);
69+
}
70+
}

0 commit comments

Comments
 (0)