<commit_before>package com.continuuity.metadata;

import com.continuuity.api.data.OperationException;
import com.continuuity.common.conf.Constants;
import com.continuuity.data.metadata.MetaDataStore;
import com.continuuity.data.operation.OperationContext;
import com.continuuity.data.runtime.DataFabricModules;
import com.continuuity.data2.transaction.inmemory.InMemoryTransactionManager;
import com.continuuity.metadata.thrift.Account;
import com.continuuity.metadata.thrift.Application;
import com.continuuity.metadata.thrift.Dataset;
import com.continuuity.metadata.thrift.Flow;
import com.continuuity.metadata.thrift.Mapreduce;
import com.continuuity.metadata.thrift.MetadataServiceException;
import com.continuuity.metadata.thrift.Query;
import com.continuuity.metadata.thrift.Stream;
import com.continuuity.runtime.MetadataModules;
import com.google.common.collect.Lists;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Collection;
import java.util.List;

/**
 * Tests metadata service functionality.
 */
public class MetadataServiceTest {

  /** Instance of metadata service. */
  private static MetadataService mds;

  /** Instance of account used for tests. */
  private static Account account;
  private static Injector injector;

  @BeforeClass
  public static void beforeMetadataService() throws Exception {
    injector = Guice.createInjector(
      new MetadataModules().getInMemoryModules(),
      new DataFabricModules().getInMemoryModules()
    );
    /* Instance of operation executor */
    injector.getInstance(InMemoryTransactionManager.class).init();
    mds = new MetadataService(injector.getInstance(MetaDataStore.class));
    account = new Account("demo");
  }

  @AfterClass
  public static void afterMetadataService() throws Exception {
    // nothing to be done here.
  }

  @Before
  public void cleanMetaData() throws OperationException {
    injector.getInstance(MetaDataStore.class).clear(
      new OperationContext(Constants.DEVELOPER_ACCOUNT_ID), "demo", null);
  }

  /**
   * Tests creation of streams with only Id. This should
   * throw MetadataServiceException.
   */
  @Test(expected = MetadataServiceException.class)
  public void testCreateStreamWithOnlyId() throws Exception {
    com.continuuity.metadata.thrift.Stream
        stream = new com.continuuity.metadata.thrift.Stream("id1");
    mds.createStream(account, stream);
    Assert.fail();
  }

  /**
   * Tests creation of streams with Id as empty string. This should
   * throw MetadataServiceException.
   */
  @Test(expected = MetadataServiceException.class)
  public void testCreateStreamWithEmptyId() throws Exception {
    com.continuuity.metadata.thrift.Stream
      stream = new com.continuuity.metadata.thrift.Stream("");
    mds.createStream(account, stream);
    Assert.fail();
  }

  /**
   * Tests creation of stream with only Id and Name. This should
   * throw MetadataServiceException.
   * @throws Exception
   */
  @Test(expected = MetadataServiceException.class)
  public void testCreateStreamWithIdAndEmptyName() throws Exception {
    Stream stream = new Stream("id1");
    stream.setName("");
    mds.createStream(account, stream);
  }

  /**
   * Tests creation of stream with all the necessary information.
   * This test should not throw any errors.
   * @throws Exception
   */
  @Test
  public void testCreateStreamCorrect() throws Exception {
    Stream stream = new Stream("id1");
    stream.setName("Funny stream");
    stream.setDescription("Funny stream that is so funny. You laugh it out");
    Assert.assertTrue(mds.createStream(account, stream));
    // Check if there is 1 stream available. Don't need to worry about
    // what's in there. We will do that later.
    Assert.assertTrue(mds.getStreams(account).size() > 0);
  }

  /**
   * Adds a stream "id2" and deletes it.
   * @throws Exception
   */
  @Test
  public void testDeleteStream() throws Exception {
    int count = mds.getStreams(account).size();

    Stream stream = new Stream("id2");
    stream.setName("Serious stream");
    stream.setDescription("Serious stream. Shutup");
    Assert.assertTrue(mds.createStream(account, stream));

    int afterAddCount = mds.getStreams(account).size();
    // Delete the stream now.
    Assert.assertTrue(mds.deleteStream(account, stream));
    int afterDeleteCount = mds.getStreams(account).size();
    Assert.assertTrue(count == afterAddCount - 1);
    Assert.assertTrue((afterAddCount - 1) == afterDeleteCount);
  }

  /**
   * Tests listing of streams for a given account.
   * @throws Exception
   */
  @Test
  public void testListStream() throws Exception {
    int before = mds.getStreams(account).size();
    Stream stream = new Stream("id3");
    stream.setName("Serious stream");
    stream.setDescription("Serious stream. Shutup");
    Assert.assertTrue(mds.createStream(account, stream));
    Collection<Stream> streams
      = mds.getStreams(account);
    int after = streams.size();
    Assert.assertTrue(after == before + 1);
    for (Stream s : streams) {
      if (s.getId().equals("id3")) {
        Assert.assertTrue("Serious stream".equals(s.getName()));
        Assert.assertTrue("Serious stream. Shutup".equals(s.getDescription()));
      }
    }
    Account account1 = new Account("abc");
    Assert.assertTrue(mds.getStreams(account1)isEmpty());
  }

  public void testCreateDataset() throws Exception {
    Dataset dataset = new Dataset("dataset1");
    dataset.setName("Data Set1");
    dataset.setType("counter");
    dataset.setDescription("test dataset");
    Assert.assertTrue(mds.createDataset(account, dataset));
    List<Dataset> dlist = mds.getDatasets(account);
    Assert.assertNotNull(dlist);
    Assert.assertTrue(dlist.size() > 0);
  }

  @Test
  public void testCreateDeleteListDataSet() throws Exception {
    testCreateDataset(); // creates a dataset.
    // Now delete it.
    Dataset dataset = new Dataset("dataset1");
    Assert.assertNotNull(mds.deleteDataset(account, dataset));
    List<Dataset> dlist = mds.getDatasets(account);
    Assert.assertTrue(dlistisEmpty());
    Dataset dataset1 = mds.getDataset(account, dataset);
    Assert.assertNotNull(dataset1);
  }

  public void testCreateQuery() throws Exception {
    Query query = new Query("query1", "appX");
    query.setName("Query 1");
    query.setServiceName("myname");
    query.setDescription("test dataset");
    Assert.assertTrue(mds.createQuery(account, query));
    List<Query> dlist = mds.getQueries(account);
    Assert.assertNotNull(dlist);
    Assert.assertTrue(dlist.size() > 0);
  }

  @Test
  public void testCreateDeleteListQuery() throws Exception {
    testCreateQuery(); // creates a dataset.
    // Now delete it.
    Query query = new Query("query1", "appX");
    Assert.assertNotNull(mds.deleteQuery(account, query));
    List<Query> qlist = mds.getQueries(account);
    Assert.assertTrue(qlistisEmpty());
    Query query1 = mds.getQuery(account, query);
    Assert.assertNotNull(query1);
  }

  @Test
  public void testCreateMapreduce() throws Exception {
    Mapreduce query = new Mapreduce("query1", "appX");
    query.setName("Mapreduce 1");
    query.setDescription("test dataset");
    Assert.assertTrue(mds.createMapreduce(account, query));
    List<Mapreduce> dlist = mds.getMapreduces(account);
    Assert.assertNotNull(dlist);
    Assert.assertTrue(dlist.size() > 0);
  }

  @Test
  public void testCreateDeleteListMapreduce() throws Exception {
    testCreateMapreduce(); // creates a dataset.
    // Now delete it.
    Mapreduce query = new Mapreduce("query1", "appX");
    Assert.assertNotNull(mds.deleteMapreduce(account, query));
    List<Mapreduce> qlist = mds.getMapreduces(account);
    Assert.assertTrue(qlistisEmpty());
    Mapreduce query1 = mds.getMapreduce(account, query);
    Assert.assertNotNull(query1);
  }

  /**
   * Tests creation of a application.
   * @throws Exception
   */
  @Test
  public void testCreateApplication() throws Exception {
    Application application = new Application("app1");
    application.setName("Application 1");
    application.setDescription("Test application");
    Assert.assertFalse(mds.getApplication(account, application).isExists());
    Assert.assertTrue(mds.createApplication(account, application));
    Assert.assertTrue(mds.getApplication(account, application).isExists());
  }

  /**
   * Tests update of a application.
   * @throws Exception
   */
  @Test
  public void testUpdateApplication() throws Exception {
    Application application = new Application("app1");
    application.setName("Application 1");
    application.setDescription("Test application");

    // Create application for first time.
    Assert.assertTrue(mds.createApplication(account, application));
    Assert.assertTrue(mds.getApplications(account).size() > 0);

    // We check what's in there.
    String beforeUpdateDescription = mds.getApplication(account, new Application("app1")).getDescription();
    Assert.assertTrue("Test application".equals(beforeUpdateDescription));

    // Now, we change the application description.
    Application updateApplication = new Application("app1");
    updateApplication.setDescription("Test updating application");
    updateApplication.setName("Application 1");
    Assert.assertTrue(mds.updateApplication(account, updateApplication));
    String afterUpdateDescription = mds.getApplication(account, new Application("app1")).getDescription();

    // We validate that it's updated.
    Assert.assertTrue("Test updating application".equals(afterUpdateDescription));
  }


  /**
   * Tests deletion of a application.
   * @throws Exception
   */
  @Test
  public void testDeleteApplication() throws Exception {
    int beforeAddCount = mds.getApplications(account).size();
    Application application = new Application("delapp1");
    application.setName("Application 1");
    application.setDescription("Test application");
    Assert.assertTrue(mds.createApplication(account, application));
    Assert.assertTrue(mds.getApplications(account).size() > 0);
    int afterAddCount = mds.getApplications(account).size();
    Application applicationToDelete = new Application("delapp1");
    Assert.assertTrue(mds.deleteApplication(account, applicationToDelete));
    int afterDeleteCount = mds.getApplications(account).size();
    Assert.assertTrue((beforeAddCount + 1) == afterAddCount);
    Assert.assertTrue((afterAddCount - 1) == afterDeleteCount);
  }

  /**
   * Tests listing of applications.
   * @throws Exception
   */
  @Test
  public void testListApplication() throws Exception {
    int before = mds.getApplications(account).size();
    Application application = new Application("tapp1");
    application.setName("Serious App");
    application.setDescription("Serious App. Shutup");
    Assert.assertTrue(mds.createApplication(account, application));
    Collection<Application> applications = mds.getApplications(account);
    int after = applications.size();
    Assert.assertTrue(after == before + 1);
    for (Application a : applications) {
      if (a.getId().equals("tapp1")) {
        Assert.assertTrue("Serious App".equals(a.getName()));
        Assert.assertTrue("Serious App. Shutup".equals(a.getDescription()));
      }
    }
    Account account1 = new Account("abc");
    Assert.assertTrue(mds.getApplications(account1)isEmpty());
  }

  /**
   * Tests listing of applications.
   * @throws Exception
   */
  @Test
  public void testFlowAndQueryStuff() throws Exception {

    // clean up streams/queries in mds if there are leftovers from other tests
    for (Stream stream : mds.getStreams(account)) {
      Assert.assertTrue(mds.deleteStream(account, stream));
    }<commit_msg>using .isEmpty() istead of .size()==0<commit_after>package com.continuuity.metadata;

import com.continuuity.api.data.OperationException;
import com.continuuity.common.conf.Constants;
import com.continuuity.data.metadata.MetaDataStore;
import com.continuuity.data.operation.OperationContext;
import com.continuuity.data.runtime.DataFabricModules;
import com.continuuity.data2.transaction.inmemory.InMemoryTransactionManager;
import com.continuuity.metadata.thrift.Account;
import com.continuuity.metadata.thrift.Application;
import com.continuuity.metadata.thrift.Dataset;
import com.continuuity.metadata.thrift.Flow;
import com.continuuity.metadata.thrift.Mapreduce;
import com.continuuity.metadata.thrift.MetadataServiceException;
import com.continuuity.metadata.thrift.Query;
import com.continuuity.metadata.thrift.Stream;
import com.continuuity.runtime.MetadataModules;
import com.google.common.collect.Lists;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Collection;
import java.util.List;

/**
 * Tests metadata service functionality.
 */
public class MetadataServiceTest {

  /** Instance of metadata service. */
  private static MetadataService mds;

  /** Instance of account used for tests. */
  private static Account account;
  private static Injector injector;

  @BeforeClass
  public static void beforeMetadataService() throws Exception {
    injector = Guice.createInjector(
      new MetadataModules().getInMemoryModules(),
      new DataFabricModules().getInMemoryModules()
    );
    /* Instance of operation executor */
    injector.getInstance(InMemoryTransactionManager.class).init();
    mds = new MetadataService(injector.getInstance(MetaDataStore.class));
    account = new Account("demo");
  }

  @AfterClass
  public static void afterMetadataService() throws Exception {
    // nothing to be done here.
  }

  @Before
  public void cleanMetaData() throws OperationException {
    injector.getInstance(MetaDataStore.class).clear(
      new OperationContext(Constants.DEVELOPER_ACCOUNT_ID), "demo", null);
  }

  /**
   * Tests creation of streams with only Id. This should
   * throw MetadataServiceException.
   */
  @Test(expected = MetadataServiceException.class)
  public void testCreateStreamWithOnlyId() throws Exception {
    com.continuuity.metadata.thrift.Stream
        stream = new com.continuuity.metadata.thrift.Stream("id1");
    mds.createStream(account, stream);
    Assert.fail();
  }

  /**
   * Tests creation of streams with Id as empty string. This should
   * throw MetadataServiceException.
   */
  @Test(expected = MetadataServiceException.class)
  public void testCreateStreamWithEmptyId() throws Exception {
    com.continuuity.metadata.thrift.Stream
      stream = new com.continuuity.metadata.thrift.Stream("");
    mds.createStream(account, stream);
    Assert.fail();
  }

  /**
   * Tests creation of stream with only Id and Name. This should
   * throw MetadataServiceException.
   * @throws Exception
   */
  @Test(expected = MetadataServiceException.class)
  public void testCreateStreamWithIdAndEmptyName() throws Exception {
    Stream stream = new Stream("id1");
    stream.setName("");
    mds.createStream(account, stream);
  }

  /**
   * Tests creation of stream with all the necessary information.
   * This test should not throw any errors.
   * @throws Exception
   */
  @Test
  public void testCreateStreamCorrect() throws Exception {
    Stream stream = new Stream("id1");
    stream.setName("Funny stream");
    stream.setDescription("Funny stream that is so funny. You laugh it out");
    Assert.assertTrue(mds.createStream(account, stream));
    // Check if there is 1 stream available. Don't need to worry about
    // what's in there. We will do that later.
    Assert.assertTrue(mds.getStreams(account).size() > 0);
  }

  /**
   * Adds a stream "id2" and deletes it.
   * @throws Exception
   */
  @Test
  public void testDeleteStream() throws Exception {
    int count = mds.getStreams(account).size();

    Stream stream = new Stream("id2");
    stream.setName("Serious stream");
    stream.setDescription("Serious stream. Shutup");
    Assert.assertTrue(mds.createStream(account, stream));

    int afterAddCount = mds.getStreams(account).size();
    // Delete the stream now.
    Assert.assertTrue(mds.deleteStream(account, stream));
    int afterDeleteCount = mds.getStreams(account).size();
    Assert.assertTrue(count == afterAddCount - 1);
    Assert.assertTrue((afterAddCount - 1) == afterDeleteCount);
  }

  /**
   * Tests listing of streams for a given account.
   * @throws Exception
   */
  @Test
  public void testListStream() throws Exception {
    int before = mds.getStreams(account).size();
    Stream stream = new Stream("id3");
    stream.setName("Serious stream");
    stream.setDescription("Serious stream. Shutup");
    Assert.assertTrue(mds.createStream(account, stream));
    Collection<Stream> streams
      = mds.getStreams(account);
    int after = streams.size();
    Assert.assertTrue(after == before + 1);
    for (Stream s : streams) {
      if (s.getId().equals("id3")) {
        Assert.assertTrue("Serious stream".equals(s.getName()));
        Assert.assertTrue("Serious stream. Shutup".equals(s.getDescription()));
      }
    }
    Account account1 = new Account("abc");
    Assert.assertTrue(mds.getStreams(account1).isEmpty());
  }

  public void testCreateDataset() throws Exception {
    Dataset dataset = new Dataset("dataset1");
    dataset.setName("Data Set1");
    dataset.setType("counter");
    dataset.setDescription("test dataset");
    Assert.assertTrue(mds.createDataset(account, dataset));
    List<Dataset> dlist = mds.getDatasets(account);
    Assert.assertNotNull(dlist);
    Assert.assertTrue(dlist.size() > 0);
  }

  @Test
  public void testCreateDeleteListDataSet() throws Exception {
    testCreateDataset(); // creates a dataset.
    // Now delete it.
    Dataset dataset = new Dataset("dataset1");
    Assert.assertNotNull(mds.deleteDataset(account, dataset));
    List<Dataset> dlist = mds.getDatasets(account);
    Assert.assertTrue(dlist.isEmpty());
    Dataset dataset1 = mds.getDataset(account, dataset);
    Assert.assertNotNull(dataset1);
  }

  public void testCreateQuery() throws Exception {
    Query query = new Query("query1", "appX");
    query.setName("Query 1");
    query.setServiceName("myname");
    query.setDescription("test dataset");
    Assert.assertTrue(mds.createQuery(account, query));
    List<Query> dlist = mds.getQueries(account);
    Assert.assertNotNull(dlist);
    Assert.assertTrue(dlist.size() > 0);
  }

  @Test
  public void testCreateDeleteListQuery() throws Exception {
    testCreateQuery(); // creates a dataset.
    // Now delete it.
    Query query = new Query("query1", "appX");
    Assert.assertNotNull(mds.deleteQuery(account, query));
    List<Query> qlist = mds.getQueries(account);
    Assert.assertTrue(qlist.isEmpty());
    Query query1 = mds.getQuery(account, query);
    Assert.assertNotNull(query1);
  }

  @Test
  public void testCreateMapreduce() throws Exception {
    Mapreduce query = new Mapreduce("query1", "appX");
    query.setName("Mapreduce 1");
    query.setDescription("test dataset");
    Assert.assertTrue(mds.createMapreduce(account, query));
    List<Mapreduce> dlist = mds.getMapreduces(account);
    Assert.assertNotNull(dlist);
    Assert.assertTrue(dlist.size() > 0);
  }

  @Test
  public void testCreateDeleteListMapreduce() throws Exception {
    testCreateMapreduce(); // creates a dataset.
    // Now delete it.
    Mapreduce query = new Mapreduce("query1", "appX");
    Assert.assertNotNull(mds.deleteMapreduce(account, query));
    List<Mapreduce> qlist = mds.getMapreduces(account);
    Assert.assertTrue(qlist.isEmpty());
    Mapreduce query1 = mds.getMapreduce(account, query);
    Assert.assertNotNull(query1);
  }

  /**
   * Tests creation of a application.
   * @throws Exception
   */
  @Test
  public void testCreateApplication() throws Exception {
    Application application = new Application("app1");
    application.setName("Application 1");
    application.setDescription("Test application");
    Assert.assertFalse(mds.getApplication(account, application).isExists());
    Assert.assertTrue(mds.createApplication(account, application));
    Assert.assertTrue(mds.getApplication(account, application).isExists());
  }

  /**
   * Tests update of a application.
   * @throws Exception
   */
  @Test
  public void testUpdateApplication() throws Exception {
    Application application = new Application("app1");
    application.setName("Application 1");
    application.setDescription("Test application");

    // Create application for first time.
    Assert.assertTrue(mds.createApplication(account, application));
    Assert.assertTrue(mds.getApplications(account).size() > 0);

    // We check what's in there.
    String beforeUpdateDescription = mds.getApplication(account, new Application("app1")).getDescription();
    Assert.assertTrue("Test application".equals(beforeUpdateDescription));

    // Now, we change the application description.
    Application updateApplication = new Application("app1");
    updateApplication.setDescription("Test updating application");
    updateApplication.setName("Application 1");
    Assert.assertTrue(mds.updateApplication(account, updateApplication));
    String afterUpdateDescription = mds.getApplication(account, new Application("app1")).getDescription();

    // We validate that it's updated.
    Assert.assertTrue("Test updating application".equals(afterUpdateDescription));
  }


  /**
   * Tests deletion of a application.
   * @throws Exception
   */
  @Test
  public void testDeleteApplication() throws Exception {
    int beforeAddCount = mds.getApplications(account).size();
    Application application = new Application("delapp1");
    application.setName("Application 1");
    application.setDescription("Test application");
    Assert.assertTrue(mds.createApplication(account, application));
    Assert.assertTrue(mds.getApplications(account).size() > 0);
    int afterAddCount = mds.getApplications(account).size();
    Application applicationToDelete = new Application("delapp1");
    Assert.assertTrue(mds.deleteApplication(account, applicationToDelete));
    int afterDeleteCount = mds.getApplications(account).size();
    Assert.assertTrue((beforeAddCount + 1) == afterAddCount);
    Assert.assertTrue((afterAddCount - 1) == afterDeleteCount);
  }

  /**
   * Tests listing of applications.
   * @throws Exception
   */
  @Test
  public void testListApplication() throws Exception {
    int before = mds.getApplications(account).size();
    Application application = new Application("tapp1");
    application.setName("Serious App");
    application.setDescription("Serious App. Shutup");
    Assert.assertTrue(mds.createApplication(account, application));
    Collection<Application> applications = mds.getApplications(account);
    int after = applications.size();
    Assert.assertTrue(after == before + 1);
    for (Application a : applications) {
      if (a.getId().equals("tapp1")) {
        Assert.assertTrue("Serious App".equals(a.getName()));
        Assert.assertTrue("Serious App. Shutup".equals(a.getDescription()));
      }
    }
    Account account1 = new Account("abc");
    Assert.assertTrue(mds.getApplications(account1).isEmpty());
  }

  /**
   * Tests listing of applications.
   * @throws Exception
   */
  @Test
  public void testFlowAndQueryStuff() throws Exception {

    // clean up streams/queries in mds if there are leftovers from other tests
    for (Stream stream : mds.getStreams(account)) {
      Assert.assertTrue(mds.deleteStream(account, stream));
    }
{'fraction_non_alphanumeric': 0.11879140819513019, 'fraction_numerical': 0.004515339494450775, 'mean_word_length': 4.1719773024879965, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '420188', 'n_tokens_mistral': 6606, 'n_tokens_neox': 6622, 'n_words': 1514}