<commit_before>/*******************************************************************************
 *
 *    Copyright (C) 2015-2018 the BBoxDB project
 *
 *    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 org.bboxdb.test.network;

import java.net.InetSocketAddress;
import java.util.EnumSet;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.bboxdb.BBoxDBMain;
import org.bboxdb.commons.math.Hyperrectangle;
import org.bboxdb.distribution.zookeeper.TupleStoreAdapter;
import org.bboxdb.distribution.zookeeper.ZookeeperClient;
import org.bboxdb.distribution.zookeeper.ZookeeperClientFactory;
import org.bboxdb.distribution.zookeeper.ZookeeperException;
import org.bboxdb.misc.BBoxDBConfigurationManager;
import org.bboxdb.misc.BBoxDBException;
import org.bboxdb.network.client.BBoxDB;
import org.bboxdb.network.client.BBoxDBClient;
import org.bboxdb.network.client.BBoxDBConnection;
import org.bboxdb.network.client.RoutingHeaderHelper;
import org.bboxdb.network.client.future.client.EmptyResultFuture;
import org.bboxdb.network.client.future.client.FutureRetryPolicy;
import org.bboxdb.network.client.future.client.TupleListFuture;
import org.bboxdb.network.packages.request.InsertOption;
import org.bboxdb.network.query.filter.UserDefinedStringFilter;
import org.bboxdb.network.routing.RoutingHeader;
import org.bboxdb.network.server.ErrorMessages;
import org.bboxdb.storage.entity.DeletedTuple;
import org.bboxdb.storage.entity.DistributionGroupConfiguration;
import org.bboxdb.storage.entity.DistributionGroupConfigurationBuilder;
import org.bboxdb.storage.entity.Tuple;
import org.bboxdb.storage.entity.TupleStoreConfiguration;
import org.bboxdb.storage.entity.TupleStoreConfigurationBuilder;
import org.bboxdb.storage.entity.TupleStoreName;
import org.bboxdb.storage.util.EnvironmentHelper;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.google.common.collect.Lists;

public class TestNetworkCommunication {

	/**
	 * The instance of the software
	 */
	private static BBoxDBMain bboxDBMain;

	/**
	 * The replication factor for the unit tests
	 */
	public final static short REPLICATION_FACTOR = 1;

	/**
	 * The name of the distribution group
	 */
	private static final String DISTRIBUTION_GROUP = "testgroup";

	/**
	 * The insert option - none
	 */
	private final EnumSet<InsertOption> INSERT_OPTIONS_NONE = EnumSet.noneOf(InsertOption.class);

	@BeforeClass
	public static void init() throws Exception {
		bboxDBMain = new BBoxDBMain();
		bboxDBMain.init();
		bboxDBMain.start();

		Thread.currentThread();
		// Wait some time to let the server process start
		Thread.sleep(5000);
	}

	@AfterClass
	public static void shutdown() throws Exception {
		if(bboxDBMain != null) {
			bboxDBMain.stop();
			bboxDBMain = null;
		}

		// Wait some time for socket re-use
		Thread.sleep(5000);
	}

	/**
	 * Re-create distribution group for each test
	 * @throws InterruptedException
	 * @throws BBoxDBException
	 */
	@Before
	public void before() throws InterruptedException, BBoxDBException {
		final BBoxDBConnection bboxdbConnection = connectToServer();
		final BBoxDBClient bboxDBClient = bboxdbConnection.getBboxDBClient();
		EnvironmentHelper.recreateDistributionGroup(bboxDBClient, DISTRIBUTION_GROUP);
		disconnect(bboxDBClient);
	}

	/**
	 * Test create a distribution group two times
	 * @throws BBoxDBException
	 * @throws InterruptedException
	 */
	@Test(timeout=60000)
	public void testCreateDistributionGroupTwoTimes() throws BBoxDBException, InterruptedException {
		final BBoxDBConnection bboxdbConnection = connectToServer();
		final BBoxDBClient bboxDBClient = bboxdbConnection.getBboxDBClient();

		// Create distribution group
		final DistributionGroupConfiguration configuration = DistributionGroupConfigurationBuilder.create(2)
				.withReplicationFactor((short) 1)
				.build();

		final EmptyResultFuture resultCreate = bboxDBClient.createDistributionGroup(DISTRIBUTION_GROUP,
				configuration);

		// Prevent retries
		resultCreate.setRetryPolicy(FutureRetryPolicy.RETRY_POLICY_NONE);

		resultCreate.waitForCompletion();
		Assert.assertTrue(resultCreate.isFailed());
		Assert.assertEquals(ErrorMessages.ERROR_DGROUP_EXISTS, resultCreate.getMessage(0));
		Assert.assertTrue(bboxdbConnection.getConnectionState().isInRunningState());

		disconnect(bboxDBClient);
	}
	
	/**
	 * Test create a distribution group two times
	 * @throws BBoxDBException
	 * @throws InterruptedException
	 */
	@Test(timeout=60000)
	public void testCreateDistributionGroupWithErrorsAndRecreate() throws BBoxDBException, InterruptedException {
		final BBoxDBConnection bboxdbConnection = connectToServer();
		final BBoxDBClient bboxDBClient = bboxdbConnection.getBboxDBClient();

		final String distributionGroupName = "mytestgroup_recreate";

		// Create distribution group
		final DistributionGroupConfiguration configuration1 = DistributionGroupConfigurationBuilder.create(2)
				.withReplicationFactor((short) 100) // More nodes than available
				.build();
		
		final EmptyResultFuture resultCreate1 = bboxDBClient.createDistributionGroup(distributionGroupName,
				configuration1);

		resultCreate1.setRetryPolicy(FutureRetryPolicy.RETRY_POLICY_NONE);
		resultCreate1.waitForCompletion();
		Assert.assertTrue(resultCreate1.isFailed());
		Assert.assertTrue(bboxdbConnection.getConnectionState().isInRunningState());

		// Create distribution group
		final DistributionGroupConfiguration configuration2 = DistributionGroupConfigurationBuilder.create(2)
				.withReplicationFactor((short) 1) // This time with a proper amount
				.build();
		
		final EmptyResultFuture resultCreate2 = bboxDBClient.createDistributionGroup(distributionGroupName,
				configuration2);

		resultCreate2.setRetryPolicy(FutureRetryPolicy.RETRY_POLICY_NONE);
		resultCreate2.waitForCompletion();
		Assert.assertFalse(resultCreate2.isFailed());
		Assert.assertTrue(bboxdbConnection.getConnectionState().isInRunningState());

		disconnect(bboxDBClient);
	}
<commit_msg>Fixed group name<commit_after>/*******************************************************************************
 *
 *    Copyright (C) 2015-2018 the BBoxDB project
 *
 *    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 org.bboxdb.test.network;

import java.net.InetSocketAddress;
import java.util.EnumSet;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.bboxdb.BBoxDBMain;
import org.bboxdb.commons.math.Hyperrectangle;
import org.bboxdb.distribution.zookeeper.TupleStoreAdapter;
import org.bboxdb.distribution.zookeeper.ZookeeperClient;
import org.bboxdb.distribution.zookeeper.ZookeeperClientFactory;
import org.bboxdb.distribution.zookeeper.ZookeeperException;
import org.bboxdb.misc.BBoxDBConfigurationManager;
import org.bboxdb.misc.BBoxDBException;
import org.bboxdb.network.client.BBoxDB;
import org.bboxdb.network.client.BBoxDBClient;
import org.bboxdb.network.client.BBoxDBConnection;
import org.bboxdb.network.client.RoutingHeaderHelper;
import org.bboxdb.network.client.future.client.EmptyResultFuture;
import org.bboxdb.network.client.future.client.FutureRetryPolicy;
import org.bboxdb.network.client.future.client.TupleListFuture;
import org.bboxdb.network.packages.request.InsertOption;
import org.bboxdb.network.query.filter.UserDefinedStringFilter;
import org.bboxdb.network.routing.RoutingHeader;
import org.bboxdb.network.server.ErrorMessages;
import org.bboxdb.storage.entity.DeletedTuple;
import org.bboxdb.storage.entity.DistributionGroupConfiguration;
import org.bboxdb.storage.entity.DistributionGroupConfigurationBuilder;
import org.bboxdb.storage.entity.Tuple;
import org.bboxdb.storage.entity.TupleStoreConfiguration;
import org.bboxdb.storage.entity.TupleStoreConfigurationBuilder;
import org.bboxdb.storage.entity.TupleStoreName;
import org.bboxdb.storage.util.EnvironmentHelper;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.google.common.collect.Lists;

public class TestNetworkCommunication {

	/**
	 * The instance of the software
	 */
	private static BBoxDBMain bboxDBMain;

	/**
	 * The replication factor for the unit tests
	 */
	public final static short REPLICATION_FACTOR = 1;

	/**
	 * The name of the distribution group
	 */
	private static final String DISTRIBUTION_GROUP = "testgroup";

	/**
	 * The insert option - none
	 */
	private final EnumSet<InsertOption> INSERT_OPTIONS_NONE = EnumSet.noneOf(InsertOption.class);

	@BeforeClass
	public static void init() throws Exception {
		bboxDBMain = new BBoxDBMain();
		bboxDBMain.init();
		bboxDBMain.start();

		Thread.currentThread();
		// Wait some time to let the server process start
		Thread.sleep(5000);
	}

	@AfterClass
	public static void shutdown() throws Exception {
		if(bboxDBMain != null) {
			bboxDBMain.stop();
			bboxDBMain = null;
		}

		// Wait some time for socket re-use
		Thread.sleep(5000);
	}

	/**
	 * Re-create distribution group for each test
	 * @throws InterruptedException
	 * @throws BBoxDBException
	 */
	@Before
	public void before() throws InterruptedException, BBoxDBException {
		final BBoxDBConnection bboxdbConnection = connectToServer();
		final BBoxDBClient bboxDBClient = bboxdbConnection.getBboxDBClient();
		EnvironmentHelper.recreateDistributionGroup(bboxDBClient, DISTRIBUTION_GROUP);
		disconnect(bboxDBClient);
	}

	/**
	 * Test create a distribution group two times
	 * @throws BBoxDBException
	 * @throws InterruptedException
	 */
	@Test(timeout=60000)
	public void testCreateDistributionGroupTwoTimes() throws BBoxDBException, InterruptedException {
		final BBoxDBConnection bboxdbConnection = connectToServer();
		final BBoxDBClient bboxDBClient = bboxdbConnection.getBboxDBClient();

		// Create distribution group
		final DistributionGroupConfiguration configuration = DistributionGroupConfigurationBuilder.create(2)
				.withReplicationFactor((short) 1)
				.build();

		final EmptyResultFuture resultCreate = bboxDBClient.createDistributionGroup(DISTRIBUTION_GROUP,
				configuration);

		// Prevent retries
		resultCreate.setRetryPolicy(FutureRetryPolicy.RETRY_POLICY_NONE);

		resultCreate.waitForCompletion();
		Assert.assertTrue(resultCreate.isFailed());
		Assert.assertEquals(ErrorMessages.ERROR_DGROUP_EXISTS, resultCreate.getMessage(0));
		Assert.assertTrue(bboxdbConnection.getConnectionState().isInRunningState());

		disconnect(bboxDBClient);
	}
	
	/**
	 * Test create a distribution group two times
	 * @throws BBoxDBException
	 * @throws InterruptedException
	 */
	@Test(timeout=60000)
	public void testCreateDistributionGroupWithErrorsAndRecreate() throws BBoxDBException, InterruptedException {
		final BBoxDBConnection bboxdbConnection = connectToServer();
		final BBoxDBClient bboxDBClient = bboxdbConnection.getBboxDBClient();

		final String distributionGroupName = "recreategroup";

		// Create distribution group
		final DistributionGroupConfiguration configuration1 = DistributionGroupConfigurationBuilder.create(2)
				.withReplicationFactor((short) 100) // More nodes than available
				.build();
		
		final EmptyResultFuture resultCreate1 = bboxDBClient.createDistributionGroup(distributionGroupName,
				configuration1);

		resultCreate1.setRetryPolicy(FutureRetryPolicy.RETRY_POLICY_NONE);
		resultCreate1.waitForCompletion();
		Assert.assertTrue(resultCreate1.isFailed());
		Assert.assertTrue(bboxdbConnection.getConnectionState().isInRunningState());

		// Create distribution group
		final DistributionGroupConfiguration configuration2 = DistributionGroupConfigurationBuilder.create(2)
				.withReplicationFactor((short) 1) // This time with a proper amount
				.build();
		
		final EmptyResultFuture resultCreate2 = bboxDBClient.createDistributionGroup(distributionGroupName,
				configuration2);

		resultCreate2.waitForCompletion();
		Assert.assertFalse(resultCreate2.isFailed());
		Assert.assertTrue(bboxdbConnection.getConnectionState().isInRunningState());

		disconnect(bboxDBClient);
	}

{'fraction_non_alphanumeric': 0.10910865810618008, 'fraction_numerical': 0.007734474731546144, 'mean_word_length': 5.639082751744766, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6275662', 'n_tokens_mistral': 4003, 'n_tokens_neox': 3689, 'n_words': 813}