14
14
import static org .mockito .Mockito .RETURNS_DEEP_STUBS ;
15
15
import static org .mockito .Mockito .doReturn ;
16
16
import static org .mockito .Mockito .mock ;
17
+ import static org .mockito .Mockito .never ;
17
18
import static org .mockito .Mockito .spy ;
18
19
import static org .mockito .Mockito .verify ;
19
20
import static org .mockito .Mockito .when ;
32
33
import io .airbyte .api .model .SourceDefinitionIdRequestBody ;
33
34
import io .airbyte .api .model .SourceDefinitionSpecificationRead ;
34
35
import io .airbyte .api .model .SourceDiscoverSchemaRead ;
36
+ import io .airbyte .api .model .SourceDiscoverSchemaRequestBody ;
35
37
import io .airbyte .api .model .SourceIdRequestBody ;
36
38
import io .airbyte .api .model .SourceUpdate ;
37
39
import io .airbyte .commons .docker .DockerUtils ;
38
40
import io .airbyte .commons .enums .Enums ;
39
41
import io .airbyte .commons .features .FeatureFlags ;
40
42
import io .airbyte .commons .json .Jsons ;
41
43
import io .airbyte .commons .lang .Exceptions ;
44
+ import io .airbyte .config .ActorCatalog ;
42
45
import io .airbyte .config .ActorDefinitionResourceRequirements ;
43
46
import io .airbyte .config .Configs .WorkerEnvironment ;
44
47
import io .airbyte .config .DestinationConnection ;
@@ -381,12 +384,14 @@ void testCheckDestinationConnectionFromUpdate() throws IOException, JsonValidati
381
384
@ Test
382
385
void testDiscoverSchemaForSourceFromSourceId () throws IOException , JsonValidationException , ConfigNotFoundException {
383
386
final SourceConnection source = SourceHelpers .generateSource (UUID .randomUUID ());
384
- final SourceIdRequestBody request = new SourceIdRequestBody ().sourceId (source .getSourceId ());
387
+ final SourceDiscoverSchemaRequestBody request = new SourceDiscoverSchemaRequestBody ().sourceId (source .getSourceId ());
385
388
386
389
final SynchronousResponse <AirbyteCatalog > discoverResponse = (SynchronousResponse <AirbyteCatalog >) jobResponse ;
387
390
final SynchronousJobMetadata metadata = mock (SynchronousJobMetadata .class );
388
391
when (discoverResponse .isSuccess ()).thenReturn (true );
389
- when (discoverResponse .getOutput ()).thenReturn (CatalogHelpers .createAirbyteCatalog ("shoes" , Field .of ("sku" , JsonSchemaType .STRING )));
392
+ final AirbyteCatalog airbyteCatalog = CatalogHelpers .createAirbyteCatalog ("shoes" ,
393
+ Field .of ("sku" , JsonSchemaType .STRING ));
394
+ when (discoverResponse .getOutput ()).thenReturn (airbyteCatalog );
390
395
when (discoverResponse .getMetadata ()).thenReturn (metadata );
391
396
when (metadata .isSucceeded ()).thenReturn (true );
392
397
@@ -396,6 +401,7 @@ void testDiscoverSchemaForSourceFromSourceId() throws IOException, JsonValidatio
396
401
.withDockerImageTag (SOURCE_DOCKER_TAG )
397
402
.withSourceDefinitionId (source .getSourceDefinitionId ()));
398
403
when (configRepository .getSourceConnection (source .getSourceId ())).thenReturn (source );
404
+ when (configRepository .getSourceCatalog (any (), any (), any ())).thenReturn (Optional .empty ());
399
405
when (synchronousSchedulerClient .createDiscoverSchemaJob (source , SOURCE_DOCKER_IMAGE ))
400
406
.thenReturn (discoverResponse );
401
407
@@ -405,13 +411,93 @@ void testDiscoverSchemaForSourceFromSourceId() throws IOException, JsonValidatio
405
411
assertNotNull (actual .getJobInfo ());
406
412
assertTrue (actual .getJobInfo ().getSucceeded ());
407
413
verify (configRepository ).getSourceConnection (source .getSourceId ());
414
+ verify (configRepository ).getSourceCatalog (eq (request .getSourceId ()), any (), eq (SOURCE_DOCKER_TAG ));
415
+ verify (configRepository ).writeActorCatalogFetchEvent (eq (airbyteCatalog ), eq (source .getSourceId ()), any (), eq (SOURCE_DOCKER_TAG ));
416
+ verify (synchronousSchedulerClient ).createDiscoverSchemaJob (source , SOURCE_DOCKER_IMAGE );
417
+ }
418
+
419
+ @ Test
420
+ void testDiscoverSchemaForSourceFromSourceIdCachedCatalog () throws IOException , JsonValidationException , ConfigNotFoundException {
421
+ final SourceConnection source = SourceHelpers .generateSource (UUID .randomUUID ());
422
+ final SourceDiscoverSchemaRequestBody request = new SourceDiscoverSchemaRequestBody ().sourceId (source .getSourceId ());
423
+
424
+ final SynchronousResponse <AirbyteCatalog > discoverResponse = (SynchronousResponse <AirbyteCatalog >) jobResponse ;
425
+ final SynchronousJobMetadata metadata = mock (SynchronousJobMetadata .class );
426
+ when (discoverResponse .isSuccess ()).thenReturn (true );
427
+ final AirbyteCatalog airbyteCatalog = CatalogHelpers .createAirbyteCatalog ("shoes" ,
428
+ Field .of ("sku" , JsonSchemaType .STRING ));
429
+ when (discoverResponse .getOutput ()).thenReturn (airbyteCatalog );
430
+ when (discoverResponse .getMetadata ()).thenReturn (metadata );
431
+ when (metadata .isSucceeded ()).thenReturn (true );
432
+
433
+ when (configRepository .getStandardSourceDefinition (source .getSourceDefinitionId ()))
434
+ .thenReturn (new StandardSourceDefinition ()
435
+ .withDockerRepository (SOURCE_DOCKER_REPO )
436
+ .withDockerImageTag (SOURCE_DOCKER_TAG )
437
+ .withSourceDefinitionId (source .getSourceDefinitionId ()));
438
+ when (configRepository .getSourceConnection (source .getSourceId ())).thenReturn (source );
439
+ final ActorCatalog actorCatalog = new ActorCatalog ()
440
+ .withCatalog (Jsons .jsonNode (airbyteCatalog ))
441
+ .withCatalogHash ("" )
442
+ .withId (UUID .randomUUID ());
443
+ when (configRepository .getSourceCatalog (any (), any (), any ())).thenReturn (Optional .of (actorCatalog ));
444
+ when (synchronousSchedulerClient .createDiscoverSchemaJob (source , SOURCE_DOCKER_IMAGE ))
445
+ .thenReturn (discoverResponse );
446
+
447
+ final SourceDiscoverSchemaRead actual = schedulerHandler .discoverSchemaForSourceFromSourceId (request );
448
+
449
+ assertNotNull (actual .getCatalog ());
450
+ assertNotNull (actual .getJobInfo ());
451
+ assertTrue (actual .getJobInfo ().getSucceeded ());
452
+ verify (configRepository ).getSourceConnection (source .getSourceId ());
453
+ verify (configRepository ).getSourceCatalog (eq (request .getSourceId ()), any (), any ());
454
+ verify (configRepository , never ()).writeActorCatalogFetchEvent (any (), any (), any (), any ());
455
+ verify (synchronousSchedulerClient , never ()).createDiscoverSchemaJob (source , SOURCE_DOCKER_IMAGE );
456
+ }
457
+
458
+ @ Test
459
+ void testDiscoverSchemaForSourceFromSourceIdDisableCache () throws IOException , JsonValidationException , ConfigNotFoundException {
460
+ final SourceConnection source = SourceHelpers .generateSource (UUID .randomUUID ());
461
+ final SourceDiscoverSchemaRequestBody request = new SourceDiscoverSchemaRequestBody ().sourceId (source .getSourceId ()).disableCache (true );
462
+
463
+ final SynchronousResponse <AirbyteCatalog > discoverResponse = (SynchronousResponse <AirbyteCatalog >) jobResponse ;
464
+ final SynchronousJobMetadata metadata = mock (SynchronousJobMetadata .class );
465
+ when (discoverResponse .isSuccess ()).thenReturn (true );
466
+ final AirbyteCatalog airbyteCatalog = CatalogHelpers .createAirbyteCatalog ("shoes" ,
467
+ Field .of ("sku" , JsonSchemaType .STRING ));
468
+ when (discoverResponse .getOutput ()).thenReturn (airbyteCatalog );
469
+ when (discoverResponse .getMetadata ()).thenReturn (metadata );
470
+ when (metadata .isSucceeded ()).thenReturn (true );
471
+
472
+ when (configRepository .getStandardSourceDefinition (source .getSourceDefinitionId ()))
473
+ .thenReturn (new StandardSourceDefinition ()
474
+ .withDockerRepository (SOURCE_DOCKER_REPO )
475
+ .withDockerImageTag (SOURCE_DOCKER_TAG )
476
+ .withSourceDefinitionId (source .getSourceDefinitionId ()));
477
+ when (configRepository .getSourceConnection (source .getSourceId ())).thenReturn (source );
478
+ final ActorCatalog actorCatalog = new ActorCatalog ()
479
+ .withCatalog (Jsons .jsonNode (airbyteCatalog ))
480
+ .withCatalogHash ("" )
481
+ .withId (UUID .randomUUID ());
482
+ when (configRepository .getSourceCatalog (any (), any (), any ())).thenReturn (Optional .of (actorCatalog ));
483
+ when (synchronousSchedulerClient .createDiscoverSchemaJob (source , SOURCE_DOCKER_IMAGE ))
484
+ .thenReturn (discoverResponse );
485
+
486
+ final SourceDiscoverSchemaRead actual = schedulerHandler .discoverSchemaForSourceFromSourceId (request );
487
+
488
+ assertNotNull (actual .getCatalog ());
489
+ assertNotNull (actual .getJobInfo ());
490
+ assertTrue (actual .getJobInfo ().getSucceeded ());
491
+ verify (configRepository ).getSourceConnection (source .getSourceId ());
492
+ verify (configRepository ).getSourceCatalog (eq (request .getSourceId ()), any (), any ());
493
+ verify (configRepository ).writeActorCatalogFetchEvent (any (), any (), any (), any ());
408
494
verify (synchronousSchedulerClient ).createDiscoverSchemaJob (source , SOURCE_DOCKER_IMAGE );
409
495
}
410
496
411
497
@ Test
412
498
void testDiscoverSchemaForSourceFromSourceIdFailed () throws IOException , JsonValidationException , ConfigNotFoundException {
413
499
final SourceConnection source = SourceHelpers .generateSource (UUID .randomUUID ());
414
- final SourceIdRequestBody request = new SourceIdRequestBody ().sourceId (source .getSourceId ());
500
+ final SourceDiscoverSchemaRequestBody request = new SourceDiscoverSchemaRequestBody ().sourceId (source .getSourceId ());
415
501
416
502
when (configRepository .getStandardSourceDefinition (source .getSourceDefinitionId ()))
417
503
.thenReturn (new StandardSourceDefinition ()
0 commit comments