<commit_before>/*
 * Copyright 2016 <NAME>
 *
 * 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.github.dannil.scbjavaclient.client;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import com.github.dannil.scbjavaclient.exception.SCBClientForbiddenException;
import com.github.dannil.scbjavaclient.exception.SCBClientUrlNotFoundException;
import com.github.dannil.scbjavaclient.utility.QueryBuilder;

import org.apache.commons.io.FilenameUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class AbstractClientIT {

    private static class DummyClient extends AbstractClient {

        protected DummyClient() {
            super();
        }

        protected DummyClient(Locale locale) {
            super(locale);
        }

        @Override
        public String getUrl() {
            // TODO Auto-generated method stub
            return null;
        }

    }

    @Test
    public void toFallbackUrlGet() {
        DummyClient client = new DummyClient(new Locale("en", "US"));

        String table = "HE/HE0103/HE0103B/BefolkningAlder";

        // This request is performed by a dummy Client which is set to English
        // (as specified in the setup method). This means that if we receive a response
        // with Swedish text, we've used the fallback url.
        String response = client.get(client.getRootUrl() + table);

        assertTrue(response.contains("ålder"));
        assertTrue(response.contains("kön"));
        assertTrue(response.contains("boendeform"));
        assertTrue(response.contains("tabellinnehåll"));
        assertTrue(response.contains("år"));
    }

    @Test
    public void toFallbackUrlPost() {
        DummyClient client = new DummyClient(new Locale("en", "US"));

        String table = "HE/HE0103/HE0103B/BefolkningAlder";

        Map<String, Collection<?>> map = new HashMap<String, Collection<?>>();
        map.put("ContentsCode", Arrays.asList("HE0103D2"));
        map.put("Alder", Arrays.asList("tot"));
        map.put("Kon", Arrays.asList("4"));
        map.put("Boendeform", Arrays.asList("SMAG"));
        map.put("Tid", Arrays.asList("2012"));

        // This request is performed by a dummy client which is set to English
        // (as specified above when creating the client). This means that if we receive a
        // response with Swedish text, we've used the fallback url.
        String response = client.post(client.getRootUrl() + table, QueryBuilder.build(map));

        assertTrue(response.contains("ålder"));
        assertTrue(response.contains("kön"));
        assertTrue(response.contains("boendeform"));
        assertTrue(response.contains("år"));
    }

    @Test
    public void postWithEmptyList() {
        DummyClient client = new DummyClient(new Locale("sv", "SE"));

        String table = "HE/HE0103/HE0103B/BefolkningAlder";

        Map<String, Collection<?>> inputMap = new HashMap<String, Collection<?>>();
        inputMap.put("Alder", Collections.EMPTY_LIST);

        String response = client.post(client.getRootUrl() + table, QueryBuilder.build(inputMap));

        assertTrue(response.contains("år"));
        assertFalse(response.contains("ålder"));
        assertFalse(response.contains("kön"));
        assertFalse(response.contains("boendeform"));
    }

    @Test(expected = SCBClientUrlNotFoundException.class)
    public void urlNotFoundException() {
        DummyClient client = new DummyClient();

        String response = client.get(client.getRootUrl() + "ABC/ABC/ABC");

        assertNull(response);
    }

    @Test(expected = SCBClientForbiddenException.class)
    public void forbiddenException() {
        // Need to use SCBClient here instead of DummyClient to reach
        // getRawData(String)-method
        SCBClient client = new SCBClient();

        // This call will result in a HTTP 403 response (forbidden) since the
        // response from this table is larger than the API allows (given all the available
        // inputs)
        String response = client.getRawData("NV/NV0119/IVPKNLonAr");<commit_msg>Fixed tests<commit_after>/*
 * Copyright 2016 <NAME>
 *
 * 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.github.dannil.scbjavaclient.client;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import com.github.dannil.scbjavaclient.exception.SCBClientForbiddenException;
import com.github.dannil.scbjavaclient.exception.SCBClientUrlNotFoundException;
import com.github.dannil.scbjavaclient.utility.QueryBuilder;

import org.apache.commons.io.FilenameUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class AbstractClientIT {

    private static class DummyClient extends AbstractClient {

        protected DummyClient() {
            super();
        }

        protected DummyClient(Locale locale) {
            super(locale);
        }

        @Override
        public String getUrl() {
            // TODO Auto-generated method stub
            return null;
        }

    }

    @Test
    public void toFallbackUrlGet() {
        DummyClient client = new DummyClient(new Locale("en", "US"));

        String url = client.getRootUrl() + "HE/HE0103/HE0103B/BefolkningAlder";

        // This request is performed by a dummy Client which is set to English
        // (as specified in the setup method). This means that if we receive a response
        // with Swedish text, we've used the fallback url.
        String response = client.get(url);

        assertTrue(response.contains("ålder"));
        assertTrue(response.contains("kön"));
        assertTrue(response.contains("boendeform"));
        assertTrue(response.contains("tabellinnehåll"));
        assertTrue(response.contains("år"));
    }

    @Test
    public void toFallbackUrlPost() {
        DummyClient client = new DummyClient(new Locale("en", "US"));

        String url = client.getRootUrl() + "HE/HE0103/HE0103B/BefolkningAlder";

        Map<String, Collection<?>> map = new HashMap<String, Collection<?>>();
        map.put("ContentsCode", Arrays.asList("HE0103D2"));
        map.put("Alder", Arrays.asList("tot"));
        map.put("Kon", Arrays.asList("4"));
        map.put("Boendeform", Arrays.asList("SMAG"));
        map.put("Tid", Arrays.asList("2012"));

        // This request is performed by a dummy client which is set to English
        // (as specified above when creating the client). This means that if we receive a
        // response with Swedish text, we've used the fallback url.
        String response = client.post(url, QueryBuilder.build(map));

        assertTrue(response.contains("ålder"));
        assertTrue(response.contains("kön"));
        assertTrue(response.contains("boendeform"));
        assertTrue(response.contains("år"));
    }

    @Test
    public void postWithEmptyList() {
        DummyClient client = new DummyClient(new Locale("sv", "SE"));

        String url = client.getRootUrl() + "HE/HE0103/HE0103B/BefolkningAlder";

        Map<String, Collection<?>> inputMap = new HashMap<String, Collection<?>>();
        inputMap.put("Alder", Collections.EMPTY_LIST);

        String response = client.post(url, QueryBuilder.build(inputMap));

        assertTrue(response.contains("år"));
        assertFalse(response.contains("ålder"));
        assertFalse(response.contains("kön"));
        assertFalse(response.contains("boendeform"));
    }

    @Test(expected = SCBClientUrlNotFoundException.class)
    public void urlNotFoundException() {
        SCBClient client = new SCBClient();

        String response = client.getRawData("ABC/ABC/ABC");

        assertNull(response);
    }

    @Test(expected = SCBClientForbiddenException.class)
    public void forbiddenException() {
        // Need to use SCBClient here instead of DummyClient to reach
        // getRawData(String)-method
        SCBClient client = new SCBClient();

        // This call will result in a HTTP 403 response (forbidden) since the
        // response from this table is larger than the API allows (given all the available
        // inputs)
        String response = client.getRawData("NV/NV0119/IVPKNLonAr");
{'fraction_non_alphanumeric': 0.11123227917121047, 'fraction_numerical': 0.010112025379200952, 'mean_word_length': 3.251158870627897, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '6963184', 'n_tokens_mistral': 2886, 'n_tokens_neox': 2787, 'n_words': 769}