AddUpdateTableIterator { public static void main(final String[] args) throws Exception { if (args.length < NUM_REQUIRED_ARGS) { System.err.println("Wrong number of arguments. \nUsage: " + "<graphId> " + "<comma separated schema paths> <store properties path> " + "<" + ADD_KEY + "," + REMOVE_KEY + " or " + UPDATE_KEY + "> " + "<file graph library path>"); System.exit(1); } final AccumuloProperties storeProps = AccumuloProperties.loadStoreProperties(getAccumuloPropertiesPath(args)); if (null == storeProps) { throw new IllegalArgumentException("Store properties are required to create a store"); } final Schema schema = Schema.fromJson(getSchemaPaths(args)); GraphLibrary library; if (null == getFileGraphLibraryPathString(args)) { library = new NoGraphLibrary(); } else { library = new FileGraphLibrary(getFileGraphLibraryPathString(args)); } library.addOrUpdate(getGraphId(args), schema, storeProps); final String storeClass = storeProps.getStoreClass(); if (null == storeClass) { throw new IllegalArgumentException("The Store class name was not found in the store properties for key: " + StoreProperties.STORE_CLASS); } final AccumuloStore store; try { store = Class.forName(storeClass).asSubclass(AccumuloStore.class).newInstance(); } catch (final InstantiationException | IllegalAccessException | ClassNotFoundException e) { throw new IllegalArgumentException("Could not create store of type: " + storeClass, e); } try { store.preInitialise( getGraphId(args), schema, storeProps ); } catch (final StoreException e) { throw new IllegalArgumentException("Could not initialise the store with provided arguments.", e); } if (!store.getConnection().tableOperations().exists(store.getTableName())) { TableUtils.createTable(store); } final String modifyKey = getModifyKey(args); switch (modifyKey) { case UPDATE_KEY: for (final String iterator : ITERATORS) { updateIterator(store, iterator); } break; case ADD_KEY: for (final String iterator : ITERATORS) { addIterator(store, iterator); } break; case REMOVE_KEY: for (final String iterator : ITERATORS) { removeIterator(store, iterator); } break; default: throw new IllegalArgumentException("Supplied add or update key (" + modifyKey + ") was not valid, it must either be " + ADD_KEY + "," + REMOVE_KEY + " or " + UPDATE_KEY + "."); } } private  AddUpdateTableIterator(); static void updateIterator(final AccumuloStore store, final String iteratorName); static void updateIterator(final AccumuloStore store, final String iteratorName,
                                      final IteratorSetting iteratorSetting); static void removeIterator(final AccumuloStore store, final String iteratorName); static void addIterator(final AccumuloStore store, final String iteratorName); static void addIterator(final AccumuloStore store, final IteratorSetting iteratorSetting); static void main(final String[] args); static final String UPDATE_KEY; static final String REMOVE_KEY; static final String ADD_KEY; }
@Test public void shouldRunMainWithFileGraphLibrary() throws Exception { final String[] args = {GRAPH_ID, SCHEMA_DIR, STORE_PROPS_PATH_UPDATED, "update", FILE_GRAPH_LIBRARY_TEST_PATH}; AddUpdateTableIterator.main(args); final Pair<Schema, StoreProperties> pair = new FileGraphLibrary(FILE_GRAPH_LIBRARY_TEST_PATH).get(GRAPH_ID); assertNotNull(pair, "Graph for " + GRAPH_ID + " was not found"); assertNotNull(pair.getFirst(), "Schema not found"); assertNotNull(pair.getSecond(), "Store properties not found"); JsonAssert.assertEquals(Schema.fromJson(Paths.get(SCHEMA_DIR)).toJson(false), pair.getFirst().toJson(false)); assertEquals(AccumuloProperties.loadStoreProperties(STORE_PROPS_PATH_UPDATED).getProperties(), pair.getSecond().getProperties()); }
@Test public void shouldOverrideExistingGraphInGraphLibrary() throws Exception { shouldRunMainWithFileGraphLibrary(); final String[] args = {GRAPH_ID, SCHEMA_2_DIR, STORE_PROPS_2_PATH_UPDATED, "update", FILE_GRAPH_LIBRARY_TEST_PATH}; AddUpdateTableIterator.main(args); final Pair<Schema, StoreProperties> pair = new FileGraphLibrary(FILE_GRAPH_LIBRARY_TEST_PATH).get(GRAPH_ID); assertNotNull(pair, "Graph for " + GRAPH_ID + " was not found"); assertNotNull(pair.getFirst(), "Schema not found"); assertNotNull(pair.getSecond(), "Store properties not found"); JsonAssert.assertEquals(Schema.fromJson(Paths.get(SCHEMA_2_DIR)).toJson(false), pair.getFirst().toJson(false)); assertEquals(AccumuloProperties.loadStoreProperties(STORE_PROPS_2_PATH_UPDATED).getProperties(), pair.getSecond().getProperties()); }
@Test public void shouldRunMainWithNoGraphLibrary() throws Exception { final String[] args = {GRAPH_ID, SCHEMA_DIR, STORE_PROPS_PATH_UPDATED, "update"}; AddUpdateTableIterator.main(args); final Pair<Schema, StoreProperties> pair = new FileGraphLibrary(FILE_GRAPH_LIBRARY_TEST_PATH).get(GRAPH_ID); assertNull(pair, "Graph should not have been stored"); }
@Test public void shouldThrowKeyErrorWhenInvalidModifyKeyGiven() throws Exception { final String[] args = {GRAPH_ID, SCHEMA_DIR, STORE_PROPS_PATH_UPDATED, "invalid key", FILE_GRAPH_LIBRARY_TEST_PATH}; IllegalArgumentException actual = assertThrows(IllegalArgumentException.class, () -> AddUpdateTableIterator.main(args)); assertEquals("Supplied add or update key (invalid key) was not valid, it must either be add,remove or update.", actual.getMessage()); }
@Test public void shouldReturnStoreClassNameNotFoundWhenStorePropsIsEmpty() throws Exception { final String[] args = {GRAPH_ID, SCHEMA_DIR, EMPTY_STORE_PROPS_PATH, "update", FILE_GRAPH_LIBRARY_TEST_PATH}; IllegalArgumentException actual = assertThrows(IllegalArgumentException.class, () -> AddUpdateTableIterator.main(args)); assertEquals("The Store class name was not found in the store properties for key: gaffer.store.class", actual.getMessage()); }
@Test public void shouldReturnInvalidFilePathErrorWhenPathDoesNotExist() throws Exception { final String[] args = {GRAPH_ID, SCHEMA_DIR, "invalid/file/path", "update", FILE_GRAPH_LIBRARY_TEST_PATH}; RuntimeException actual = assertThrows(RuntimeException.class, () -> AddUpdateTableIterator.main(args)); assertEquals("Failed to load store properties file : invalid/file/path", actual.getMessage()); }