Search in sources :

Example 11 with UIDMeta

use of net.opentsdb.meta.UIDMeta in project opentsdb by OpenTSDB.

the class TestSearchRpc method setupAnswerSearchQuery.

/**
   * Configures an Answer to respond with when the tests call 
   * tsdb.executeSearch(), responding to the type of query requested with valid
   * responses for parsing tests.
   */
private void setupAnswerSearchQuery() {
    mock_plugin = mock(SearchPlugin.class);
    Whitebox.setInternalState(tsdb, "search", mock_plugin);
    when(mock_plugin.executeQuery((SearchQuery) any())).thenAnswer(new Answer<Deferred<SearchQuery>>() {

        @Override
        public Deferred<SearchQuery> answer(InvocationOnMock invocation) throws Throwable {
            final Object[] args = invocation.getArguments();
            search_query = (SearchQuery) args[0];
            List<Object> results = new ArrayList<Object>(1);
            // if we want an empty response, return an empty response
            if (search_query.getQuery().toUpperCase().equals("EMTPY")) {
                search_query.setResults(results);
                search_query.setTotalResults(0);
                return Deferred.fromResult(search_query);
            }
            switch(search_query.getType()) {
                case TSMETA:
                    final TSMeta meta = new TSMeta("000001000001000001");
                    meta.setCreated(1356998400);
                    meta.setDescription("System CPU metric");
                    UIDMeta uid = new UIDMeta(UniqueIdType.METRIC, "000001");
                    final Field uid_name = UIDMeta.class.getDeclaredField("name");
                    uid_name.setAccessible(true);
                    uid_name.set(uid, "sys.cpu.0");
                    final Field metric = TSMeta.class.getDeclaredField("metric");
                    metric.setAccessible(true);
                    metric.set(meta, uid);
                    final ArrayList<UIDMeta> tags = new ArrayList<UIDMeta>(2);
                    uid = new UIDMeta(UniqueIdType.TAGK, "000001");
                    uid_name.set(uid, "host");
                    tags.add(uid);
                    uid = new UIDMeta(UniqueIdType.TAGV, "000001");
                    uid_name.set(uid, "web01");
                    tags.add(uid);
                    final Field tags_field = TSMeta.class.getDeclaredField("tags");
                    tags_field.setAccessible(true);
                    tags_field.set(meta, tags);
                    results.add(meta);
                    break;
                case LOOKUP:
                case TSMETA_SUMMARY:
                    final HashMap<String, Object> ts = new HashMap<String, Object>(1);
                    ts.put("metric", "sys.cpu.0");
                    final HashMap<String, String> tag_map = new HashMap<String, String>(2);
                    tag_map.put("host", "web01");
                    tag_map.put("owner", "ops");
                    ts.put("tags", tag_map);
                    ts.put("tsuid", "000001000001000001");
                    results.add(ts);
                    break;
                case TSUIDS:
                    results.add("000001000001000001");
                    results.add("000002000002000002");
                    break;
                case UIDMETA:
                    UIDMeta uid2 = new UIDMeta(UniqueIdType.METRIC, "000001");
                    final Field name_field = UIDMeta.class.getDeclaredField("name");
                    name_field.setAccessible(true);
                    name_field.set(uid2, "sys.cpu.0");
                    results.add(uid2);
                    uid2 = new UIDMeta(UniqueIdType.TAGK, "000001");
                    name_field.set(uid2, "host");
                    results.add(uid2);
                    break;
                case ANNOTATION:
                    final Annotation note = new Annotation();
                    note.setStartTime(1356998400);
                    note.setEndTime(1356998460);
                    note.setDescription("Something went pear shaped");
                    note.setTSUID("000001000001000001");
                    results.add(note);
                    break;
            }
            search_query.setResults(results);
            search_query.setTotalResults(results.size());
            search_query.setTime(0.42F);
            return Deferred.fromResult(search_query);
        }
    });
}
Also used : SearchQuery(net.opentsdb.search.SearchQuery) HashMap(java.util.HashMap) Deferred(com.stumbleupon.async.Deferred) ArrayList(java.util.ArrayList) TSMeta(net.opentsdb.meta.TSMeta) Annotation(net.opentsdb.meta.Annotation) Field(java.lang.reflect.Field) SearchPlugin(net.opentsdb.search.SearchPlugin) UIDMeta(net.opentsdb.meta.UIDMeta) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

UIDMeta (net.opentsdb.meta.UIDMeta)11 ArrayList (java.util.ArrayList)5 Deferred (com.stumbleupon.async.Deferred)3 Field (java.lang.reflect.Field)3 TSMeta (net.opentsdb.meta.TSMeta)3 UniqueIdType (net.opentsdb.uid.UniqueId.UniqueIdType)2 Callback (com.stumbleupon.async.Callback)1 DeferredGroupException (com.stumbleupon.async.DeferredGroupException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 TSDB (net.opentsdb.core.TSDB)1 Annotation (net.opentsdb.meta.Annotation)1 SearchPlugin (net.opentsdb.search.SearchPlugin)1 SearchQuery (net.opentsdb.search.SearchQuery)1 MockBase (net.opentsdb.storage.MockBase)1 NoSuchUniqueId (net.opentsdb.uid.NoSuchUniqueId)1 Config (net.opentsdb.utils.Config)1 KeyValue (org.hbase.async.KeyValue)1