HiveTableAction { public List<PatchOperation> checkAndApply(HiveRoad road) { List<PatchOperation> patches = new ArrayList<>(); String name = road.getName(); SchemaVersion latestSchemaVersion = road .getSchemas() .entrySet() .stream() .map(Entry::getValue) .filter(s -> !s.isDeleted()) .max(Ordering.natural().onResultOf(SchemaVersion::getVersion)) .orElseThrow(() -> new NoActiveSchemaException()); Schema schema = latestSchemaVersion.getSchema(); int version = latestSchemaVersion.getVersion(); if (!tableExists.test(name)) { patches.addAll(createTable(name, schema, version)); } else if (schemaVersionChanged.test(name, version)) { patches.addAll(updateSchema(name, schema, version)); } else { log.debug("Nothing to do for road {}", road.getName()); } return patches; } @Autowired  HiveTableAction(
      HiveTableManager hiveTableManager,
      LoadingCacheFactory loadingCacheFactory,
      HiveNotificationHandler hiveNotificationHandler,
      @Value("${hive.grantPublicSelect:false}") boolean grantPublicSelect); List<PatchOperation> checkAndApply(HiveRoad road);  }
@Test public void schemaChanged() { when(hiveTableManager.tableExists(ROAD_NAME)).thenReturn(true); when(hiveTableManager.getSchemaVersion(ROAD_NAME)).thenReturn(1); HiveRoad road = HiveRoad .builder() .name(ROAD_NAME) .schemas(schemas(schemaVersion1, schemaVersion2)) .destinations(destinations) .status(status) .build(); List<PatchOperation> result = underTest.checkAndApply(road); verify(hiveTableManager).alterTable(ROAD_NAME, schema, 2); List<PatchOperation> patches = HiveStatusPatchBuilder.patchBuilder().set(HIVE_SCHEMA_VERSION, 2).build(); assertThat(result, is(patches)); verifyNoMoreInteractions(notificationHandler); }
@Test public void tableExistsAndSchemaNotChanged() { when(hiveTableManager.tableExists(ROAD_NAME)).thenReturn(true); when(hiveTableManager.getSchemaVersion(ROAD_NAME)).thenReturn(1); HiveRoad road = HiveRoad .builder() .name(ROAD_NAME) .schemas(schemas(schemaVersion1)) .destinations(destinations) .status(status) .build(); List<PatchOperation> result = underTest.checkAndApply(road); List<PatchOperation> patches = Collections.emptyList(); assertThat(result, is(patches)); verifyNoMoreInteractions(notificationHandler); }
@Test(expected = NoActiveSchemaException.class) public void noSchema() { HiveRoad road = HiveRoad .builder() .name(ROAD_NAME) .schemas(schemas(schemaVersion1Deleted)) .destinations(destinations) .status(status) .build(); underTest.checkAndApply(road); verifyNoMoreInteractions(notificationHandler); }