|
11 | 11 | import static org.mockito.Mockito.when; |
12 | 12 |
|
13 | 13 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 14 | +import java.lang.reflect.Method; |
14 | 15 | import java.util.HashMap; |
| 16 | +import java.util.List; |
15 | 17 | import java.util.Map; |
16 | 18 | import one.x1f.sip.foundation.core.util.exception.SIPFrameworkException; |
17 | 19 | import one.x1f.sip.foundation.testkit.configurationproperties.models.EndpointProperties; |
18 | 20 | import one.x1f.sip.foundation.testkit.configurationproperties.models.MessageProperties; |
19 | 21 | import one.x1f.sip.foundation.testkit.workflow.givenphase.Mock; |
20 | 22 | import one.x1f.sip.foundation.testkit.workflow.whenphase.routeinvoker.impl.DirectRouteInvokerTest; |
21 | 23 | import org.apache.camel.*; |
| 24 | +import org.apache.camel.component.jackson.JacksonDataFormat; |
| 25 | +import org.apache.camel.model.*; |
| 26 | +import org.apache.camel.model.dataformat.JsonDataFormat; |
22 | 27 | import org.junit.jupiter.api.BeforeEach; |
23 | 28 | import org.junit.jupiter.api.Test; |
24 | 29 | import org.junit.jupiter.params.ParameterizedTest; |
@@ -170,4 +175,195 @@ void GIVEN_PersonJsonRequestModel_WHEN_unmarshallExchangeBodyFromJson_THEN_expec |
170 | 175 | }) |
171 | 176 | .isInstanceOf(SIPFrameworkException.class); |
172 | 177 | } |
| 178 | + |
| 179 | + @Test |
| 180 | + void |
| 181 | + GIVEN_choiceContainingJsonDslUnmarshal_WHEN_extractUnmarshalClass_THEN_returnsUnmarshalType() { |
| 182 | + JsonDataFormat jsonDf = mock(JsonDataFormat.class); |
| 183 | + when(jsonDf.getUnmarshalType()).thenReturn((Class) MyModel.class); |
| 184 | + |
| 185 | + UnmarshalDefinition unmarshalDef = mock(UnmarshalDefinition.class); |
| 186 | + when(unmarshalDef.getDataFormatType()).thenReturn(jsonDf); |
| 187 | + |
| 188 | + ChoiceDefinition choiceDef = mock(ChoiceDefinition.class); |
| 189 | + when(choiceDef.getOutputs()).thenReturn(List.<ProcessorDefinition<?>>of(unmarshalDef)); |
| 190 | + |
| 191 | + RouteDefinition routeDef = mock(RouteDefinition.class); |
| 192 | + when(routeDef.getOutputs()).thenReturn(List.<ProcessorDefinition<?>>of(choiceDef)); |
| 193 | + |
| 194 | + Class<?> result = TestKitHelper.extractUnmarshalClass(routeDef); |
| 195 | + |
| 196 | + assertThat(result).isEqualTo(MyModel.class); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + void |
| 201 | + GIVEN_choiceContainingRawJacksonDataFormatUnmarshal_WHEN_extractUnmarshalClass_THEN_returnsUnmarshalType() { |
| 202 | + JacksonDataFormat jacksonDf = mock(JacksonDataFormat.class); |
| 203 | + when(jacksonDf.getUnmarshalType()).thenReturn((Class) MyModel.class); |
| 204 | + |
| 205 | + DataFormatDefinition dfDef = mock(DataFormatDefinition.class); |
| 206 | + when(dfDef.getDataFormat()).thenReturn(jacksonDf); |
| 207 | + |
| 208 | + UnmarshalDefinition unmarshalDef = mock(UnmarshalDefinition.class); |
| 209 | + when(unmarshalDef.getDataFormatType()).thenReturn(dfDef); |
| 210 | + |
| 211 | + ChoiceDefinition choiceDef = mock(ChoiceDefinition.class); |
| 212 | + when(choiceDef.getOutputs()).thenReturn(List.<ProcessorDefinition<?>>of(unmarshalDef)); |
| 213 | + |
| 214 | + RouteDefinition routeDef = mock(RouteDefinition.class); |
| 215 | + when(routeDef.getOutputs()).thenReturn(List.<ProcessorDefinition<?>>of(choiceDef)); |
| 216 | + |
| 217 | + Class<?> result = TestKitHelper.extractUnmarshalClass(routeDef); |
| 218 | + |
| 219 | + assertThat(result).isEqualTo(MyModel.class); |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + void GIVEN_routeWithoutChoice_WHEN_extractUnmarshalClass_THEN_returnsNull() { |
| 224 | + LogDefinition logDef = mock(LogDefinition.class); |
| 225 | + |
| 226 | + RouteDefinition routeDef = mock(RouteDefinition.class); |
| 227 | + when(routeDef.getOutputs()).thenReturn(List.<ProcessorDefinition<?>>of(logDef)); |
| 228 | + |
| 229 | + Class<?> result = TestKitHelper.extractUnmarshalClass(routeDef); |
| 230 | + |
| 231 | + assertThat(result).isNull(); |
| 232 | + } |
| 233 | + |
| 234 | + @Test |
| 235 | + void GIVEN_choiceWithoutUnmarshal_WHEN_extractUnmarshalClass_THEN_returnsNull() { |
| 236 | + LogDefinition logDef = mock(LogDefinition.class); |
| 237 | + |
| 238 | + ChoiceDefinition choiceDef = mock(ChoiceDefinition.class); |
| 239 | + when(choiceDef.getOutputs()).thenReturn(List.<ProcessorDefinition<?>>of(logDef)); |
| 240 | + |
| 241 | + RouteDefinition routeDef = mock(RouteDefinition.class); |
| 242 | + when(routeDef.getOutputs()).thenReturn(List.<ProcessorDefinition<?>>of(choiceDef)); |
| 243 | + |
| 244 | + Class<?> result = TestKitHelper.extractUnmarshalClass(routeDef); |
| 245 | + |
| 246 | + assertThat(result).isNull(); |
| 247 | + } |
| 248 | + |
| 249 | + @Test |
| 250 | + void GIVEN_outputsContainingUnmarshalDefinition_WHEN_findUnmarshalType_THEN_returnsUnmarshalType() |
| 251 | + throws Exception { |
| 252 | + JsonDataFormat jsonDf = mock(JsonDataFormat.class); |
| 253 | + when(jsonDf.getUnmarshalType()).thenReturn((Class) MyModel.class); |
| 254 | + |
| 255 | + UnmarshalDefinition unmarshalDef = mock(UnmarshalDefinition.class); |
| 256 | + when(unmarshalDef.getDataFormatType()).thenReturn(jsonDf); |
| 257 | + |
| 258 | + List<ProcessorDefinition<?>> outputs = List.of(unmarshalDef); |
| 259 | + |
| 260 | + Class<?> result = invokeFindUnmarshalType(outputs); |
| 261 | + |
| 262 | + assertThat(result).isEqualTo(MyModel.class); |
| 263 | + } |
| 264 | + |
| 265 | + @Test |
| 266 | + void GIVEN_emptyOutputs_WHEN_findUnmarshalType_THEN_returnsNull() throws Exception { |
| 267 | + Class<?> result = invokeFindUnmarshalType(List.of()); |
| 268 | + |
| 269 | + assertThat(result).isNull(); |
| 270 | + } |
| 271 | + |
| 272 | + @Test |
| 273 | + void GIVEN_outputsWithoutUnmarshalDefinition_WHEN_findUnmarshalType_THEN_returnsNull() |
| 274 | + throws Exception { |
| 275 | + LogDefinition logDef = mock(LogDefinition.class); |
| 276 | + |
| 277 | + List<ProcessorDefinition<?>> outputs = List.of(logDef); |
| 278 | + |
| 279 | + Class<?> result = invokeFindUnmarshalType(outputs); |
| 280 | + |
| 281 | + assertThat(result).isNull(); |
| 282 | + } |
| 283 | + |
| 284 | + @Test |
| 285 | + void GIVEN_jsonDataFormatModel_WHEN_extractUnmarshalType_THEN_returnsUnmarshalType() |
| 286 | + throws Exception { |
| 287 | + JsonDataFormat jsonDf = mock(JsonDataFormat.class); |
| 288 | + when(jsonDf.getUnmarshalType()).thenReturn((Class) MyModel.class); |
| 289 | + |
| 290 | + Class<?> result = invokeExtractUnmarshalType(jsonDf); |
| 291 | + |
| 292 | + assertThat(result).isEqualTo(MyModel.class); |
| 293 | + } |
| 294 | + |
| 295 | + @Test |
| 296 | + void |
| 297 | + GIVEN_dataFormatDefinitionWrappingJacksonDataFormat_WHEN_extractUnmarshalType_THEN_returnsUnmarshalType() |
| 298 | + throws Exception { |
| 299 | + JacksonDataFormat jacksonDf = mock(JacksonDataFormat.class); |
| 300 | + when(jacksonDf.getUnmarshalType()).thenReturn((Class) MyModel.class); |
| 301 | + |
| 302 | + DataFormatDefinition dfDef = mock(DataFormatDefinition.class); |
| 303 | + when(dfDef.getDataFormat()).thenReturn(jacksonDf); |
| 304 | + |
| 305 | + Class<?> result = invokeExtractUnmarshalType(dfDef); |
| 306 | + |
| 307 | + assertThat(result).isEqualTo(MyModel.class); |
| 308 | + } |
| 309 | + |
| 310 | + @Test |
| 311 | + void |
| 312 | + GIVEN_arrayUnmarshalType_WHEN_extractUnmarshalType_THEN_returnsArrayClassWithMatchingComponentType() |
| 313 | + throws Exception { |
| 314 | + JacksonDataFormat jacksonDf = mock(JacksonDataFormat.class); |
| 315 | + when(jacksonDf.getUnmarshalType()).thenReturn((Class) MyModel[].class); |
| 316 | + |
| 317 | + DataFormatDefinition dfDef = mock(DataFormatDefinition.class); |
| 318 | + when(dfDef.getDataFormat()).thenReturn(jacksonDf); |
| 319 | + |
| 320 | + Class<?> result = invokeExtractUnmarshalType(dfDef); |
| 321 | + |
| 322 | + assertThat(result).isEqualTo(MyModel[].class); |
| 323 | + assertThat(result.getComponentType()).isEqualTo(MyModel.class); |
| 324 | + } |
| 325 | + |
| 326 | + @Test |
| 327 | + void GIVEN_nullDataFormatDefinition_WHEN_extractUnmarshalType_THEN_returnsNull() |
| 328 | + throws Exception { |
| 329 | + Class<?> result = invokeExtractUnmarshalType(null); |
| 330 | + |
| 331 | + assertThat(result).isNull(); |
| 332 | + } |
| 333 | + |
| 334 | + @Test |
| 335 | + void GIVEN_unrelatedDataFormatDefinition_WHEN_extractUnmarshalType_THEN_returnsNull() |
| 336 | + throws Exception { |
| 337 | + DataFormatDefinition dfDef = mock(DataFormatDefinition.class); |
| 338 | + when(dfDef.getDataFormat()).thenReturn(null); |
| 339 | + |
| 340 | + Class<?> result = invokeExtractUnmarshalType(dfDef); |
| 341 | + |
| 342 | + assertThat(result).isNull(); |
| 343 | + } |
| 344 | + |
| 345 | + private Class<?> invokeFindUnmarshalType(List<ProcessorDefinition<?>> outputs) throws Exception { |
| 346 | + Method m = TestKitHelper.class.getDeclaredMethod("findUnmarshalType", List.class); |
| 347 | + m.setAccessible(true); |
| 348 | + return (Class<?>) m.invoke(null, outputs); |
| 349 | + } |
| 350 | + |
| 351 | + private Class<?> invokeExtractUnmarshalType(DataFormatDefinition dfDef) throws Exception { |
| 352 | + Method m = |
| 353 | + TestKitHelper.class.getDeclaredMethod("extractUnmarshalType", DataFormatDefinition.class); |
| 354 | + m.setAccessible(true); |
| 355 | + return (Class<?>) m.invoke(null, dfDef); |
| 356 | + } |
| 357 | + |
| 358 | + static class MyModel { |
| 359 | + private String field; |
| 360 | + |
| 361 | + public String getField() { |
| 362 | + return field; |
| 363 | + } |
| 364 | + |
| 365 | + public void setField(String field) { |
| 366 | + this.field = field; |
| 367 | + } |
| 368 | + } |
173 | 369 | } |
0 commit comments