com.flipkart.foxtrot.core.TestUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.flipkart.foxtrot.core.TestUtils.java

Source

/**
 * Copyright 2014 Flipkart Internet Pvt. Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.flipkart.foxtrot.core;

import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import com.flipkart.foxtrot.core.datastore.impl.hbase.HbaseConfig;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.flipkart.foxtrot.common.Document;
import com.flipkart.foxtrot.common.Table;
import com.flipkart.foxtrot.core.common.Action;
import com.flipkart.foxtrot.core.datastore.DataStore;
import com.flipkart.foxtrot.core.datastore.DataStoreException;
import com.flipkart.foxtrot.core.datastore.impl.hbase.HBaseDataStore;
import com.flipkart.foxtrot.core.datastore.impl.hbase.HbaseTableConnection;
import com.flipkart.foxtrot.core.querystore.actions.spi.ActionMetadata;
import com.flipkart.foxtrot.core.querystore.actions.spi.AnalyticsLoader;
import com.flipkart.foxtrot.core.querystore.actions.spi.AnalyticsProvider;

/**
 * Created by rishabh.goyal on 28/04/14.
 */
public class TestUtils {

    private static final Logger logger = LoggerFactory.getLogger(TestUtils.class.getSimpleName());
    public static String TEST_TABLE_NAME = "test-table";
    public static Table TEST_TABLE = new Table(TEST_TABLE_NAME, 7);

    public static DataStore getDataStore() throws DataStoreException {
        HTableInterface tableInterface = MockHTable.create();
        HbaseTableConnection tableConnection = Mockito.mock(HbaseTableConnection.class);
        when(tableConnection.getTable(Matchers.<Table>any())).thenReturn(tableInterface);
        when(tableConnection.getHbaseConfig()).thenReturn(new HbaseConfig());
        return new HBaseDataStore(tableConnection, new ObjectMapper());
    }

    public static Document getDocument(String id, long timestamp, Object[] args, ObjectMapper mapper) {
        Map<String, Object> data = new HashMap<String, Object>();
        for (int i = 0; i < args.length; i += 2) {
            data.put((String) args[i], args[i + 1]);
        }
        return new Document(id, timestamp, mapper.valueToTree(data));
    }

    public static void registerActions(AnalyticsLoader analyticsLoader, ObjectMapper mapper) throws Exception {
        Reflections reflections = new Reflections("com.flipkart.foxtrot", new SubTypesScanner());
        Set<Class<? extends Action>> actions = reflections.getSubTypesOf(Action.class);
        if (actions.isEmpty()) {
            throw new Exception("No analytics actions found!!");
        }
        List<NamedType> types = new Vector<NamedType>();
        for (Class<? extends Action> action : actions) {
            AnalyticsProvider analyticsProvider = action.getAnnotation(AnalyticsProvider.class);
            if (null == analyticsProvider.request() || null == analyticsProvider.opcode()
                    || analyticsProvider.opcode().isEmpty() || null == analyticsProvider.response()) {
                throw new Exception("Invalid annotation on " + action.getCanonicalName());
            }
            if (analyticsProvider.opcode().equalsIgnoreCase("default")) {
                logger.warn("Action " + action.getCanonicalName() + " does not specify cache token. "
                        + "Using default cache.");
            }
            analyticsLoader.register(new ActionMetadata(analyticsProvider.request(), action,
                    analyticsProvider.cacheable(), analyticsProvider.opcode()));
            types.add(new NamedType(analyticsProvider.request(), analyticsProvider.opcode()));
            types.add(new NamedType(analyticsProvider.response(), analyticsProvider.opcode()));
            logger.info("Registered action: " + action.getCanonicalName());
        }
        mapper.getSubtypeResolver().registerSubtypes(types.toArray(new NamedType[types.size()]));
    }

    public static List<Document> getQueryDocuments(ObjectMapper mapper) {
        List<Document> documents = new Vector<Document>();
        documents.add(TestUtils.getDocument("Z", 1397658117004L,
                new Object[] { "os", "android", "device", "nexus", "battery", 24 }, mapper));
        documents.add(TestUtils.getDocument("Y", 1397658117003L,
                new Object[] { "os", "android", "device", "nexus", "battery", 48 }, mapper));
        documents.add(TestUtils.getDocument("X", 1397658117002L,
                new Object[] { "os", "android", "device", "nexus", "battery", 74 }, mapper));
        documents.add(TestUtils.getDocument("W", 1397658117001L,
                new Object[] { "os", "android", "device", "nexus", "battery", 99 }, mapper));
        documents.add(TestUtils.getDocument("A", 1397658118000L,
                new Object[] { "os", "android", "version", 1, "device", "nexus" }, mapper));
        documents.add(TestUtils.getDocument("B", 1397658118001L,
                new Object[] { "os", "android", "version", 1, "device", "galaxy" }, mapper));
        documents.add(TestUtils.getDocument("C", 1397658118002L,
                new Object[] { "os", "android", "version", 2, "device", "nexus" }, mapper));
        documents.add(TestUtils.getDocument("D", 1397658118003L,
                new Object[] { "os", "ios", "version", 1, "device", "iphone" }, mapper));
        documents.add(TestUtils.getDocument("E", 1397658118004L,
                new Object[] { "os", "ios", "version", 2, "device", "ipad" }, mapper));
        return documents;
    }

    public static List<Document> getGroupDocuments(ObjectMapper mapper) {
        List<Document> documents = new Vector<Document>();
        documents.add(TestUtils.getDocument("Z", 1397658117000L,
                new Object[] { "os", "android", "version", 1, "device", "nexus", "battery", 24 }, mapper));
        documents.add(TestUtils.getDocument("Y", 1397658117000L,
                new Object[] { "os", "android", "version", 1, "device", "nexus", "battery", 48 }, mapper));
        documents.add(TestUtils.getDocument("X", 1397658117000L,
                new Object[] { "os", "android", "version", 3, "device", "galaxy", "battery", 74 }, mapper));
        documents.add(TestUtils.getDocument("W", 1397658117000L,
                new Object[] { "os", "android", "version", 2, "device", "nexus", "battery", 99 }, mapper));
        documents.add(TestUtils.getDocument("A", 1397658118000L,
                new Object[] { "os", "android", "version", 3, "device", "nexus", "battery", 87 }, mapper));
        documents.add(TestUtils.getDocument("B", 1397658118001L,
                new Object[] { "os", "android", "version", 2, "device", "galaxy", "battery", 76 }, mapper));
        documents.add(TestUtils.getDocument("C", 1397658118002L,
                new Object[] { "os", "android", "version", 2, "device", "nexus", "battery", 78 }, mapper));
        documents.add(TestUtils.getDocument("D", 1397658118003L,
                new Object[] { "os", "ios", "version", 1, "device", "iphone", "battery", 24 }, mapper));
        documents.add(TestUtils.getDocument("E", 1397658118004L,
                new Object[] { "os", "ios", "version", 2, "device", "ipad", "battery", 56 }, mapper));
        documents.add(TestUtils.getDocument("F", 1397658118005L,
                new Object[] { "os", "ios", "version", 2, "device", "nexus", "battery", 35 }, mapper));
        documents.add(TestUtils.getDocument("G", 1397658118006L,
                new Object[] { "os", "ios", "version", 2, "device", "ipad", "battery", 44 }, mapper));
        documents.add(TestUtils.getDocument("ABCD", 1397658118006L, new Object[] { "header.data", "ios" }, mapper));
        return documents;
    }

    public static List<Document> getHistogramDocuments(ObjectMapper mapper) {
        List<Document> documents = new Vector<Document>();
        documents.add(TestUtils.getDocument("Z", 1397658117000L,
                new Object[] { "os", "android", "version", 1, "device", "nexus", "battery", 24 }, mapper));
        documents.add(TestUtils.getDocument("Y", 1397651117000L,
                new Object[] { "os", "android", "version", 1, "device", "nexus", "battery", 48 }, mapper));
        documents.add(TestUtils.getDocument("X", 1397651117000L,
                new Object[] { "os", "android", "version", 3, "device", "galaxy", "battery", 74 }, mapper));
        documents.add(TestUtils.getDocument("W", 1397658117000L,
                new Object[] { "os", "android", "version", 2, "device", "nexus", "battery", 99 }, mapper));
        documents.add(TestUtils.getDocument("A", 1397658118000L,
                new Object[] { "os", "android", "version", 3, "device", "nexus", "battery", 87 }, mapper));
        documents.add(TestUtils.getDocument("B", 1397658218001L,
                new Object[] { "os", "android", "version", 2, "device", "galaxy", "battery", 76 }, mapper));
        documents.add(TestUtils.getDocument("C", 1398658218002L,
                new Object[] { "os", "android", "version", 2, "device", "nexus", "battery", 78 }, mapper));
        documents.add(TestUtils.getDocument("D", 1397758218003L,
                new Object[] { "os", "ios", "version", 1, "device", "iphone", "battery", 24 }, mapper));
        documents.add(TestUtils.getDocument("E", 1397958118004L,
                new Object[] { "os", "ios", "version", 2, "device", "ipad", "battery", 56 }, mapper));
        documents.add(TestUtils.getDocument("F", 1398653118005L,
                new Object[] { "os", "ios", "version", 2, "device", "nexus", "battery", 35 }, mapper));
        documents.add(TestUtils.getDocument("G", 1398653118006L,
                new Object[] { "os", "ios", "version", 2, "device", "ipad", "battery", 44 }, mapper));
        return documents;
    }

    public static List<Document> getTrendDocuments(ObjectMapper mapper) {
        List<Document> documents = new Vector<Document>();
        documents.add(TestUtils.getDocument("Z", 1397658117000L,
                new Object[] { "os", "android", "version", 1, "device", "nexus", "battery", 24 }, mapper));
        documents.add(TestUtils.getDocument("Y", 1397651117000L,
                new Object[] { "os", "android", "version", 1, "device", "nexus", "battery", 48 }, mapper));
        documents.add(TestUtils.getDocument("X", 1397651117000L,
                new Object[] { "os", "android", "version", 3, "device", "galaxy", "battery", 74 }, mapper));
        documents.add(TestUtils.getDocument("W", 1397658117000L,
                new Object[] { "os", "android", "version", 2, "device", "nexus", "battery", 99 }, mapper));
        documents.add(TestUtils.getDocument("A", 1397658118000L,
                new Object[] { "os", "android", "version", 3, "device", "nexus", "battery", 87 }, mapper));
        documents.add(TestUtils.getDocument("B", 1397658218001L,
                new Object[] { "os", "android", "version", 2, "device", "galaxy", "battery", 76 }, mapper));
        documents.add(TestUtils.getDocument("C", 1398658218002L,
                new Object[] { "os", "android", "version", 2, "device", "nexus", "battery", 78 }, mapper));
        documents.add(TestUtils.getDocument("D", 1397758218003L,
                new Object[] { "os", "ios", "version", 1, "device", "iphone", "battery", 24 }, mapper));
        documents.add(TestUtils.getDocument("E", 1397958118004L,
                new Object[] { "os", "ios", "version", 2, "device", "ipad", "battery", 56 }, mapper));
        documents.add(TestUtils.getDocument("F", 1398653118005L,
                new Object[] { "os", "ios", "version", 2, "device", "nexus", "battery", 35 }, mapper));
        documents.add(TestUtils.getDocument("G", 1398653118006L,
                new Object[] { "os", "ios", "version", 2, "device", "ipad", "battery", 44 }, mapper));
        return documents;
    }

    public static List<Document> getCountDocuments(ObjectMapper mapper) {
        List<Document> documents = new Vector<Document>();
        documents.add(TestUtils.getDocument("Z", 1397658117000L,
                new Object[] { "os", "android", "version", 1, "device", "nexus", "battery", 24 }, mapper));
        documents.add(TestUtils.getDocument("Y", 1397651117000L,
                new Object[] { "os", "android", "version", 1, "device", "nexus", "battery", 48 }, mapper));
        documents.add(TestUtils.getDocument("X", 1397651117000L,
                new Object[] { "os", "android", "version", 3, "device", "galaxy", "battery", 74 }, mapper));
        documents.add(TestUtils.getDocument("W", 1397658117000L,
                new Object[] { "os", "android", "version", 2, "device", "nexus", "battery", 99 }, mapper));
        documents.add(TestUtils.getDocument("A", 1397658118000L,
                new Object[] { "os", "android", "version", 3, "device", "nexus", "battery", 87 }, mapper));
        documents.add(TestUtils.getDocument("B", 1397658218001L,
                new Object[] { "os", "android", "version", 2, "device", "galaxy", "battery", 76 }, mapper));
        documents.add(TestUtils.getDocument("C", 1398658218002L,
                new Object[] { "os", "android", "version", 2, "device", "nexus", "battery", 78 }, mapper));
        documents.add(TestUtils.getDocument("D", 1397758218003L,
                new Object[] { "os", "ios", "version", 1, "device", "iphone", "battery", 24 }, mapper));
        documents.add(TestUtils.getDocument("E", 1397958118004L,
                new Object[] { "os", "ios", "version", 2, "device", "ipad", "battery", 56 }, mapper));
        documents.add(TestUtils.getDocument("F", 1398653118005L,
                new Object[] { "os", "ios", "version", 2, "device", "nexus", "battery", 35 }, mapper));
        documents.add(TestUtils.getDocument("G", 1398653118006L,
                new Object[] { "os", "ios", "version", 2, "device", "ipad", "battery", 44 }, mapper));
        return documents;
    }

    public static List<Document> getDistinctDocuments(ObjectMapper mapper) {
        List<Document> documents = new Vector<Document>();
        documents.add(TestUtils.getDocument("Z", 1397658117000L,
                new Object[] { "os", "android", "version", 1, "device", "nexus", "battery", 24 }, mapper));
        documents.add(TestUtils.getDocument("Y", 1397651117000L,
                new Object[] { "os", "android", "version", 1, "device", "nexus", "battery", 48 }, mapper));
        documents.add(TestUtils.getDocument("A", 1397658118000L,
                new Object[] { "os", "android", "version", 3, "device", "nexus", "battery", 87 }, mapper));
        documents.add(TestUtils.getDocument("D", 1397758218003L,
                new Object[] { "os", "ios", "version", 1, "device", "iphone", "battery", 24 }, mapper));
        documents.add(TestUtils.getDocument("E", 1397958118004L,
                new Object[] { "os", "ios", "version", 2, "device", "ipad", "battery", 56 }, mapper));
        documents.add(TestUtils.getDocument("F", 1398653118005L,
                new Object[] { "os", "ios", "version", 2, "device", "nexus", "battery", 35 }, mapper));
        documents.add(TestUtils.getDocument("G", 1398653118006L,
                new Object[] { "os", "ios", "version", 2, "device", "ipad", "battery", 44 }, mapper));
        return documents;
    }

    public static List<Document> getMappingDocuments(ObjectMapper mapper) {
        List<Document> documents = new Vector<Document>();
        Map<String, Object> document = new HashMap<String, Object>();
        document.put("word", "1234");
        document.put("data", Collections.singletonMap("data", "d"));
        document.put("header", Collections.singletonList(Collections.singletonMap("hello", "world")));
        documents.add(new Document("Z", System.currentTimeMillis(), mapper.valueToTree(document)));

        document = new HashMap<String, Object>();
        document.put("word", "1234");
        document.put("data", Collections.singletonMap("data", "d"));
        document.put("head", Collections.singletonList(Collections.singletonMap("hello", 23)));
        documents.add(new Document("Y", System.currentTimeMillis(), mapper.valueToTree(document)));
        return documents;
    }

    public static List<Document> getQueryDocumentsDifferentDate(ObjectMapper mapper, long startTimestamp) {
        List<Document> documents = new Vector<Document>();
        documents.add(TestUtils.getDocument("Z", startTimestamp,
                new Object[] { "os", "android", "device", "nexus", "battery", 24 }, mapper));
        documents.add(TestUtils.getDocument("Y", startTimestamp++,
                new Object[] { "os", "android", "device", "nexus", "battery", 48 }, mapper));
        documents.add(TestUtils.getDocument("X", startTimestamp++,
                new Object[] { "os", "android", "device", "nexus", "battery", 74 }, mapper));
        documents.add(TestUtils.getDocument("W", startTimestamp++,
                new Object[] { "os", "android", "device", "nexus", "battery", 99 }, mapper));
        documents.add(TestUtils.getDocument("A", startTimestamp++,
                new Object[] { "os", "android", "version", 1, "device", "nexus" }, mapper));
        documents.add(TestUtils.getDocument("B", startTimestamp++,
                new Object[] { "os", "android", "version", 1, "device", "galaxy" }, mapper));
        documents.add(TestUtils.getDocument("C", startTimestamp++,
                new Object[] { "os", "android", "version", 2, "device", "nexus" }, mapper));
        documents.add(TestUtils.getDocument("D", startTimestamp++,
                new Object[] { "os", "ios", "version", 1, "device", "iphone" }, mapper));
        documents.add(TestUtils.getDocument("E", startTimestamp++,
                new Object[] { "os", "ios", "version", 2, "device", "ipad" }, mapper));
        return documents;
    }

}