<commit_before>/*
 * Copyright 2016 Copyright 2016 Akamai Technologies, Inc. All Rights Reserved.
 *
 * 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 com.akamai.testing.edgegrid.core;


import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.net.URI;
import java.util.GregorianCalendar;
import java.util.Set;
import java.util.UUID;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

/**
 * Unit tests EdgeGridV1Signer.
 *
 * @author <EMAIL>
 */
public class EdgeGridV1SignerTest {

    static final EdgeGridV1Signer DEFAULT_SIGNER = new EdgeGridV1Signer();
    static final UUID DEFAULT_NONCE = UUID.fromString("ec9d20ee-1e9b-4c1f-925a-f0017754f86c");
    static final ClientCredential DEFAULT_CREDENTIAL = ClientCredential.builder()
            .accessToken("<KEY>")
            .clientSecret("<KEY>
            .clientToken("akaa-k7glklzuxkkh2ycw-oadjrtwpvpn6yjoj")
            .build();

    static final long DEFAULT_TIMESTAMP = new GregorianCalendar(2016, 7, 4, 9, 0, 0).getTimeInMillis();

    @Test(dataProvider = "requestsForDefaultSettings")
    public void testForDefaultSettings(String caseName, String expectedAuthorizationHeader, Request request) throws RequestSigningException {
        String actualAuthorizationHeader = DEFAULT_SIGNER.getAuthorizationHeaderValue(request, DEFAULT_CREDENTIAL, DEFAULT_TIMESTAMP, DEFAULT_NONCE);
        assertThat(actualAuthorizationHeader, is(equalTo(expectedAuthorizationHeader)));
    }

    @Test(dataProvider = "requestsForHeadersSigning")
    public void testForHeadersSigning(String caseName, String expectedAuthorizationHeader, Set<String> headersToSign, Request request) throws RequestSigningException {
        EdgeGridV1Signer signer = new EdgeGridV1Signer(Algorithm.EG1_HMAC_SHA256, headersToSign, EdgeGridV1Signer.DEFAULT_MAX_BODY_SIZE_IN_BYTES);
        String actualAuthorizationHeader = signer.getAuthorizationHeaderValue(request, DEFAULT_CREDENTIAL, DEFAULT_TIMESTAMP, DEFAULT_NONCE);
        assertThat(actualAuthorizationHeader, is(equalTo(expectedAuthorizationHeader)));
    }

    @Test(expectedExceptions = IllegalArgumentException.class)
    public void shouldRejectRequestWithDuplicateHeaderNames() throws RequestSigningException {
        Request request = Request.builder()
                .method("GET")
                .uriWithQuery(URI.create("http://control.akamai.com/check"))
                .headers(ImmutableMultimap.<String, String>builder().put("Duplicate", "X").put("Duplicate", "Y").build())
                .build();

        DEFAULT_SIGNER.getAuthorizationHeaderValue(request, DEFAULT_CREDENTIAL, DEFAULT_TIMESTAMP, DEFAULT_NONCE);
    }<commit_msg>Change test input date to avoid timezone conversion problem<commit_after>/*
 * Copyright 2016 Copyright 2016 Akamai Technologies, Inc. All Rights Reserved.
 *
 * 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 com.akamai.testing.edgegrid.core;


import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.net.URI;
import java.util.Set;
import java.util.UUID;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

/**
 * Unit tests EdgeGridV1Signer.
 *
 * @author <EMAIL>
 */
public class EdgeGridV1SignerTest {

    static final EdgeGridV1Signer DEFAULT_SIGNER = new EdgeGridV1Signer();
    static final UUID DEFAULT_NONCE = UUID.fromString("ec9d20ee-1e9b-4c1f-925a-f0017754f86c");
    static final ClientCredential DEFAULT_CREDENTIAL = ClientCredential.builder()
            .accessToken("<KEY>")
            .clientSecret("<KEY>
            .clientToken("<KEY>")
            .build();

    /** Static timestamp corresponds to 2016-08-04T07:00:00+0000. */
    static final long DEFAULT_TIMESTAMP = 1470294000000L;

    @Test(dataProvider = "requestsForDefaultSettings")
    public void testForDefaultSettings(String caseName, String expectedAuthorizationHeader, Request request) throws RequestSigningException {
        String actualAuthorizationHeader = DEFAULT_SIGNER.getAuthorizationHeaderValue(request, DEFAULT_CREDENTIAL, DEFAULT_TIMESTAMP, DEFAULT_NONCE);
        assertThat(actualAuthorizationHeader, is(equalTo(expectedAuthorizationHeader)));
    }

    @Test(dataProvider = "requestsForHeadersSigning")
    public void testForHeadersSigning(String caseName, String expectedAuthorizationHeader, Set<String> headersToSign, Request request) throws RequestSigningException {
        EdgeGridV1Signer signer = new EdgeGridV1Signer(Algorithm.EG1_HMAC_SHA256, headersToSign, EdgeGridV1Signer.DEFAULT_MAX_BODY_SIZE_IN_BYTES);
        String actualAuthorizationHeader = signer.getAuthorizationHeaderValue(request, DEFAULT_CREDENTIAL, DEFAULT_TIMESTAMP, DEFAULT_NONCE);
        assertThat(actualAuthorizationHeader, is(equalTo(expectedAuthorizationHeader)));
    }

    @Test(expectedExceptions = IllegalArgumentException.class)
    public void shouldRejectRequestWithDuplicateHeaderNames() throws RequestSigningException {
        Request request = Request.builder()
                .method("GET")
                .uriWithQuery(URI.create("http://control.akamai.com/check"))
                .headers(ImmutableMultimap.<String, String>builder().put("Duplicate", "X").put("Duplicate", "Y").build())
                .build();

        DEFAULT_SIGNER.getAuthorizationHeaderValue(request, DEFAULT_CREDENTIAL, DEFAULT_TIMESTAMP, DEFAULT_NONCE);
    }
{'fraction_non_alphanumeric': 0.0883693574474342, 'fraction_numerical': 0.01867372445228643, 'mean_word_length': 4.602965403624382, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, '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': '4049885', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1873, 'n_words': 455}