### Question:
ApiaryReadOnlyAuthPreEventListener extends MetaStorePreEventListener { @Override public void onEvent(PreEventContext context) throws MetaException, NoSuchObjectException, InvalidOperationException { switch (context.getEventType()) { case READ_TABLE: Table table = ((PreReadTableEvent) context).getTable(); applyWhitelist(table.getDbName()); break; case READ_DATABASE: Database db = ((PreReadDatabaseEvent) context).getDatabase(); applyWhitelist(db.getName()); break; default: throw new InvalidOperationException(context.getEventType() + " is disabled from read-only metastores."); } } ApiaryReadOnlyAuthPreEventListener(Configuration config); @Override void onEvent(PreEventContext context);  }### Answer:
@Test public void onReadTableEventAuthorized() throws Exception { PreReadTableEvent context = mock(PreReadTableEvent.class); when(context.getEventType()).thenReturn(PreEventType.READ_TABLE); Table table = new Table(); table.setTableName(tableName); table.setDbName(authorizedDatabaseName1); when(context.getTable()).thenReturn(table); listener.onEvent(context); }
@Test(expected = InvalidOperationException.class) public void onReadTableEventUnauthorized() throws Exception { PreReadTableEvent context = mock(PreReadTableEvent.class); when(context.getEventType()).thenReturn(PreEventType.READ_TABLE); Table table = new Table(); table.setTableName(tableName); table.setDbName(unauthorizedDatabaseName); when(context.getTable()).thenReturn(table); listener.onEvent(context); }
@Test public void onReadDatabaseEventAuthorized() throws Exception { PreReadDatabaseEvent context = mock(PreReadDatabaseEvent.class); when(context.getEventType()).thenReturn(PreEventType.READ_DATABASE); Database db = new Database(); db.setName(authorizedDatabaseName2); when(context.getDatabase()).thenReturn(db); listener.onEvent(context); }
@Test(expected = InvalidOperationException.class) public void onReadDatabaseEventUnauthorized() throws Exception { PreReadDatabaseEvent context = mock(PreReadDatabaseEvent.class); when(context.getEventType()).thenReturn(PreEventType.READ_DATABASE); Database db = new Database(); db.setName(unauthorizedDatabaseName); when(context.getDatabase()).thenReturn(db); listener.onEvent(context); }
@Test(expected = InvalidOperationException.class) public void onCreateTableEvent() throws Exception { PreCreateTableEvent context = mock(PreCreateTableEvent.class); when(context.getEventType()).thenReturn(PreEventType.CREATE_TABLE); Table table = new Table(); table.setTableName(tableName); table.setDbName(authorizedDatabaseName1); listener.onEvent(context); }
@Test(expected = InvalidOperationException.class) public void onCreateDatabaseEvent() throws Exception { PreCreateDatabaseEvent context = mock(PreCreateDatabaseEvent.class); when(context.getEventType()).thenReturn(PreEventType.CREATE_DATABASE); Database db = new Database(); db.setName(authorizedDatabaseName1); listener.onEvent(context); }
@Test public void dbListTrimmed() throws Exception { environmentVariables .set(whitelistEnvironmentVariable, authorizedDatabaseName1 + " , " + authorizedDatabaseName2); listener = new ApiaryReadOnlyAuthPreEventListener(configuration); PreReadDatabaseEvent context = mock(PreReadDatabaseEvent.class); when(context.getEventType()).thenReturn(PreEventType.READ_DATABASE); Database db = new Database(); db.setName(authorizedDatabaseName2); when(context.getDatabase()).thenReturn(db); listener.onEvent(context); }