/*
 *
 * CODENVY CONFIDENTIAL
 * ________________
 *
 * [2012] - [2013] Codenvy, S.A.
 * All Rights Reserved.
 * NOTICE: All information contained herein is, and remains
 * the property of Codenvy S.A. and its suppliers,
 * if any. The intellectual and technical concepts contained
 * herein are proprietary to Codenvy S.A.
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Codenvy S.A..
 */


package com.codenvy.analytics.metrics;

import com.codenvy.analytics.BaseTest;
import com.codenvy.analytics.datamodel.LongValueData;
import com.codenvy.analytics.datamodel.ValueData;
import com.codenvy.analytics.pig.scripts.ScriptType;
import com.codenvy.analytics.pig.scripts.util.Event;
import com.codenvy.analytics.pig.scripts.util.LogGenerator;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static org.testng.AssertJUnit.assertEquals;


/** @author <a href="mailto:abazko@codenvy.com">Anatoliy Bazko</a> */
public class TestFilters extends BaseTest {

    @BeforeClass
    public void init() throws Exception {
        List<Event> events = new ArrayList<>();
        events.add(Event.Builder.createTenantCreatedEvent("ws1", "user1").withDate("2013-02-10").build());
        events.add(Event.Builder.createTenantCreatedEvent("ws2", "anonymoususer_edjkx4").withDate("2013-02-10").build());
        events.add(Event.Builder.createTenantCreatedEvent("tmp-22rct0cq0rh8vs", "user2").withDate("2013-02-10").build());
        events.add(Event.Builder.createTenantCreatedEvent("tmp-p42qbfzn6iz9gn", "AnonymousUser_lnmyzh").withDate("2013-02-10").build());

        File log = LogGenerator.generateLog(events);

        Context.Builder builder = new Context.Builder();
        builder.put(Parameters.FROM_DATE, "20130210");
        builder.put(Parameters.TO_DATE, "20130210");
        builder.putAll(scriptsManager.getScript(ScriptType.EVENTS, MetricType.CREATED_WORKSPACES).getParamsAsMap());
        builder.put(Parameters.USER, Parameters.USER_TYPES.ANY.toString());
        builder.put(Parameters.WS, Parameters.WS_TYPES.ANY.toString());
        builder.put(Parameters.LOG, log.getAbsolutePath());
        pigServer.execute(ScriptType.EVENTS, builder.build());
    }

    @Test(dataProvider = "dataProvider")
    public void test(Object wsFilter, Object userFilter, long result) throws Exception {
        Metric metric = MetricFactory.getMetric(MetricType.CREATED_WORKSPACES);

        Context.Builder builder = new Context.Builder();
        if (wsFilter != null) {
            builder.put(MetricFilter.WS, wsFilter);
        }
        if (userFilter != null) {
            builder.put(MetricFilter.USER, userFilter);
        }


        ValueData valueData = metric.getValue(builder.build());
        assertEquals(wsFilter + ":" + userFilter, valueData, LongValueData.valueOf(result));
    }

    @DataProvider(name = "dataProvider")
    public Object[][] parametersFilterProvider() {
        return new Object[][]{{"ws1", null, 1},
                              {"tmp-22rct0cq0rh8vs", null, 1},
                              {null, "user1", 1},
                              {null, "anonymoususer_edjkx4", 1},
                              {Parameters.WS_TYPES.TEMPORARY.toString(), Parameters.USER_TYPES.ANONYMOUS.toString(), 1},
                              {Parameters.WS_TYPES.PERSISTENT.toString(), Parameters.USER_TYPES.REGISTERED.toString(), 1},
                              {Parameters.WS_TYPES.ANY.toString(), Parameters.USER_TYPES.ANY.toString(), 4},
                              {null, null, 4},
                              {null, Parameters.USER_TYPES.ANONYMOUS.toString(), 2},
                              {null, Parameters.USER_TYPES.REGISTERED.toString(), 2},
                              {Parameters.WS_TYPES.PERSISTENT.toString(), null, 2},
                              {Parameters.WS_TYPES.TEMPORARY.toString(), null, 2}};
    }
}

@@ -1481,37 +1481,38 @@
 t.Builder.create
-Tenan
+Projec
 tCreatedEvent(%22w
@@ -1514,20 +1514,34 @@
 nt(%22
-ws
+user
 1%22, %22
-user1
+ws1%22, %22%22, %22p%22, %22t
 %22).w
@@ -1603,37 +1603,38 @@
 t.Builder.create
-Tenan
+Projec
 tCreatedEvent(%22w
@@ -1635,15 +1635,8 @@
 ent(
-%22ws2%22, 
 %22ano
@@ -1653,16 +1653,37 @@
 _edjkx4%22
+, %22ws2%22, %22%22, %22p%22, %22t%22
 ).withDa
@@ -1740,37 +1740,38 @@
 t.Builder.create
-Tenan
+Projec
 tCreatedEvent(%22t
@@ -1764,24 +1764,33 @@
 reatedEvent(
+%22user2%22, 
 %22tmp-22rct0c
@@ -1800,21 +1800,26 @@
 h8vs%22, %22
-user2
+%22, %22p%22, %22t
 %22).withD
@@ -1857,32 +1857,49 @@
      events.add(
+%0A                
 Event.Builder.cr
@@ -1906,13 +1906,14 @@
 eate
-Tenan
+Projec
 tCre
@@ -1922,16 +1922,40 @@
 edEvent(
+%22AnonymousUser_lnmyzh%22, 
 %22tmp-p42
@@ -1969,36 +1969,26 @@
 z9gn%22, %22
-AnonymousUser_lnmyzh
+%22, %22p%22, %22t
 %22).withD
@@ -2290,28 +2290,30 @@
 (ScriptType.
-EVEN
+PROJEC
 TS, MetricTy
@@ -2315,33 +2315,23 @@
 ricType.
-CREATED_WORKSPACE
+PROJECT
 S).getPa
@@ -2348,156 +2348,8 @@
 ));%0A
-        builder.put(Parameters.USER, Parameters.USER_TYPES.ANY.toString());%0A        builder.put(Parameters.WS, Parameters.WS_TYPES.ANY.toString());%0A
     
@@ -2445,12 +2445,14 @@
 ype.
-EVEN
+PROJEC
 TS, 
@@ -2677,17 +2677,15 @@
 TED_
-WORKSPACE
+PROJECT
 S);%0A

Fix test