@Override public Health getHealth() { try { ColumnFamily<String, String> CF_SYSTEM_LOCAL = new ColumnFamily<String, String>( "system.local", StringSerializer.get(), StringSerializer.get(), StringSerializer.get() ); OperationResult<CqlResult<String, String>> result = keyspace.prepareQuery( CF_SYSTEM_LOCAL ) .setConsistencyLevel(ConsistencyLevel.CL_ONE) .withCql( "SELECT now() FROM system.local;" ) .execute(); if ( result.getResult().getRows().size() > 0 ) { return Health.GREEN; } } catch ( ConnectionException ex ) { logger.error( "Error connecting to Cassandra", ex ); } return Health.RED; } }
Map<StaticBuffer, EntryList> result = new HashMap<StaticBuffer, EntryList>(rows.size());
private void checkRowResult(Rows<Integer, Population> result, Integer startKey, Integer size, Population ... expected) throws Exception { int rowKey = startKey; for (Row<Integer, Population> row : result) { Assert.assertTrue(rowKey == row.getKey()); checkResult(row.getColumns(), expected); rowKey++; } Assert.assertTrue("Result: " + result.size() + ", size: " + size, size == result.size()); }
private void checkRowResult(Rows<Integer, Population> result, List<Integer> rowKeys, Population ... expected) throws Exception { int index = 0; for (Row<Integer, Population> row : result) { Assert.assertEquals(rowKeys.toString() + " " + row.getKey(), rowKeys.get(index++), row.getKey()); checkResult(row.getColumns(), expected); } Assert.assertTrue(rowKeys.size() == result.size()); }
@SuppressWarnings("unchecked") @Test public void testGetFilterIdsRawIndex() { ZuulFilterDAO dao = new ZuulFilterDAOCassandra(gateway); // setup empty response String fids = "filter1|filter2|filter3"; /* create mock response data */ Row<String, String> row0 = mockRow(); ColumnList<String> columnList0 = mockColumnList(row0); mockColumn(columnList0, "filter_ids", fids); // when(response.getRowByIndex(0)).thenReturn(row0); Iterator<Row<String, String>> iterator = (Iterator<Row<String, String>>) mock(Iterator.class); when(response.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, false); // 1 row when(iterator.next()).thenReturn(row0, (Row) null); when(gateway.select(anyString())).thenReturn(response); when(response.isEmpty()).thenReturn(false); when(response.size()).thenReturn(1); /* exercise the method we're testing */ String list = dao.getFilterIdsRaw("index"); /* validate responses */ assertEquals(fids, list); }
@SuppressWarnings("unchecked") @Test public void testGetFilterIdsRawIndex() { ZuulFilterDAO dao = new ZuulFilterDAOCassandra(gateway); // setup empty response String fids = "filter1|filter2|filter3"; /* create mock response data */ Row<String, String> row0 = mockRow(); ColumnList<String> columnList0 = mockColumnList(row0); mockColumn(columnList0, "filter_ids", fids); // when(response.getRowByIndex(0)).thenReturn(row0); Iterator<Row<String, String>> iterator = (Iterator<Row<String, String>>) mock(Iterator.class); when(response.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, false); // 1 row when(iterator.next()).thenReturn(row0, (Row) null); when(gateway.select(anyString())).thenReturn(response); when(response.isEmpty()).thenReturn(false); when(response.size()).thenReturn(1); /* exercise the method we're testing */ String list = dao.getFilterIdsRaw("index"); /* validate responses */ assertEquals(fids, list); }
/** * We can't unit test the CQL query or how Cassandra will behave, that will have to be manually confirmed. * We can however test that whatever rows are returned do end up in the List<EndpointURI> that we expect. */ @SuppressWarnings("unchecked") @Test public void testGetAllEndpointsReturnsResults() { ZuulFilterDAO dao = new ZuulFilterDAOCassandra(gateway); // setup empty response String fids = "filter1|filter2"; /* create mock response data */ Row<String, String> row0 = mockRow(); ColumnList<String> columnList0 = mockColumnList(row0); mockColumn(columnList0, "filter_ids", fids); // when(response.getRowByIndex(0)).thenReturn(row0); Iterator<Row<String, String>> iterator = (Iterator<Row<String, String>>) mock(Iterator.class); when(response.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, false); // 1 row when(iterator.next()).thenReturn(row0, (Row) null); when(gateway.select(anyString())).thenReturn(response); when(response.isEmpty()).thenReturn(false); when(response.size()).thenReturn(1); /* exercise the method we're testing */ List<String> list = dao.getAllFilterIDs(); /* validate responses */ assertEquals("filter1", list.get(0)); assertEquals("filter2", list.get(1)); }
@SuppressWarnings("unchecked") @Test public void testGetFilterIdsIndex() { ZuulFilterDAO dao = new ZuulFilterDAOCassandra(gateway); // setup empty response String fids = "filter1|filter2|filter3"; /* create mock response data */ Row<String, String> row0 = mockRow(); ColumnList<String> columnList0 = mockColumnList(row0); mockColumn(columnList0, "filter_ids", fids); // when(response.getRowByIndex(0)).thenReturn(row0); Iterator<Row<String, String>> iterator = (Iterator<Row<String, String>>) mock(Iterator.class); when(response.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, false); // 1 row when(iterator.next()).thenReturn(row0, (Row) null); when(gateway.select(anyString())).thenReturn(response); when(response.isEmpty()).thenReturn(false); when(response.size()).thenReturn(1); /* exercise the method we're testing */ List<String> list = dao.getFilterIdsIndex("index"); /* validate responses */ assertEquals(list.size(), 3); assertEquals(list.get(0), "filter1"); assertEquals(list.get(1), "filter2"); assertEquals(list.get(2), "filter3"); }
@SuppressWarnings("unchecked") @Test public void testGetFilterIdsIndex() { ZuulFilterDAO dao = new ZuulFilterDAOCassandra(gateway); // setup empty response String fids = "filter1|filter2|filter3"; /* create mock response data */ Row<String, String> row0 = mockRow(); ColumnList<String> columnList0 = mockColumnList(row0); mockColumn(columnList0, "filter_ids", fids); // when(response.getRowByIndex(0)).thenReturn(row0); Iterator<Row<String, String>> iterator = (Iterator<Row<String, String>>) mock(Iterator.class); when(response.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true, false); // 1 row when(iterator.next()).thenReturn(row0, (Row) null); when(gateway.select(anyString())).thenReturn(response); when(response.isEmpty()).thenReturn(false); when(response.size()).thenReturn(1); /* exercise the method we're testing */ List<String> list = dao.getFilterIdsIndex("index"); /* validate responses */ assertEquals(list.size(), 3); assertEquals(list.get(0), "filter1"); assertEquals(list.get(1), "filter2"); assertEquals(list.get(2), "filter3"); }
@Test public void testEmptyTable() throws Exception { CqlResult<String, String> result = keyspace.prepareQuery(CF_EMPTY_TABLE) .withCql("select * from astyanaxunittests.empty_table where key = 'blah'") .execute() .getResult(); Assert.assertFalse(result.hasRows()); Assert.assertFalse(result.hasNumber()); Assert.assertTrue(0 == result.getRows().size()); } }
private static void mockGetScriptForEndpoint(Rows<String, String> response, String filter) { /* mock data so that the getScriptForEndpoint at the end of the method will work */ Calendar now = Calendar.getInstance(); Row<String, String> row0 = mockRow(); ColumnList<String> columnList0 = mockColumnList(row0); mockColumn(columnList0, "filter_id", filter); mockColumn(columnList0, "revision", 1L); mockColumn(columnList0, "active", false); mockColumn(columnList0, "creation_date", now.getTime()); mockColumn(columnList0, "filter_code", "System.out.println(\"hello world\")".getBytes()); // what we put here doesn't matter mockColumn(columnList0, "filter_name", "name"); mockColumn(columnList0, "filter_type", "INBOUND"); mockColumn(columnList0, "canary", false); when(response.getRowByIndex(0)).thenReturn(row0); when(response.isEmpty()).thenReturn(true, false); // true on the first request, false thereafter when(response.size()).thenReturn(1); } }
private static void mockGetScriptForEndpoint(Rows<String, String> response, String filter) { /* mock data so that the getScriptForEndpoint at the end of the method will work */ Calendar now = Calendar.getInstance(); Row<String, String> row0 = mockRow(); ColumnList<String> columnList0 = mockColumnList(row0); mockColumn(columnList0, "filter_id", filter); mockColumn(columnList0, "revision", 1L); mockColumn(columnList0, "active", false); mockColumn(columnList0, "creation_date", now.getTime()); mockColumn(columnList0, "filter_code", "System.out.println(\"hello world\")".getBytes()); // what we put here doesn't matter mockColumn(columnList0, "filter_name", "name"); mockColumn(columnList0, "filter_type", "type"); mockColumn(columnList0, "canary", false); when(response.getRowByIndex(0)).thenReturn(row0); when(response.isEmpty()).thenReturn(true, false); // true on the first request, false thereafter when(response.size()).thenReturn(1); } }
private void testRowKeysWithAllColumns(boolean rowDeleted) throws Exception { Set<String> rowKeys = getRandomRowKeys(); Rows<String, String> rows = keyspace.prepareQuery(CF_COLUMN_RANGE_TEST).getRowSlice(rowKeys).execute().getResult(); if (rowDeleted) { Assert.assertTrue(rows.isEmpty()); return; } Assert.assertFalse(rows.isEmpty()); int rowKeysSize = rowKeys.size(); for (Row<String, String> row : rows) { boolean isPresent = rowKeys.remove(row.getKey()); Assert.assertTrue("Extraneous row: " + row.getKey(), isPresent); ColumnList<String> colList = row.getColumns(); Assert.assertEquals(26, colList.size()); for(int index=0; index<26; index++) { Column<String> col = colList.getColumnByIndex(index); Assert.assertTrue(String.valueOf((char)('a' + index)).equals(col.getName())); Assert.assertEquals(index + 1, col.getIntegerValue()); } } Assert.assertEquals(rowKeysSize, rows.size()); }
@Test public void testRowKeysAllColumnsQuery() throws Exception { keyspace.prepareQuery(CF_USER_INFO) .withCaching(true) .getRowSlice("acct_0", "acct_1", "acct_2", "acct_3") .execute(); Rows<String, String> result = keyspace.prepareQuery(CF_USER_INFO) .withCaching(true) .getRowSlice("acct_4", "acct_5", "acct_6", "acct_7") .execute() .getResult(); Assert.assertNotNull(result); Assert.assertTrue(4 == result.size()); for (Row<String, String> row : result) { int rowNo = Integer.parseInt(row.getKey().substring("acct_".length())); Assert.assertTrue(rowNo >= 4); Assert.assertTrue(rowNo <= 7); Assert.assertTrue(13 == row.getColumns().size()); } }
@Test public void testGetAllKeysRoot() throws ConnectionException { LOG.info("Starting testGetAllKeysRoot..."); List<String> keys = new ArrayList<String>(); for (char key = 'A'; key <= 'Z'; key++) { String keyName = Character.toString(key); keys.add(keyName); } OperationResult<Rows<String, String>> result = keyspace .prepareQuery(CF_STANDARD1) .getKeySlice(keys.toArray(new String[keys.size()])) .execute(); Assert.assertEquals(26, result.getResult().size()); Row<String, String> row; row = result.getResult().getRow("A"); Assert.assertEquals("A", row.getKey()); row = result.getResult().getRow("B"); Assert.assertEquals("B", row.getKey()); row = result.getResult().getRow("NonExistent"); Assert.assertNull(row); row = result.getResult().getRowByIndex(10); Assert.assertEquals("K", row.getKey()); LOG.info("... testGetAllKeysRoot"); }
@Test public void testRowKeysColumnSetQuery() throws Exception { keyspace.prepareQuery(CF_USER_INFO) .withCaching(true) .getRowSlice("acct_0", "acct_1", "acct_2", "acct_3") .withColumnSlice("firstname", "lastname", "age") .execute(); Rows<String, String> result = keyspace.prepareQuery(CF_USER_INFO) .withCaching(true) .getRowSlice("acct_4", "acct_5", "acct_6", "acct_7") .withColumnSlice("firstname", "lastname", "age") .execute() .getResult(); Assert.assertNotNull(result); Assert.assertTrue(4 == result.size()); for (Row<String, String> row : result) { int rowNo = Integer.parseInt(row.getKey().substring("acct_".length())); Assert.assertTrue(rowNo >= 4); Assert.assertTrue(rowNo <= 7); Assert.assertTrue(3 == row.getColumns().size()); } }
public String findKey(String app, String id, String location, String datacenter) { try { final String selectClause = String.format( "SELECT * FROM %s USING CONSISTENCY LOCAL_QUORUM WHERE %s = '%s' and %s = '%s' and %s = '%s' and %s = '%s' ", "tokens", CN_APPID, app, CN_ID, id, CN_LOCATION, location, CN_DC, datacenter); logger.info(selectClause); final ColumnFamily<String, String> CF_INSTANCES_NEW = ColumnFamily.newColumnFamily(KS_NAME, StringSerializer.get(), StringSerializer.get()); OperationResult<CqlResult<String, String>> result = bootKeyspace.prepareQuery(CF_INSTANCES_NEW) .withCql(selectClause).execute(); if (result == null || result.getResult().getRows().size() == 0) return null; Row<String, String> row = result.getResult().getRows().getRowByIndex(0); return row.getKey(); } catch (Exception e) { logger.warn("Caught an Unknown Exception during find a row matching cluster[" + app + "], id[" + id + "], and region[" + datacenter + "] ... -> " + e.getMessage()); throw new RuntimeException(e); } }
public String findKey(String cluster, String instanceId, String dc) { try { final String selectClause = String.format( "SELECT * FROM %s WHERE %s = '%s' and %s = '%s' and %s = '%s' ", CF_NAME_INSTANCES, CN_CLUSTER, cluster, CN_INSTANCEID, instanceId, CN_LOCATION, dc); logger.info(selectClause); final ColumnFamily<String, String> CF_INSTANCES_NEW = ColumnFamily.newColumnFamily(KS_NAME, StringSerializer.get(), StringSerializer.get()); OperationResult<CqlResult<String, String>> result = bootKeyspace.prepareQuery(CF_INSTANCES_NEW) .withCql(selectClause).execute(); if (result == null || result.getResult().getRows().size() == 0) return null; Row<String, String> row = result.getResult().getRows().getRowByIndex(0); return row.getKey(); } catch (Exception e) { logger.warn("Caught an Unknown Exception during find a row matching cluster[" + cluster + "], id[" + instanceId + "], and region[" + dc + "] ... -> " + e.getMessage()); throw new RuntimeException(e); } }
@Test public void testRowRangeAllColumnsQuery() throws Exception { String startToken = Murmur3Partitioner.get().getTokenForKey(StringSerializer.get().fromString("A")); String endToken = Murmur3Partitioner.get().getTokenForKey(StringSerializer.get().fromString("G")); keyspace.prepareQuery(CF_ROW_RANGE) .withCaching(true) .getRowRange(null, null, startToken, endToken, 10) .execute(); Rows<String, String> result = keyspace.prepareQuery(CF_ROW_RANGE) .withCaching(true) .getRowRange(null, null, startToken, endToken, 10) .execute() .getResult(); Assert.assertNotNull(result); Assert.assertTrue(3 == result.size()); for (Row<String, String> row : result) { Assert.assertTrue(26 == row.getColumns().size()); } }
@Test public void testRowRangeColumnSetQuery() throws Exception { String startToken = Murmur3Partitioner.get().getTokenForKey(StringSerializer.get().fromString("A")); String endToken = Murmur3Partitioner.get().getTokenForKey(StringSerializer.get().fromString("G")); keyspace.prepareQuery(CF_ROW_RANGE) .withCaching(true) .getRowRange(null, null, startToken, endToken, 10) .withColumnSlice("a", "s", "d", "f") .execute(); Rows<String, String> result = keyspace.prepareQuery(CF_ROW_RANGE) .withCaching(true) .getRowRange(null, null, startToken, endToken, 10) .withColumnSlice("a", "s", "d", "f") .execute() .getResult(); Assert.assertNotNull(result); Assert.assertTrue(3 == result.size()); for (Row<String, String> row : result) { Assert.assertTrue(4 == row.getColumns().size()); } }