ezbake.data.elastic.test.EzElasticTestUtils.java Source code

Java tutorial

Introduction

Here is the source code for ezbake.data.elastic.test.EzElasticTestUtils.java

Source

/*   Copyright (C) 2013-2014 Computer Sciences Corporation
 *
 * 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 ezbake.data.elastic.test;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import ezbake.base.thrift.Visibility;
import ezbake.data.elastic.thrift.BaseFacetValue;
import ezbake.data.elastic.thrift.Document;
import ezbake.data.elastic.thrift.Facet;
import ezbake.data.elastic.thrift.FacetRange;
import ezbake.data.elastic.thrift.FacetRequest;
import ezbake.data.elastic.thrift.RangeFacet;
import ezbake.data.elastic.thrift.RangeType;

public final class EzElasticTestUtils {
    public static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

    private EzElasticTestUtils() {
    }

    public static Document generateDocument(String type, String jsonObject, String auth) {
        return generateDocument(type, jsonObject, new Visibility().setFormalVisibility(auth));
    }

    public static Document generateDocument(String type, String jsonObject, Visibility vis) {
        final Document doc = new Document();
        doc.set_id(UUID.randomUUID().toString());
        doc.set_type(type);
        doc.set_jsonObject(jsonObject);
        doc.setVisibility(vis);
        return doc;
    }

    public static Map<String, Object> jsonToMap(String json) {
        final JsonObject object = (JsonObject) new JsonParser().parse(json);
        final Iterator<Map.Entry<String, JsonElement>> iterator = object.entrySet().iterator();
        final Map<String, Object> map = new HashMap<>();
        while (iterator.hasNext()) {
            final Map.Entry<String, JsonElement> entry = iterator.next();
            final String key = entry.getKey();
            final JsonElement value = entry.getValue();
            if (value.isJsonPrimitive()) {
                map.put(key, value.getAsString());
            } else {
                map.put(key, jsonToMap(value.toString()));
            }
        }
        return map;
    }

    public static Facet generateDateBucketFacet(long... times) {
        final SimpleDateFormat dtg = new SimpleDateFormat(DATE_FORMAT);
        final Facet ssrDateFacet = new Facet();
        final RangeFacet dateRangeFacet = new RangeFacet();
        final BaseFacetValue dateField = new BaseFacetValue();
        dateField.setFacetField("visit");
        dateRangeFacet.setField(dateField);

        for (long time : times) {
            FacetRange range = new FacetRange(RangeType.DATE);
            range.setRangeFrom(dtg.format(time));
            dateRangeFacet.addToRanges(range);
        }

        final FacetRequest dateRequest = new FacetRequest();
        dateRequest.setRangeFacet(dateRangeFacet);

        ssrDateFacet.setLabel("Report Date");
        ssrDateFacet.setFacet(dateRequest);

        return ssrDateFacet;
    }
}