<commit_before>/*
 * Copyright 2014 The Apache Software Foundation.
 *
 * 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.apache.usergrid.rest.applications.collection;

import com.google.inject.Guice;
import com.google.inject.Injector;
import java.io.IOException;
import java.util.Map;
import org.apache.usergrid.corepersistence.TestGuiceModule;
import org.apache.usergrid.persistence.collection.serialization.SerializationFig;
import org.apache.usergrid.rest.AbstractRestIT;
import org.apache.usergrid.rest.TestContextSetup;
import org.apache.usergrid.rest.test.resource.CustomCollection;
import org.apache.usergrid.utils.MapUtils;
import static org.junit.Assert.fail;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class DuplicateNameIT extends AbstractRestIT {

    private static final Logger logger = LoggerFactory.getLogger( DuplicateNameIT.class );

    @Rule
    public TestContextSetup context = new TestContextSetup( this );

    @Test
    public void duplicateNamePrevention() {

        CustomCollection things = context.application().customCollection( "things" );

        Map<String, String> entity = MapUtils.hashMap( "name", "enzo" );

        try {
            things.create( entity );
        } catch (IOException ex) {
            logger.error("Cannot create entity", ex);
        }

        refreshIndex( context.getAppUuid() );

        Injector injector = Guice.createInjector( new TestGuiceModule( null ) ); 
        SerializationFig sfig = injector.getInstance( SerializationFig.class );

        // wait for any temporary unique value records to timeout
        try { Thread.sleep( sfig.getTimeout() * 1100 ); } catch (InterruptedException ignored) {} 

        try {
            things.create( entity );
            fail("Should not have created duplicate entity");
            
        } catch (Exception ex) {
            // good
        }
    }
}<commit_msg>[USERGRID-327] Bringing DuplicateNameIT inline with the new REST test framework<commit_after>/*
 * Copyright 2014 The Apache Software Foundation.
 *
 * 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.apache.usergrid.rest.applications.collection;

import com.sun.jersey.api.client.UniformInterfaceException;
import org.apache.usergrid.rest.test.resource2point0.AbstractRestIT;
import org.apache.usergrid.rest.test.resource2point0.model.Entity;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;


public class DuplicateNameIT extends AbstractRestIT {

    /**
     * Test to ensure that an error is returned when
     * attempting to POST multiple entities to the
     * same collection with the same name
     */
    @Test
    public void duplicateNamePrevention() {

        String collectionName = "things";
        Entity entity = new Entity();
        entity.put("name", "enzo");
        //Create an entity named "enzo" in the "things" collection
        entity = this.app().collection(collectionName).post(entity);
        refreshIndex();
        try {
            // Try to create a second entity in "things" with the name "enzo".
            this.app().collection(collectionName).post(entity);
            // fail if the POST did not return an exception
            fail("Should not have created duplicate entity");
        } catch (UniformInterfaceException uie) {
            //Check for an exception
            assertEquals(400, uie.getResponse().getStatus());
        }
    }
}
{'fraction_non_alphanumeric': 0.08920704845814978, 'fraction_numerical': 0.007048458149779736, 'mean_word_length': 3.212430426716141, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '5664935', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1159, 'n_words': 402}