<commit_before>/*
 * Copyright © 2015 <NAME>, Inc.
 *
 * 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 co.cask.cdap.test;

import co.cask.cdap.StandaloneContainer;
import co.cask.cdap.api.app.Application;
import co.cask.cdap.api.dataset.DatasetSpecification;
import co.cask.cdap.client.ApplicationClient;
import co.cask.cdap.client.DatasetClient;
import co.cask.cdap.client.MetaClient;
import co.cask.cdap.client.ProgramClient;
import co.cask.cdap.client.StreamClient;
import co.cask.cdap.client.config.ClientConfig;
import co.cask.cdap.client.exception.NotFoundException;
import co.cask.cdap.client.exception.ProgramNotFoundException;
import co.cask.cdap.client.exception.UnAuthorizedAccessTokenException;
import co.cask.cdap.common.conf.CConfiguration;
import co.cask.cdap.common.utils.DirUtils;
import co.cask.cdap.data.Namespace;
import co.cask.cdap.data2.datafabric.DefaultDatasetNamespace;
import co.cask.cdap.proto.ApplicationRecord;
import co.cask.cdap.proto.ProgramRecord;
import co.cask.cdap.proto.ProgramType;
import co.cask.cdap.proto.StreamRecord;
import co.cask.cdap.security.authentication.client.AccessToken;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import org.apache.twill.filesystem.LocalLocationFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.annotation.Nullable;

/**
 *
 */
public class IntegrationTestBase {

  private static final Logger LOG = LoggerFactory.getLogger(IntegrationTestBase.class);

  /**
   * If empty, start our own CDAP standalone instance for testing.
   * If not empty, use the provided remote CDAP instance for testing.
   */
  private static final String INSTANCE_URI = System.getProperty("instanceUri", "");

  /**
   * CDAP access token for making requests to secure CDAP instances.
   * Can be obtained via the CDAP authentication server.
   */
  private static final String ACCESS_TOKEN = System.getProperty("accessToken", "");

  private static File tempDir;
  private static LocalLocationFactory locationFactory;
  private static IntegrationTestManager testManager;

  @BeforeClass
  public static void beforeClass() {
    tempDir = Files.createTempDir();
    locationFactory = new LocalLocationFactory(tempDir);
  }

  @AfterClass
  public static void afterClass() {
    try {
      DirUtils.deleteDirectoryContents(tempDir);
    } catch (IOException e) {
      LOG.warn("Failed to delete temp directory: " + tempDir.getAbsolutePath(), e);
    }

  }

  @Before
  public void setUp() throws Exception {
    if (INSTANCE_URI.isEmpty()) {
      StandaloneContainer.start();
    }

    assertNoApps();
    assertNoUserDatasets();
    // TODO: check metrics, streams, etc.

    // TODO: check no streams once streams can be deleted instead of truncating all streams
    StreamClient streamClient = getStreamClient();
    List<StreamRecord> streamRecords = streamClient.list();
    if (streamRecords.size() > 0) {
      for (StreamRecord streamRecord : streamRecords) {
        try {
          streamClient.truncate(streamRecord.getId());
        } catch (Exception e) {
          Assert.fail("All existing streams must be truncated" +
                      " - failed to truncate stream '" + streamRecord.getId() + "'");
        }
      }
    }
  }

  @After
  public void tearDown() throws Exception {
    assertNoApps();
    assertNoUserDatasets();
    // TODO: check metrics, streams, etc.

    if (INSTANCE_URI.isEmpty()) {
      StandaloneContainer.stop();
    }
  }

  protected static TestManager getTestManager() {
    return new IntegrationTestManager(getClientConfig(), locationFactory);
  }

  protected static ClientConfig getClientConfig() {
    ClientConfig.Builder builder = new ClientConfig.Builder();<commit_msg>Add clear call in IntegrationTestBase.tearDown()<commit_after>/*
 * Copyright © 2015 <NAME>, Inc.
 *
 * 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 co.cask.cdap.test;

import co.cask.cdap.StandaloneContainer;
import co.cask.cdap.api.app.Application;
import co.cask.cdap.api.dataset.DatasetSpecification;
import co.cask.cdap.client.ApplicationClient;
import co.cask.cdap.client.DatasetClient;
import co.cask.cdap.client.MetaClient;
import co.cask.cdap.client.ProgramClient;
import co.cask.cdap.client.StreamClient;
import co.cask.cdap.client.config.ClientConfig;
import co.cask.cdap.client.exception.NotFoundException;
import co.cask.cdap.client.exception.ProgramNotFoundException;
import co.cask.cdap.client.exception.UnAuthorizedAccessTokenException;
import co.cask.cdap.common.conf.CConfiguration;
import co.cask.cdap.common.utils.DirUtils;
import co.cask.cdap.data.Namespace;
import co.cask.cdap.data2.datafabric.DefaultDatasetNamespace;
import co.cask.cdap.proto.ApplicationRecord;
import co.cask.cdap.proto.ProgramRecord;
import co.cask.cdap.proto.ProgramType;
import co.cask.cdap.proto.StreamRecord;
import co.cask.cdap.security.authentication.client.AccessToken;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import org.apache.twill.filesystem.LocalLocationFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.annotation.Nullable;

/**
 *
 */
public class IntegrationTestBase {

  private static final Logger LOG = LoggerFactory.getLogger(IntegrationTestBase.class);

  /**
   * If empty, start our own CDAP standalone instance for testing.
   * If not empty, use the provided remote CDAP instance for testing.
   */
  private static final String INSTANCE_URI = System.getProperty("instanceUri", "");

  /**
   * CDAP access token for making requests to secure CDAP instances.
   * Can be obtained via the CDAP authentication server.
   */
  private static final String ACCESS_TOKEN = System.getProperty("accessToken", "");

  private static File tempDir;
  private static LocalLocationFactory locationFactory;

  @BeforeClass
  public static void beforeClass() {
    tempDir = Files.createTempDir();
    locationFactory = new LocalLocationFactory(tempDir);
  }

  @AfterClass
  public static void afterClass() {
    try {
      DirUtils.deleteDirectoryContents(tempDir);
    } catch (IOException e) {
      LOG.warn("Failed to delete temp directory: " + tempDir.getAbsolutePath(), e);
    }

  }

  @Before
  public void setUp() throws Exception {
    if (INSTANCE_URI.isEmpty()) {
      StandaloneContainer.start();
    }

    assertNoApps();
    assertNoUserDatasets();
    // TODO: check metrics, streams, etc.

    // TODO: check no streams once streams can be deleted instead of truncating all streams
    StreamClient streamClient = getStreamClient();
    List<StreamRecord> streamRecords = streamClient.list();
    if (streamRecords.size() > 0) {
      for (StreamRecord streamRecord : streamRecords) {
        try {
          streamClient.truncate(streamRecord.getId());
        } catch (Exception e) {
          Assert.fail("All existing streams must be truncated" +
                      " - failed to truncate stream '" + streamRecord.getId() + "'");
        }
      }
    }
  }

  @After
  public void tearDown() throws Exception {
    getTestManager().clear();
    assertNoApps();
    assertNoUserDatasets();
    // TODO: check metrics, streams, etc.

    if (INSTANCE_URI.isEmpty()) {
      StandaloneContainer.stop();
    }
  }

  protected static TestManager getTestManager() {
    return new IntegrationTestManager(getClientConfig(), locationFactory);
  }

  protected static ClientConfig getClientConfig() {
    ClientConfig.Builder builder = new ClientConfig.Builder();
{'fraction_non_alphanumeric': 0.10193358554014292, 'fraction_numerical': 0.0025220680958385876, 'mean_word_length': 4.062234042553191, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6746579', 'n_tokens_mistral': 2760, 'n_tokens_neox': 2735, 'n_words': 724}