FullUpdateModifier { static void applyEvent(Map<String, Collection<String>> image, NotificationEvent event, MessageDeserializer deserializer) { EventMessage.EventType eventType = EventMessage.EventType.valueOf(event.getEventType()); switch (eventType) { case CREATE_DATABASE: createDatabase(image, event, deserializer); break; case DROP_DATABASE: dropDatabase(image, event, deserializer); break; case CREATE_TABLE: createTable(image, event, deserializer); break; case DROP_TABLE: dropTable(image, event, deserializer); break; case ALTER_TABLE: alterTable(image, event, deserializer); break; case ADD_PARTITION: addPartition(image, event, deserializer); break; case DROP_PARTITION: dropPartition(image, event, deserializer); break; case ALTER_PARTITION: alterPartition(image, event, deserializer); break; default: LOGGER.error("Notification with ID:{} has invalid event type: {}", event.getEventId(), event.getEventType()); break; } } private  FullUpdateModifier();   }
@Test public void testDropDatabaseWrongLocation() throws Exception { Map<String, Collection<String>> update = new HashMap<>(); update.put(DB.toLowerCase(), Collections.singleton(PATH)); NotificationEvent event = new NotificationEvent(0, 0, DROP_DATABASE.toString(), ""); MessageDeserializer deserializer = Mockito.mock(SentryJSONMessageDeserializer.class); SentryJSONDropDatabaseMessage message = new SentryJSONDropDatabaseMessage(SERVER, PRINCIPAL, DB, 0L, "hdfs: Mockito.when(deserializer.getDropDatabaseMessage("")).thenReturn(message); FullUpdateModifier.applyEvent(update, event, deserializer); Map<String, Set<String>> expected = new HashMap<>(); expected.put(DB.toLowerCase(), Collections.singleton(PATH)); assertEquals(expected, update); }
@Test public void testDropDatabaseWithTables() throws Exception { Map<String, Collection<String>> update = new HashMap<>(); update.put(DB.toLowerCase(), Collections.singleton(PATH)); update.put(AUTH, Collections.singleton(PATH)); update.put("unrelated", Collections.singleton(PATH)); NotificationEvent event = new NotificationEvent(0, 0, DROP_DATABASE.toString(), ""); MessageDeserializer deserializer = Mockito.mock(SentryJSONMessageDeserializer.class); SentryJSONDropDatabaseMessage message = new SentryJSONDropDatabaseMessage(SERVER, PRINCIPAL, DB, 0L, LOCATION); Mockito.when(deserializer.getDropDatabaseMessage("")).thenReturn(message); FullUpdateModifier.applyEvent(update, event, deserializer); Map<String, Set<String>> expected = new HashMap<>(); expected.put("unrelated", Collections.singleton(PATH)); assertEquals(expected, update); }
@Test public void testDropTableWrongLocation() throws Exception { Map<String, Collection<String>> update = new HashMap<>(); update.put(AUTH, Collections.singleton(PATH)); NotificationEvent event = new NotificationEvent(0, 0, DROP_TABLE.toString(), ""); MessageDeserializer deserializer = Mockito.mock(SentryJSONMessageDeserializer.class); SentryJSONDropTableMessage message = new SentryJSONDropTableMessage(SERVER, PRINCIPAL, DB, TABLE, 0L, "hdfs: Mockito.when(deserializer.getDropTableMessage("")).thenReturn(message); FullUpdateModifier.applyEvent(update, event, deserializer); assertEquals(Collections.singleton(PATH), update.get(AUTH)); assertEquals(1, update.size()); }
@Test public void testDropPartitions() throws Exception { String partPath = "hello/world"; String partLocation = uri(partPath); Map<String, Collection<String>> update = new HashMap<>(); Set<String> locations = new HashSet<>(); locations.add(PATH); locations.add(partPath); update.put(AUTH, locations); NotificationEvent event = new NotificationEvent(0, 0, DROP_PARTITION.toString(), ""); MessageDeserializer deserializer = Mockito.mock(SentryJSONMessageDeserializer.class); SentryJSONDropPartitionMessage message = new SentryJSONDropPartitionMessage(SERVER, PRINCIPAL, TABLE_OBJ, Collections.<Map<String,String>>emptyList(), 0L, Collections.singletonList(partLocation)); Mockito.when(deserializer.getDropPartitionMessage("")).thenReturn(message); FullUpdateModifier.applyEvent(update, event, deserializer); assertEquals(Collections.singleton(PATH), update.get(AUTH)); }
@Test public void testAlterTableChangeDbNameNoTables() throws Exception { Map<String, Collection<String>> update = new HashMap<>(); update.put(DB.toLowerCase(), Collections.singleton(PATH)); String newDbName = "Db2"; NotificationEvent event = new NotificationEvent(0, 0, ALTER_TABLE.toString(), ""); event.setDbName(newDbName); event.setTableName(TABLE); MessageDeserializer deserializer = Mockito.mock(SentryJSONMessageDeserializer.class); SentryJSONAlterTableMessage message = new SentryJSONAlterTableMessage(SERVER, PRINCIPAL, TABLE_OBJ, TABLE_OBJ, 0L); Mockito.when(deserializer.getAlterTableMessage("")).thenReturn(message); FullUpdateModifier.applyEvent(update, event, deserializer); assertEquals(Collections.singleton(PATH), update.get(newDbName.toLowerCase())); assertFalse(update.containsKey(DB.toLowerCase())); }
@Test public void testAlterTableChangeDbNameWithTables() throws Exception { Map<String, Collection<String>> update = new HashMap<>(); update.put(DB.toLowerCase(), Collections.singleton(PATH)); Set<String> locations = new HashSet<>(1); locations.add(PATH); update.put(AUTH, locations); String newDbName = "Db2"; String newAuth = newDbName.toLowerCase() + "." + TABLE.toLowerCase(); NotificationEvent event = new NotificationEvent(0, 0, ALTER_TABLE.toString(), ""); event.setDbName(newDbName); event.setTableName(TABLE); MessageDeserializer deserializer = Mockito.mock(SentryJSONMessageDeserializer.class); SentryJSONAlterTableMessage message = new SentryJSONAlterTableMessage(SERVER, PRINCIPAL, TABLE_OBJ, TABLE_OBJ, 0L); Mockito.when(deserializer.getAlterTableMessage("")).thenReturn(message); FullUpdateModifier.applyEvent(update, event, deserializer); Map<String, Set<String>> expected = new HashMap<>(2); expected.put(newDbName.toLowerCase(), Collections.singleton(PATH)); expected.put(newAuth, Collections.singleton(PATH)); assertEquals(expected, update); }
@Test public void testAlterTableChangeTableName() throws Exception { Map<String, Collection<String>> update = new HashMap<>(); update.put(DB.toLowerCase(), Collections.singleton(PATH)); Set<String> locations = new HashSet<>(1); locations.add(PATH); update.put(AUTH, locations); String newTableName = "Table2"; String newAuth = DB.toLowerCase() + "." + newTableName.toLowerCase(); NotificationEvent event = new NotificationEvent(0, 0, ALTER_TABLE.toString(), ""); event.setDbName(DB); event.setTableName(newTableName); MessageDeserializer deserializer = Mockito.mock(SentryJSONMessageDeserializer.class); SentryJSONAlterTableMessage message = new SentryJSONAlterTableMessage(SERVER, PRINCIPAL, TABLE_OBJ, TABLE_OBJ, 0L); Mockito.when(deserializer.getAlterTableMessage("")).thenReturn(message); FullUpdateModifier.applyEvent(update, event, deserializer); Map<String, Set<String>> expected = new HashMap<>(2); expected.put(DB.toLowerCase(), Collections.singleton(PATH)); expected.put(newAuth, Collections.singleton(PATH)); assertEquals(expected, update); }
@Test public void testAlterTableChangeLocation() throws Exception { Map<String, Collection<String>> update = new HashMap<>(); update.put(DB.toLowerCase(), Collections.singleton(PATH)); Set<String> locations = new HashSet<>(1); locations.add(PATH); update.put(AUTH, locations); NotificationEvent event = new NotificationEvent(0, 0, ALTER_TABLE.toString(), ""); event.setDbName(DB); event.setTableName(TABLE); String newPath = "hello/world"; String newLocation = uri(newPath); MessageDeserializer deserializer = Mockito.mock(SentryJSONMessageDeserializer.class); Table tableWithNewLocation = buildTable(DB, TABLE, newLocation); SentryJSONAlterTableMessage message = new SentryJSONAlterTableMessage(SERVER, PRINCIPAL, TABLE_OBJ, tableWithNewLocation, 0L); Mockito.when(deserializer.getAlterTableMessage("")).thenReturn(message); FullUpdateModifier.applyEvent(update, event, deserializer); Map<String, Set<String>> expected = new HashMap<>(2); expected.put(DB.toLowerCase(), Collections.singleton(PATH)); expected.put(AUTH.toLowerCase(), Collections.singleton(newPath)); assertEquals(expected, update); }