|
1 | 1 | package software.amazon.smithy.typescript.codegen;
|
2 | 2 |
|
| 3 | +import java.util.LinkedHashSet; |
| 4 | +import java.util.List; |
| 5 | +import java.util.stream.Stream; |
3 | 6 | import org.junit.jupiter.api.Test;
|
| 7 | +import org.junit.jupiter.api.extension.ExtendWith; |
4 | 8 | import org.junit.jupiter.params.ParameterizedTest;
|
5 | 9 | import org.junit.jupiter.params.provider.Arguments;
|
6 | 10 | import org.junit.jupiter.params.provider.MethodSource;
|
| 11 | +import org.mockito.Mock; |
| 12 | +import org.mockito.junit.jupiter.MockitoExtension; |
7 | 13 | import software.amazon.smithy.model.Model;
|
| 14 | +import software.amazon.smithy.model.knowledge.ServiceIndex; |
8 | 15 | import software.amazon.smithy.model.node.Node;
|
9 | 16 | import software.amazon.smithy.model.node.ObjectNode;
|
| 17 | +import software.amazon.smithy.model.shapes.ServiceShape; |
10 | 18 | import software.amazon.smithy.model.shapes.ShapeId;
|
11 |
| - |
12 |
| -import java.util.stream.Stream; |
| 19 | +import software.amazon.smithy.typescript.codegen.protocols.ProtocolPriority; |
| 20 | +import software.amazon.smithy.utils.MapUtils; |
13 | 21 |
|
14 | 22 | import static org.hamcrest.MatcherAssert.assertThat;
|
15 | 23 | import static org.hamcrest.Matchers.equalTo;
|
16 | 24 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
| 25 | +import static org.mockito.ArgumentMatchers.any; |
| 26 | +import static org.mockito.Mockito.when; |
17 | 27 |
|
| 28 | +@ExtendWith(MockitoExtension.class) |
18 | 29 | public class TypeScriptSettingsTest {
|
19 | 30 |
|
20 | 31 | @Test
|
@@ -87,6 +98,121 @@ private static Stream<Arguments> providePackageDescriptionTestCases() {
|
87 | 98 | );
|
88 | 99 | }
|
89 | 100 |
|
| 101 | + @Test |
| 102 | + public void resolveServiceProtocol(@Mock Model model, |
| 103 | + @Mock ServiceShape service, |
| 104 | + @Mock ServiceIndex serviceIndex) { |
| 105 | + TypeScriptSettings subject = new TypeScriptSettings(); |
| 106 | + |
| 107 | + // note: these are mock protocol names. |
| 108 | + ShapeId rpcv2Cbor = ShapeId.from("namespace#rpcv2Cbor"); |
| 109 | + ShapeId json1_0 = ShapeId.from("namespace#json1_0"); |
| 110 | + ShapeId json1_1 = ShapeId.from("namespace#json1_1"); |
| 111 | + ShapeId restJson1 = ShapeId.from("namespace#restJson1"); |
| 112 | + ShapeId restXml = ShapeId.from("namespace#restXml"); |
| 113 | + ShapeId query = ShapeId.from("namespace#query"); |
| 114 | + ShapeId serviceQuery = ShapeId.from("namespace#serviceQuery"); |
| 115 | + |
| 116 | + when(model.getKnowledge(any(), any())).thenReturn(serviceIndex); |
| 117 | + ShapeId serviceShapeId = ShapeId.from("namespace#Service"); |
| 118 | + when(service.toShapeId()).thenReturn(serviceShapeId); |
| 119 | + |
| 120 | + LinkedHashSet<ShapeId> protocolShapeIds = new LinkedHashSet<>( |
| 121 | + List.of( |
| 122 | + json1_0, json1_1, restJson1, rpcv2Cbor, restXml, query, serviceQuery |
| 123 | + ) |
| 124 | + ); |
| 125 | + |
| 126 | + { |
| 127 | + // spec case 1. |
| 128 | + when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of( |
| 129 | + rpcv2Cbor, null, |
| 130 | + json1_0, null |
| 131 | + )); |
| 132 | + ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds); |
| 133 | + // Note: JS customization JSON higher default priority than CBOR. |
| 134 | + assertEquals(json1_0, protocol); |
| 135 | + } |
| 136 | + |
| 137 | + { |
| 138 | + // spec case 2. |
| 139 | + when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of( |
| 140 | + rpcv2Cbor, null |
| 141 | + )); |
| 142 | + ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds); |
| 143 | + assertEquals(rpcv2Cbor, protocol); |
| 144 | + } |
| 145 | + |
| 146 | + { |
| 147 | + // spec case 3. |
| 148 | + when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of( |
| 149 | + rpcv2Cbor, null, |
| 150 | + json1_0, null, |
| 151 | + query, null |
| 152 | + )); |
| 153 | + ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds); |
| 154 | + // Note: JS customization JSON higher default priority than CBOR. |
| 155 | + assertEquals(json1_0, protocol); |
| 156 | + } |
| 157 | + |
| 158 | + { |
| 159 | + // spec case 4. |
| 160 | + when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of( |
| 161 | + json1_0, null, |
| 162 | + query, null |
| 163 | + )); |
| 164 | + ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds); |
| 165 | + assertEquals(json1_0, protocol); |
| 166 | + } |
| 167 | + |
| 168 | + { |
| 169 | + // spec case 5. |
| 170 | + when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of( |
| 171 | + query, null |
| 172 | + )); |
| 173 | + ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds); |
| 174 | + assertEquals(query, protocol); |
| 175 | + } |
| 176 | + |
| 177 | + { |
| 178 | + // service override, non-spec |
| 179 | + when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of( |
| 180 | + json1_0, null, |
| 181 | + json1_1, null, |
| 182 | + restJson1, null, |
| 183 | + rpcv2Cbor, null, |
| 184 | + restXml, null, |
| 185 | + query, null, |
| 186 | + serviceQuery, null |
| 187 | + )); |
| 188 | + ProtocolPriority.setProtocolPriority(serviceShapeId, List.of( |
| 189 | + serviceQuery, rpcv2Cbor, json1_1, restJson1, restXml, query |
| 190 | + )); |
| 191 | + ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds); |
| 192 | + ProtocolPriority.deleteProtocolPriority(serviceShapeId); |
| 193 | + assertEquals(serviceQuery, protocol); |
| 194 | + } |
| 195 | + |
| 196 | + { |
| 197 | + // global default override |
| 198 | + when(serviceIndex.getProtocols(service)).thenReturn(MapUtils.of( |
| 199 | + json1_0, null, |
| 200 | + json1_1, null, |
| 201 | + restJson1, null, |
| 202 | + rpcv2Cbor, null, |
| 203 | + restXml, null, |
| 204 | + query, null, |
| 205 | + serviceQuery, null |
| 206 | + )); |
| 207 | + ProtocolPriority.setCustomDefaultProtocolPriority(List.of( |
| 208 | + rpcv2Cbor, json1_1, restJson1, restXml, query |
| 209 | + )); |
| 210 | + ShapeId protocol = subject.resolveServiceProtocol(model, service, protocolShapeIds); |
| 211 | + ProtocolPriority.deleteCustomDefaultProtocolPriority(); |
| 212 | + assertEquals(rpcv2Cbor, protocol); |
| 213 | + } |
| 214 | + } |
| 215 | + |
90 | 216 | @Test
|
91 | 217 | public void resolvesSupportProtocols() {
|
92 | 218 | // TODO
|
|
0 commit comments