<commit_before>/*
 * Zed Attack Proxy (ZAP) and its related class files.
 * 
 * ZAP is an HTTP/HTTPS proxy for assessing web application security.
 * 
 * Copyright 2018 The ZAP Development Team
 * 
 * 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.zaproxy.zap.model;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.log4j.varia.NullAppender;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Validates that the translated vulnerabilities files have expected content (e.g. same number of vulnerabilities as the source
 * file).
 */
public class ValidateTranslatedVulnerabilitiesFilesUnitTest {

    private static final Path DIRECTORY = Paths.get("src/lang");
    private static final String FILE_NAME = "vulnerabilities";
    private static final String FILE_EXTENSION = ".xml";
    private static final String SOURCE_FILE = FILE_NAME + FILE_EXTENSION;

    private VulnerabilitiesLoader loader = new VulnerabilitiesLoader(DIRECTORY, FILE_NAME, FILE_EXTENSION);

    @BeforeClass
    public static void suppressLogging() {
        Logger.getRootLogger().addAppender(new NullAppender());
    }

    @Test
    public void shouldThrownExceptionIfDirectoryIsNull() {
        // Given
        Map<String, Vulnerability> mainVulns = loadFile(SOURCE_FILE);
        List<String> translations = loader.getListOfVulnerabilitiesFiles();
        translations.remove(SOURCE_FILE);
        // When
        for (String file : translations) {
            Map<String, Vulnerability> vulns = loadFile(file);
            // Then
            assertThat(file, vulns.values(), hasSize(mainVulns.size()));
            mainVulns.forEach((k, v) -> {
                Vulnerability vuln = vulns.get(k);
                assertThat("Missing " + k + " in " + file, vuln, is(notNullValue()));
                assertThat(
                        "Wrong number of references in " + file + " for " + k,
                        vuln.getReferences(),
                        hasSize(v.getReferences().size()));
            });
        }
    }

    private Map<String, Vulnerability> loadFile(String fileName) {
        Map<String, Vulnerability> map = new HashMap<>();
        for (Vulnerability vulnerability : loader.loadVulnerabilitiesFile(DIRECTORY.resolve(fileName))) {
            map.put(vulnerability.getId(), vulnerability);
        }
        return map;
    }
}<commit_msg>Assert vulnerabilities files are properly loaded<commit_after>/*
 * Zed Attack Proxy (ZAP) and its related class files.
 * 
 * ZAP is an HTTP/HTTPS proxy for assessing web application security.
 * 
 * Copyright 2018 The ZAP Development Team
 * 
 * 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.zaproxy.zap.model;

import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.log4j.varia.NullAppender;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Validates that the translated vulnerabilities files have expected content (e.g. same number of vulnerabilities as the source
 * file).
 */
public class ValidateTranslatedVulnerabilitiesFilesUnitTest {

    private static final Path DIRECTORY = Paths.get("src/lang");
    private static final String FILE_NAME = "vulnerabilities";
    private static final String FILE_EXTENSION = ".xml";
    private static final String SOURCE_FILE = FILE_NAME + FILE_EXTENSION;

    private VulnerabilitiesLoader loader = new VulnerabilitiesLoader(DIRECTORY, FILE_NAME, FILE_EXTENSION);

    @BeforeClass
    public static void suppressLogging() {
        Logger.getRootLogger().addAppender(new NullAppender());
    }

    @Test
    public void shouldLoadAllVulnerabilitiesFilesAvailable() {
        // Given
        Map<String, Vulnerability> mainVulns = loadFile(SOURCE_FILE);
        List<String> translations = loader.getListOfVulnerabilitiesFiles();
        translations.remove(SOURCE_FILE);
        // When
        for (String file : translations) {
            Map<String, Vulnerability> vulns = loadFile(file);
            // Then
            assertThat(file, vulns.values(), hasSize(mainVulns.size()));
            mainVulns.forEach((k, v) -> {
                Vulnerability vuln = vulns.get(k);
                assertThat("Missing " + k + " in " + file, vuln, is(notNullValue()));
                assertThat(
                        "Wrong number of references in " + file + " for " + k,
                        vuln.getReferences(),
                        hasSize(v.getReferences().size()));
            });
        }
    }

    private Map<String, Vulnerability> loadFile(String fileName) {
        List<Vulnerability> vulnerabilities = loader.loadVulnerabilitiesFile(DIRECTORY.resolve(fileName));
        assertThat("File " + fileName + " is not wellformed.", vulnerabilities, is(notNullValue()));

        Map<String, Vulnerability> map = new HashMap<>();
        for (Vulnerability vulnerability : vulnerabilities) {
            map.put(vulnerability.getId(), vulnerability);
        }
        return map;
    }
}
{'fraction_non_alphanumeric': 0.09546974764366069, 'fraction_numerical': 0.0030404378230465185, 'mean_word_length': 3.0736842105263156, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '142612', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1779, 'n_words': 539}