<commit_before>/*
 * The contents of this file are subject to the license and copyright
 * detailed in the LICENSE and NOTICE files at the root of the source
 * tree and available online at
 *
 *     http://duracloud.org/license/
 */
package org.duracloud.integration.durastore.rest;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpStatus;
import org.duracloud.client.HttpHeaders;
import org.duracloud.common.constant.Constants;
import org.duracloud.common.web.EncodeUtil;
import org.duracloud.common.web.RestHttpHelper.HttpResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Runtime test of content REST API. The durastore web application must be
 * deployed and available at the baseUrl location in order for these tests to
 * pass.
 *
 * @author <NAME>
 */
public class TestContentRest extends BaseRestTester {

    private static final String CONTENT = "<content />";

    private static String[] contentIds = {"content1",
                                          "dir0/dir1/content2"};

    @Before
    public void setUp() throws Exception {
        // Add space
        setNewSpaceId();
        HttpResponse response = RestTestHelper.addSpace(BaseRestTester.spaceId);
        checkResponse(response, HttpStatus.SC_CREATED);

        for (String contentId : contentIds) {
            setUpContent(contentId);
        }
    }

    private void setUpContent(String contentId) throws Exception {
        HttpResponse response;

        // Add content to space
        String url = BaseRestTester.baseUrl + "/" + BaseRestTester.spaceId + "/" + contentId;
        Map<String, String> headers = new HashMap<String, String>();
        headers.put(RestTestHelper.PROPERTIES_NAME,
                    RestTestHelper.PROPERTIES_VALUE);
        response = BaseRestTester.restHelper.put(url, CONTENT, headers);
        checkResponse(response, HttpStatus.SC_CREATED);
    }

    @After
    public void tearDown() throws Exception {
        // Delete space
        HttpResponse response = RestTestHelper.deleteSpace(BaseRestTester.spaceId);
        String responseText = checkResponse(response, HttpStatus.SC_OK);
        assertNotNull(responseText);
    }

    private String removeParams(String contentId) {
        int paramStart = contentId.indexOf('?');
        int endIndex = paramStart > 0 ? paramStart : contentId.length();
        return contentId.substring(0, endIndex);
    }

    @Test
    public void testAddContent() throws Exception {
        // Test invalid content IDs

        // Question mark
        String id = "test?content";
        id = EncodeUtil.urlEncode(id);
        HttpResponse response = addContentItem(id);
        checkResponse(response, HttpStatus.SC_BAD_REQUEST);

        // Backslash
        id = "test\\content";
        id = EncodeUtil.urlEncode(id);
        response = addContentItem(id);
        checkResponse(response, HttpStatus.SC_BAD_REQUEST);

        // Too long
        id = "test-content";
        while(id.getBytes().length <= 1024) {
            id += "test-content";
        }
        response = addContentItem(id);
        checkResponse(response, HttpStatus.SC_BAD_REQUEST);

        // Test valid content IDs

        // Test Special characters
        char[] specialChars = {'~','`','!','@','$','^','&','*','(',')','_','-',
                               '+','=','\'',':','.',',','<','>','"','[',']',
                               '{','}','#','%',';','|',' ','/'};
        for(char character : specialChars) {
            testCharacterInContentId(character);
        }
    }

    private HttpResponse addContentItem(String contentId) throws Exception {
        String url = BaseRestTester.baseUrl + "/" + BaseRestTester.spaceId + "/" + contentId;
        Map<String, String> headers = new HashMap<String, String>();
        return BaseRestTester.restHelper.put(url,
                                             new ByteArrayInputStream(CONTENT.getBytes()),
                                             "text/plain",
                                             headers);
    }

    private void testCharacterInContentId(char character) throws Exception {
        String contentId = "test-" + String.valueOf(character) + "-content";
        contentId = EncodeUtil.urlEncode(contentId);
        HttpResponse response = addContentItem(contentId);
        assertEquals("Testing character: " + character,
                     HttpStatus.SC_CREATED,
                     response.getStatusCode());
        response = getContentItem(contentId);
        checkResponse(response, HttpStatus.SC_OK);
    }

    private HttpResponse getContentItem(String contentId) throws Exception {
        String url = BaseRestTester.baseUrl + "/" + BaseRestTester.spaceId + "/" + contentId;
        return BaseRestTester.restHelper.get(url);
    }
<commit_msg>Resolves compile issue in integration test due to change in RestHttpHelper<commit_after>/*
 * The contents of this file are subject to the license and copyright
 * detailed in the LICENSE and NOTICE files at the root of the source
 * tree and available online at
 *
 *     http://duracloud.org/license/
 */
package org.duracloud.integration.durastore.rest;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpStatus;
import org.duracloud.client.HttpHeaders;
import org.duracloud.common.constant.Constants;
import org.duracloud.common.web.EncodeUtil;
import org.duracloud.common.web.RestHttpHelper.HttpResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Runtime test of content REST API. The durastore web application must be
 * deployed and available at the baseUrl location in order for these tests to
 * pass.
 *
 * @author <NAME>
 */
public class TestContentRest extends BaseRestTester {

    private static final String CONTENT = "<content />";

    private static String[] contentIds = {"content1",
                                          "dir0/dir1/content2"};

    @Before
    public void setUp() throws Exception {
        // Add space
        setNewSpaceId();
        HttpResponse response = RestTestHelper.addSpace(BaseRestTester.spaceId);
        checkResponse(response, HttpStatus.SC_CREATED);

        for (String contentId : contentIds) {
            setUpContent(contentId);
        }
    }

    private void setUpContent(String contentId) throws Exception {
        HttpResponse response;

        // Add content to space
        String url = BaseRestTester.baseUrl + "/" + BaseRestTester.spaceId + "/" + contentId;
        Map<String, String> headers = new HashMap<String, String>();
        headers.put(RestTestHelper.PROPERTIES_NAME,
                    RestTestHelper.PROPERTIES_VALUE);
        response = BaseRestTester.restHelper.put(url, CONTENT, headers);
        checkResponse(response, HttpStatus.SC_CREATED);
    }

    @After
    public void tearDown() throws Exception {
        // Delete space
        HttpResponse response = RestTestHelper.deleteSpace(BaseRestTester.spaceId);
        String responseText = checkResponse(response, HttpStatus.SC_OK);
        assertNotNull(responseText);
    }

    private String removeParams(String contentId) {
        int paramStart = contentId.indexOf('?');
        int endIndex = paramStart > 0 ? paramStart : contentId.length();
        return contentId.substring(0, endIndex);
    }

    @Test
    public void testAddContent() throws Exception {
        // Test invalid content IDs

        // Question mark
        String id = "test?content";
        id = EncodeUtil.urlEncode(id);
        HttpResponse response = addContentItem(id);
        checkResponse(response, HttpStatus.SC_BAD_REQUEST);

        // Backslash
        id = "test\\content";
        id = EncodeUtil.urlEncode(id);
        response = addContentItem(id);
        checkResponse(response, HttpStatus.SC_BAD_REQUEST);

        // Too long
        id = "test-content";
        while(id.getBytes().length <= 1024) {
            id += "test-content";
        }
        response = addContentItem(id);
        checkResponse(response, HttpStatus.SC_BAD_REQUEST);

        // Test valid content IDs

        // Test Special characters
        char[] specialChars = {'~','`','!','@','$','^','&','*','(',')','_','-',
                               '+','=','\'',':','.',',','<','>','"','[',']',
                               '{','}','#','%',';','|',' ','/'};
        for(char character : specialChars) {
            testCharacterInContentId(character);
        }
    }

    private HttpResponse addContentItem(String contentId) throws Exception {
        String url = BaseRestTester.baseUrl + "/" + BaseRestTester.spaceId + "/" + contentId;
        Map<String, String> headers = new HashMap<String, String>();
        return BaseRestTester.restHelper.put(url,
                                             new ByteArrayInputStream(CONTENT.getBytes()),
                                             "text/plain",
                                             CONTENT.length(),
                                             headers);
    }

    private void testCharacterInContentId(char character) throws Exception {
        String contentId = "test-" + String.valueOf(character) + "-content";
        contentId = EncodeUtil.urlEncode(contentId);
        HttpResponse response = addContentItem(contentId);
        assertEquals("Testing character: " + character,
                     HttpStatus.SC_CREATED,
                     response.getStatusCode());
        response = getContentItem(contentId);
        checkResponse(response, HttpStatus.SC_OK);
    }

    private HttpResponse getContentItem(String contentId) throws Exception {
        String url = BaseRestTester.baseUrl + "/" + BaseRestTester.spaceId + "/" + contentId;
        return BaseRestTester.restHelper.get(url);
    }

{'fraction_non_alphanumeric': 0.11474423269809428, 'fraction_numerical': 0.0020060180541624875, 'mean_word_length': 2.4525623268698062, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3440899', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2583, 'n_words': 617}