<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.client;

import co.cask.cdap.client.app.AppReturnsArgs;
import co.cask.cdap.client.app.FakeApp;
import co.cask.cdap.client.common.ClientTestBase;
import co.cask.cdap.common.conf.Constants;
import co.cask.cdap.common.exception.NotFoundException;
import co.cask.cdap.common.exception.ProgramNotFoundException;
import co.cask.cdap.proto.Id;
import co.cask.cdap.proto.NamespaceMeta;
import co.cask.cdap.proto.ProgramType;
import co.cask.cdap.test.XSlowTests;
import co.cask.common.http.HttpMethod;
import co.cask.common.http.HttpRequest;
import co.cask.common.http.HttpRequests;
import co.cask.common.http.HttpResponse;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

import static org.junit.Assert.assertEquals;

/**
 * Test for {@link PreferencesClient}
 */
@Category(XSlowTests.class)
public class PreferencesClientTestRun extends ClientTestBase {
  private static final Gson GSON = new Gson();

  private PreferencesClient client;
  private ApplicationClient appClient;
  private ServiceClient serviceClient;
  private ProgramClient programClient;
  private NamespaceClient namespaceClient;

  @Before
  public void setUp() throws Throwable {
    super.setUp();
    client = new PreferencesClient(clientConfig);
    appClient = new ApplicationClient(clientConfig);
    serviceClient = new ServiceClient(clientConfig);
    programClient = new ProgramClient(clientConfig);
    namespaceClient = new NamespaceClient(clientConfig);
  }

  @Test
  public void testProgramAPI() throws Exception {
    // Add Namespace Id when ProgramClient needs it.
    Map<String, String> propMap = Maps.newHashMap();
    propMap.put("key", "instance");
    File jarFile = createAppJarFile(AppReturnsArgs.class);
    appClient.deploy(jarFile);
    try {
      client.setInstancePreferences(propMap);
      Map<String, String> setMap = Maps.newHashMap();
      setMap.put("saved", "args");
      programClient.setRuntimeArgs(AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE, setMap);
      assertEquals(setMap, programClient.getRuntimeArgs(AppReturnsArgs.NAME, ProgramType.SERVICE,
                                                        AppReturnsArgs.SERVICE));
      programClient.start(AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE,
                          ImmutableMap.of("run", "value"));
      assertProgramRunning(programClient, AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      propMap.put("run", "value");
      propMap.putAll(setMap);
      URL serviceURL = new URL(serviceClient.getServiceURL(AppReturnsArgs.NAME, AppReturnsArgs.SERVICE),
                               AppReturnsArgs.ENDPOINT);
      HttpRequest request = HttpRequest.builder(HttpMethod.GET, serviceURL).build();
      HttpResponse response = HttpRequests.execute(request);
      assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
      assertEquals(GSON.toJson(propMap), response.getResponseBodyAsString());
      programClient.stop(AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      assertProgramStopped(programClient, AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);

      client.deleteInstancePreferences();
      programClient.start(AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      assertProgramRunning(programClient, AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      propMap.remove("key");
      propMap.remove("run");
      serviceURL = new URL(serviceClient.getServiceURL(AppReturnsArgs.NAME, AppReturnsArgs.SERVICE),
                           AppReturnsArgs.ENDPOINT);
      request = HttpRequest.builder(HttpMethod.GET, serviceURL).build();
      response = HttpRequests.execute(request);
      assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
      assertEquals(GSON.toJson(propMap), response.getResponseBodyAsString());
      programClient.stop(AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      assertProgramStopped(programClient, AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);

      propMap.clear();
      programClient.setRuntimeArgs(AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE, propMap);
      programClient.start(AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      assertProgramRunning(programClient, AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      serviceURL = new URL(serviceClient.getServiceURL(AppReturnsArgs.NAME, AppReturnsArgs.SERVICE),
                           AppReturnsArgs.ENDPOINT);
      request = HttpRequest.builder(HttpMethod.GET, serviceURL).build();
      response = HttpRequests.execute(request);
      assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
      assertEquals(GSON.toJson(propMap), response.getResponseBodyAsString());
    } finally {
      programClient.stop(AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      assertProgramStopped(programClient, AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      appClient.delete(AppReturnsArgs.NAME);
    }
  }

  @Test
  public void testPreferences() throws Exception {
    Id.Namespace invalidNamespace = Id.Namespace.from("invalid");
    namespaceClient.create(new NamespaceMeta.Builder().setId(invalidNamespace.getId()).build());

    Map<String, String> propMap = client.getInstancePreferences();
    Assert.assertEquals(ImmutableMap.<String, String>of(), propMap);
    propMap.put("k1", "instance");
    client.setInstancePreferences(propMap);
    Assert.assertEquals(propMap, client.getInstancePreferences());

    File jarFile = createAppJarFile(FakeApp.class);
    appClient.deploy(jarFile);

    propMap.put("k1", "namespace");
    client.setNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, propMap);
    Assert.assertEquals(propMap, client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, true));
    Assert.assertEquals(propMap, client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, false));
    Assert.assertTrue(client.getNamespacePreferences(invalidNamespace, false).isEmpty());
    Assert.assertEquals("instance", client.getNamespacePreferences(invalidNamespace, true).get("k1"));

    client.deleteNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID);
    propMap.put("k1", "instance");
    Assert.assertEquals(propMap, client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, true));
    Assert.assertEquals(ImmutableMap.<String, String>of(), client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID,
                                                                                          false));
    propMap.put("k1", "namespace");
    client.setNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, propMap);
    Assert.assertEquals(propMap, client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, true));
    Assert.assertEquals(propMap, client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, false));
    propMap.put("k1", "application");
    client.setApplicationPreferences(FakeApp.ID, propMap);
    Assert.assertEquals(propMap, client.getApplicationPreferences(FakeApp.ID, true));
    Assert.assertEquals(propMap, client.getApplicationPreferences(FakeApp.ID, false));
    propMap.put("k1", "program");
    client.setProgramPreferences(FakeApp.ID, "flows", FakeApp.FLOWS.get(0), propMap);
    Assert.assertEquals(propMap, client.getProgramPreferences(FakeApp.ID, "flows", FakeApp.FLOWS.get(0), true));
    Assert.assertEquals(propMap, client.getProgramPreferences(FakeApp.ID, "flows", FakeApp.FLOWS.get(0), false));
    client.deleteProgramPreferences(FakeApp.ID, "flows", FakeApp.FLOWS.get(0));
    propMap.put("k1", "application");
    Assert.assertTrue(client.getProgramPreferences(FakeApp.ID, "flows", FakeApp.FLOWS.get(0), false).isEmpty());
    Assert.assertEquals(propMap, client.getProgramPreferences(FakeApp.ID, "flows", FakeApp.FLOWS.get(0), true));

    client.deleteApplicationPreferences(FakeApp.ID);

    propMap.put("k1", "namespace");
    Assert.assertTrue(client.getApplicationPreferences(FakeApp.ID, false).isEmpty());
    Assert.assertEquals(propMap, client.getApplicationPreferences(FakeApp.ID, true));
    Assert.assertEquals(propMap, client.getProgramPreferences(FakeApp.ID, "flows",
                                                              FakeApp.FLOWS.get(0), true));

    client.deleteNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID);
    propMap.put("k1", "instance");
    Assert.assertTrue(client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, false).isEmpty());
    Assert.assertEquals(propMap, client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, true));
    Assert.assertEquals(propMap, client.getApplicationPreferences(FakeApp.ID, true));
    Assert.assertEquals(propMap, client.getProgramPreferences(FakeApp.ID, "flows",
                                                              FakeApp.FLOWS.get(0), true));

    client.deleteInstancePreferences();
    propMap.clear();
    Assert.assertEquals(propMap, client.getInstancePreferences());
    Assert.assertEquals(propMap, client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, true));
    Assert.assertEquals(propMap, client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, true));
    Assert.assertEquals(propMap, client.getApplicationPreferences(FakeApp.ID, true));
    Assert.assertEquals(propMap, client.getProgramPreferences(FakeApp.ID, "flows",
                                                              FakeApp.FLOWS.get(0), true));


    //Test Deleting Application
    propMap.put("k1", "application");
    client.setApplicationPreferences(FakeApp.ID, propMap);
    Assert.assertEquals(propMap, client.getApplicationPreferences(FakeApp.ID, false));

    propMap.put("k1", "program");
    client.setProgramPreferences(FakeApp.ID, "flows", FakeApp.FLOWS.get(0), propMap);
    Assert.assertEquals(propMap, client.getProgramPreferences(FakeApp.ID, "flows",
                                                              FakeApp.FLOWS.get(0), false));

    appClient.delete(FakeApp.ID.getId());
    // deleting the app should have deleted the preferences that were stored. so deploy the app and check
    // if the preferences are empty. we need to deploy the app again since getting preferences of non-existent apps
    // is not allowed by the API.
    appClient.deploy(jarFile);
    propMap.clear();
    Assert.assertEquals(propMap, client.getApplicationPreferences(FakeApp.ID, false));
    Assert.assertEquals(propMap, client.getProgramPreferences(FakeApp.ID, "flows",
                                                              FakeApp.FLOWS.get(0), false));

    appClient.delete(FakeApp.ID.getId());
    namespaceClient.delete(invalidNamespace);
  }

  @Test
  public void testDeletingNamespace() throws Exception {
    Map<String, String> propMap = Maps.newHashMap();
    propMap.put("k1", "namespace");

    Id.Namespace myspace = Id.Namespace.from("myspace");
    namespaceClient.create(new NamespaceMeta.Builder().setId(myspace.getId()).build());

    client.setNamespacePreferences(myspace, propMap);
    Assert.assertEquals(propMap, client.getNamespacePreferences(myspace, false));
    Assert.assertEquals(propMap, client.getNamespacePreferences(myspace, true));

    namespaceClient.delete(myspace);
    namespaceClient.create(new NamespaceMeta.Builder().setId(myspace.getId()).build());
    Assert.assertTrue(client.getNamespacePreferences(myspace, false).isEmpty());
    Assert.assertTrue(client.getNamespacePreferences(myspace, true).isEmpty());

    namespaceClient.delete(myspace);
  }

  @Test(expected = NotFoundException.class)
  public void testInvalidNamespace() throws Exception {
    Id.Namespace somespace = Id.Namespace.from("somespace");
    client.setNamespacePreferences(somespace, ImmutableMap.of("k1", "v1"));
  }

  @Test(expected = NotFoundException.class)
  public void testInvalidApplication() throws Exception {
    Id.Application someapp = Id.Application.from("somespace", "someapp");
    client.getApplicationPreferences(someapp, true);
  }

  @Test(expected = ProgramNotFoundException.class)
  public void testInvalidProgram() throws Exception {
    Id.Application someapp = Id.Application.from("somespace", "someapp");
    client.deleteProgramPreferences(someapp, "flows", "myflow");
  }
}
<commit_msg>Fix checkstyle<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.client;

import co.cask.cdap.client.app.AppReturnsArgs;
import co.cask.cdap.client.app.FakeApp;
import co.cask.cdap.client.common.ClientTestBase;
import co.cask.cdap.common.conf.Constants;
import co.cask.cdap.common.exception.NotFoundException;
import co.cask.cdap.common.exception.ProgramNotFoundException;
import co.cask.cdap.proto.Id;
import co.cask.cdap.proto.NamespaceMeta;
import co.cask.cdap.proto.ProgramType;
import co.cask.cdap.test.XSlowTests;
import co.cask.common.http.HttpMethod;
import co.cask.common.http.HttpRequest;
import co.cask.common.http.HttpRequests;
import co.cask.common.http.HttpResponse;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

import static org.junit.Assert.assertEquals;

/**
 * Test for {@link PreferencesClient}
 */
@Category(XSlowTests.class)
public class PreferencesClientTestRun extends ClientTestBase {
  private static final Gson GSON = new Gson();

  private PreferencesClient client;
  private ApplicationClient appClient;
  private ServiceClient serviceClient;
  private ProgramClient programClient;
  private NamespaceClient namespaceClient;

  @Before
  public void setUp() throws Throwable {
    super.setUp();
    client = new PreferencesClient(clientConfig);
    appClient = new ApplicationClient(clientConfig);
    serviceClient = new ServiceClient(clientConfig);
    programClient = new ProgramClient(clientConfig);
    namespaceClient = new NamespaceClient(clientConfig);
  }

  @Test
  public void testProgramAPI() throws Exception {
    // Add Namespace Id when ProgramClient needs it.
    Map<String, String> propMap = Maps.newHashMap();
    propMap.put("key", "instance");
    File jarFile = createAppJarFile(AppReturnsArgs.class);
    appClient.deploy(jarFile);
    try {
      client.setInstancePreferences(propMap);
      Map<String, String> setMap = Maps.newHashMap();
      setMap.put("saved", "args");
      programClient.setRuntimeArgs(AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE, setMap);
      assertEquals(setMap, programClient.getRuntimeArgs(AppReturnsArgs.NAME, ProgramType.SERVICE,
                                                        AppReturnsArgs.SERVICE));
      programClient.start(AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE,
                          ImmutableMap.of("run", "value"));
      assertProgramRunning(programClient, AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      propMap.put("run", "value");
      propMap.putAll(setMap);
      URL serviceURL = new URL(serviceClient.getServiceURL(AppReturnsArgs.NAME, AppReturnsArgs.SERVICE),
                               AppReturnsArgs.ENDPOINT);
      HttpRequest request = HttpRequest.builder(HttpMethod.GET, serviceURL).build();
      HttpResponse response = HttpRequests.execute(request);
      assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
      assertEquals(GSON.toJson(propMap), response.getResponseBodyAsString());
      programClient.stop(AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      assertProgramStopped(programClient, AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);

      client.deleteInstancePreferences();
      programClient.start(AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      assertProgramRunning(programClient, AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      propMap.remove("key");
      propMap.remove("run");
      serviceURL = new URL(serviceClient.getServiceURL(AppReturnsArgs.NAME, AppReturnsArgs.SERVICE),
                           AppReturnsArgs.ENDPOINT);
      request = HttpRequest.builder(HttpMethod.GET, serviceURL).build();
      response = HttpRequests.execute(request);
      assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
      assertEquals(GSON.toJson(propMap), response.getResponseBodyAsString());
      programClient.stop(AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      assertProgramStopped(programClient, AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);

      propMap.clear();
      programClient.setRuntimeArgs(AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE, propMap);
      programClient.start(AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      assertProgramRunning(programClient, AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      serviceURL = new URL(serviceClient.getServiceURL(AppReturnsArgs.NAME, AppReturnsArgs.SERVICE),
                           AppReturnsArgs.ENDPOINT);
      request = HttpRequest.builder(HttpMethod.GET, serviceURL).build();
      response = HttpRequests.execute(request);
      assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
      assertEquals(GSON.toJson(propMap), response.getResponseBodyAsString());
    } finally {
      programClient.stop(AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      assertProgramStopped(programClient, AppReturnsArgs.NAME, ProgramType.SERVICE, AppReturnsArgs.SERVICE);
      appClient.delete(AppReturnsArgs.NAME);
    }
  }

  @Test
  public void testPreferences() throws Exception {
    Id.Namespace invalidNamespace = Id.Namespace.from("invalid");
    namespaceClient.create(new NamespaceMeta.Builder().setId(invalidNamespace.getId()).build());

    Map<String, String> propMap = client.getInstancePreferences();
    Assert.assertEquals(ImmutableMap.<String, String>of(), propMap);
    propMap.put("k1", "instance");
    client.setInstancePreferences(propMap);
    Assert.assertEquals(propMap, client.getInstancePreferences());

    File jarFile = createAppJarFile(FakeApp.class);
    appClient.deploy(jarFile);

    propMap.put("k1", "namespace");
    client.setNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, propMap);
    Assert.assertEquals(propMap, client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, true));
    Assert.assertEquals(propMap, client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, false));
    Assert.assertTrue(client.getNamespacePreferences(invalidNamespace, false).isEmpty());
    Assert.assertEquals("instance", client.getNamespacePreferences(invalidNamespace, true).get("k1"));

    client.deleteNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID);
    propMap.put("k1", "instance");
    Assert.assertEquals(propMap, client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, true));
    Assert.assertEquals(ImmutableMap.<String, String>of(),
                        client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, false));
    propMap.put("k1", "namespace");
    client.setNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, propMap);
    Assert.assertEquals(propMap, client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, true));
    Assert.assertEquals(propMap, client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, false));
    propMap.put("k1", "application");
    client.setApplicationPreferences(FakeApp.ID, propMap);
    Assert.assertEquals(propMap, client.getApplicationPreferences(FakeApp.ID, true));
    Assert.assertEquals(propMap, client.getApplicationPreferences(FakeApp.ID, false));
    propMap.put("k1", "program");
    client.setProgramPreferences(FakeApp.ID, "flows", FakeApp.FLOWS.get(0), propMap);
    Assert.assertEquals(propMap, client.getProgramPreferences(FakeApp.ID, "flows", FakeApp.FLOWS.get(0), true));
    Assert.assertEquals(propMap, client.getProgramPreferences(FakeApp.ID, "flows", FakeApp.FLOWS.get(0), false));
    client.deleteProgramPreferences(FakeApp.ID, "flows", FakeApp.FLOWS.get(0));
    propMap.put("k1", "application");
    Assert.assertTrue(client.getProgramPreferences(FakeApp.ID, "flows", FakeApp.FLOWS.get(0), false).isEmpty());
    Assert.assertEquals(propMap, client.getProgramPreferences(FakeApp.ID, "flows", FakeApp.FLOWS.get(0), true));

    client.deleteApplicationPreferences(FakeApp.ID);

    propMap.put("k1", "namespace");
    Assert.assertTrue(client.getApplicationPreferences(FakeApp.ID, false).isEmpty());
    Assert.assertEquals(propMap, client.getApplicationPreferences(FakeApp.ID, true));
    Assert.assertEquals(propMap, client.getProgramPreferences(FakeApp.ID, "flows",
                                                              FakeApp.FLOWS.get(0), true));

    client.deleteNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID);
    propMap.put("k1", "instance");
    Assert.assertTrue(client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, false).isEmpty());
    Assert.assertEquals(propMap, client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, true));
    Assert.assertEquals(propMap, client.getApplicationPreferences(FakeApp.ID, true));
    Assert.assertEquals(propMap, client.getProgramPreferences(FakeApp.ID, "flows",
                                                              FakeApp.FLOWS.get(0), true));

    client.deleteInstancePreferences();
    propMap.clear();
    Assert.assertEquals(propMap, client.getInstancePreferences());
    Assert.assertEquals(propMap, client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, true));
    Assert.assertEquals(propMap, client.getNamespacePreferences(Constants.DEFAULT_NAMESPACE_ID, true));
    Assert.assertEquals(propMap, client.getApplicationPreferences(FakeApp.ID, true));
    Assert.assertEquals(propMap, client.getProgramPreferences(FakeApp.ID, "flows",
                                                              FakeApp.FLOWS.get(0), true));


    //Test Deleting Application
    propMap.put("k1", "application");
    client.setApplicationPreferences(FakeApp.ID, propMap);
    Assert.assertEquals(propMap, client.getApplicationPreferences(FakeApp.ID, false));

    propMap.put("k1", "program");
    client.setProgramPreferences(FakeApp.ID, "flows", FakeApp.FLOWS.get(0), propMap);
    Assert.assertEquals(propMap, client.getProgramPreferences(FakeApp.ID, "flows",
                                                              FakeApp.FLOWS.get(0), false));

    appClient.delete(FakeApp.ID.getId());
    // deleting the app should have deleted the preferences that were stored. so deploy the app and check
    // if the preferences are empty. we need to deploy the app again since getting preferences of non-existent apps
    // is not allowed by the API.
    appClient.deploy(jarFile);
    propMap.clear();
    Assert.assertEquals(propMap, client.getApplicationPreferences(FakeApp.ID, false));
    Assert.assertEquals(propMap, client.getProgramPreferences(FakeApp.ID, "flows",
                                                              FakeApp.FLOWS.get(0), false));

    appClient.delete(FakeApp.ID.getId());
    namespaceClient.delete(invalidNamespace);
  }

  @Test
  public void testDeletingNamespace() throws Exception {
    Map<String, String> propMap = Maps.newHashMap();
    propMap.put("k1", "namespace");

    Id.Namespace myspace = Id.Namespace.from("myspace");
    namespaceClient.create(new NamespaceMeta.Builder().setId(myspace.getId()).build());

    client.setNamespacePreferences(myspace, propMap);
    Assert.assertEquals(propMap, client.getNamespacePreferences(myspace, false));
    Assert.assertEquals(propMap, client.getNamespacePreferences(myspace, true));

    namespaceClient.delete(myspace);
    namespaceClient.create(new NamespaceMeta.Builder().setId(myspace.getId()).build());
    Assert.assertTrue(client.getNamespacePreferences(myspace, false).isEmpty());
    Assert.assertTrue(client.getNamespacePreferences(myspace, true).isEmpty());

    namespaceClient.delete(myspace);
  }

  @Test(expected = NotFoundException.class)
  public void testInvalidNamespace() throws Exception {
    Id.Namespace somespace = Id.Namespace.from("somespace");
    client.setNamespacePreferences(somespace, ImmutableMap.of("k1", "v1"));
  }

  @Test(expected = NotFoundException.class)
  public void testInvalidApplication() throws Exception {
    Id.Application someapp = Id.Application.from("somespace", "someapp");
    client.getApplicationPreferences(someapp, true);
  }

  @Test(expected = ProgramNotFoundException.class)
  public void testInvalidProgram() throws Exception {
    Id.Application someapp = Id.Application.from("somespace", "someapp");
    client.deleteProgramPreferences(someapp, "flows", "myflow");
  }
}

{'fraction_non_alphanumeric': 0.11300245329307416, 'fraction_numerical': 0.002642007926023778, 'mean_word_length': 4.425061425061425, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4630619', 'n_tokens_mistral': 7127, 'n_tokens_neox': 7169, 'n_words': 1263}