package com.linkedin.thirdeye.datalayer.bao;

import com.linkedin.thirdeye.datalayer.DaoTestUtils;
import com.linkedin.thirdeye.datalayer.dto.AlertConfigDTO;
import com.linkedin.thirdeye.datalayer.pojo.AlertConfigBean.EmailConfig;
import com.linkedin.thirdeye.datasource.DAORegistry;
import java.util.Arrays;
import java.util.List;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.linkedin.thirdeye.constant.MetricAggFunction;
import com.linkedin.thirdeye.datalayer.dto.AnomalyFunctionDTO;

public class TestAnomalyFunctionManager {

  private Long anomalyFunctionId;
  private static String collection = "my dataset";
  private static String metricName = "__counts";

  private DAOTestBase testDAOProvider;
  private AnomalyFunctionManager anomalyFunctionDAO;
  @BeforeClass
  void beforeClass() {
    testDAOProvider = DAOTestBase.getInstance();
    DAORegistry daoRegistry = DAORegistry.getInstance();
    anomalyFunctionDAO = daoRegistry.getAnomalyFunctionDAO();
  }

  @AfterClass(alwaysRun = true)
  void afterClass() {
    testDAOProvider.cleanup();
  }

  @Test
  public void testCreate() {
    anomalyFunctionId = anomalyFunctionDAO.save(DaoTestUtils.getTestFunctionSpec(metricName, collection));
    Assert.assertNotNull(anomalyFunctionId);

    // test fetch all
    List<AnomalyFunctionDTO> functions = anomalyFunctionDAO.findAll();
    Assert.assertEquals(functions.size(), 1);

    functions = anomalyFunctionDAO.findAllActiveFunctions();
    Assert.assertEquals(functions.size(), 1);
  }

  @Test(dependsOnMethods = {"testCreate"})
  public void testFindNameEquals(){
    AnomalyFunctionDTO anomalyFunctionSpec = DaoTestUtils.getTestFunctionSpec(metricName, collection);
    Assert.assertNotNull(anomalyFunctionDAO.findWhereNameEquals(anomalyFunctionSpec.getFunctionName()));
  }

  @Test(dependsOnMethods = {"testCreate"})
  public void testFindAllByCollection() {
    List<AnomalyFunctionDTO> functions = anomalyFunctionDAO.findAllByCollection(collection);
    Assert.assertEquals(functions.size(), 1);
  }

  @Test(dependsOnMethods = {"testCreate"})
  public void testFindAllByApplication() {
    AlertConfigDTO alertConfigDTO = new AlertConfigDTO();
    alertConfigDTO.setName("test");
    alertConfigDTO.setApplication("test");
    EmailConfig emailConfig = new EmailConfig();
    emailConfig.setFunctionIds(Arrays.asList(anomalyFunctionId));
    alertConfigDTO.setEmailConfig(emailConfig);
    DAORegistry.getInstance().getAlertConfigDAO().save(alertConfigDTO);

    List<AnomalyFunctionDTO> applicationAnomalyFunctions = anomalyFunctionDAO.findAllByApplication("test");
    Assert.assertEquals(applicationAnomalyFunctions.size(), 1);
  }

  @Test(dependsOnMethods = {"testFindAllByCollection"})
  public void testDistinctMetricsByCollection() {
    List<String> metrics = anomalyFunctionDAO.findDistinctTopicMetricsByCollection(collection);
    Assert.assertEquals(metrics.get(0), metricName);
  }

  @Test(dependsOnMethods = { "testDistinctMetricsByCollection" })
  public void testUpdate() {
    AnomalyFunctionDTO spec = anomalyFunctionDAO.findById(anomalyFunctionId);
    Assert.assertNotNull(spec);
    Assert.assertEquals(spec.getMetricFunction(), MetricAggFunction.SUM);
    spec.setMetricFunction(MetricAggFunction.COUNT);
    anomalyFunctionDAO.save(spec);
    AnomalyFunctionDTO specReturned = anomalyFunctionDAO.findById(anomalyFunctionId);
    Assert.assertEquals(specReturned.getMetricFunction(), MetricAggFunction.COUNT);
  }

  @Test(dependsOnMethods = { "testUpdate" })
  public void testDelete() {
    anomalyFunctionDAO.deleteById(anomalyFunctionId);
    AnomalyFunctionDTO spec = anomalyFunctionDAO.findById(anomalyFunctionId);
    Assert.assertNull(spec);
  }
}

@@ -302,16 +302,46 @@
 Arrays;%0A
+import java.util.Collections;%0A
 import j
@@ -895,16 +895,62 @@
 ionDAO;%0A
+  private AlertConfigManager alertConfigDAO;%0A%0A
   @Befor
@@ -1143,24 +1143,78 @@
 ctionDAO();%0A
+    alertConfigDAO = daoRegistry.getAlertConfigDAO();%0A
   %7D%0A%0A  @Afte
@@ -2294,32 +2294,349 @@
 %7B%22testCreate%22%7D)%0A
+  public void testDistinctMetricsByCollection() %7B%0A    List%3CString%3E metrics = anomalyFunctionDAO.findDistinctTopicMetricsByCollection(collection);%0A    Assert.assertEquals(metrics.get(0), metricName);%0A  %7D%0A%0A  @Test(dependsOnMethods = %7B%22testFindNameEquals%22, %22testFindAllByCollection%22, %22testDistinctMetricsByCollection%22%7D)%0A
   public void te
@@ -2883,17 +2883,29 @@
 Ids(
-Arrays.as
+Collections.singleton
 List
@@ -2982,38 +2982,9 @@
     
-DAORegistry.getInstance().getA
+a
 lert
@@ -2992,18 +2992,16 @@
 onfigDAO
-()
 .save(al
@@ -3213,32 +3213,33 @@
 ndsOnMethods = %7B
+ 
 %22testFindAllByCo
@@ -3240,283 +3240,15 @@
 llBy
-Collection%22%7D)%0A  public void testDistinctMetricsByCollection() %7B%0A    List%3CString%3E metrics = anomalyFunctionDAO.findDistinctTopicMetricsByCollection(collection);%0A    Assert.assertEquals(metrics.get(0), metricName);%0A  %7D%0A%0A  @Test(dependsOnMethods = %7B %22testDistinctMetricsByCollec
+Applica
 tion

test - fix anomaly manager concurrent test bug (#2879)