/*
 * This file is part of Dependency-Track.
 *
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * Copyright (c) OWASP Foundation. All Rights Reserved.
 */
package org.dependencytrack.model;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

class VulnerabilityTest {

    @Test
    void testId() {
        Vulnerability vuln = new Vulnerability();
        vuln.setId(111L);
        Assertions.assertEquals(111L, vuln.getId());
    } 

    @Test
    void testSeverity() {
        // NB: Before https://github.com/DependencyTrack/dependency-track/issues/2474,
        // severity was computed in the getter based on the provided scores.
        Vulnerability vuln = new Vulnerability();
        vuln.setCvssV2Vector("Vector");
        vuln.setCvssV2BaseScore(new BigDecimal(5.0));
        vuln.setCvssV2ImpactSubScore(new BigDecimal(5.1));
        vuln.setCvssV2ExploitabilitySubScore(new BigDecimal(5.2));
        vuln.setCvssV3Vector("Vector");
        vuln.setCvssV3BaseScore(new BigDecimal(6.0));
        vuln.setCvssV3ImpactSubScore(new BigDecimal(6.1));
        vuln.setCvssV3ExploitabilitySubScore(new BigDecimal(6.2));
        Assertions.assertNull(vuln.getSeverity());

        // NB: Before https://github.com/DependencyTrack/dependency-track/issues/2474,
        // calling setSeverity cleared all score and vector fields.
        vuln.setSeverity(Severity.HIGH);
        Assertions.assertNotNull(vuln.getCvssV2Vector());
        Assertions.assertNotNull(vuln.getCvssV2BaseScore());
        Assertions.assertNotNull(vuln.getCvssV2ImpactSubScore());
        Assertions.assertNotNull(vuln.getCvssV2ExploitabilitySubScore());
        Assertions.assertNotNull(vuln.getCvssV3Vector());
        Assertions.assertNotNull(vuln.getCvssV3BaseScore());
        Assertions.assertNotNull(vuln.getCvssV3ImpactSubScore());
        Assertions.assertNotNull(vuln.getCvssV3ExploitabilitySubScore());
        Assertions.assertEquals(Severity.HIGH, vuln.getSeverity());
    } 

    @Test
    void testVulnId() {
        Vulnerability vuln = new Vulnerability();
        vuln.setVulnId("CVE-2019-0000");
        Assertions.assertEquals("CVE-2019-0000", vuln.getVulnId());
    } 

    @Test
    void testDescription() {
        Vulnerability vuln = new Vulnerability();
        vuln.setDescription("My description");
        Assertions.assertEquals("My description", vuln.getDescription());
    }

    @Test
    void testRecommendation() {
        Vulnerability vuln = new Vulnerability();
        vuln.setRecommendation("My recommendation");
        Assertions.assertEquals("My recommendation", vuln.getRecommendation());
    } 

    @Test
    void testReferences() {
        Vulnerability vuln = new Vulnerability();
        vuln.setReferences("My references");
        Assertions.assertEquals("My references", vuln.getReferences());
    } 

    @Test
    void testCredits() {
        Vulnerability vuln = new Vulnerability();
        vuln.setCredits("My credits");
        Assertions.assertEquals("My credits", vuln.getCredits());
    } 

    @Test
    void testCreated() {
        Date date = new Date();
        Vulnerability vuln = new Vulnerability();
        vuln.setCreated(date);
        Assertions.assertEquals(date, vuln.getCreated());
    } 

    @Test
    void testPublished() {
        Date date = new Date();
        Vulnerability vuln = new Vulnerability();
        vuln.setPublished(date);
        Assertions.assertEquals(date, vuln.getPublished());
    }

    @Test
    void testUpdated() {
        Date date = new Date();
        Vulnerability vuln = new Vulnerability();
        vuln.setUpdated(date);
        Assertions.assertEquals(date, vuln.getUpdated());
    } 

    @Test
    void testVulnerableVersions() {
        Vulnerability vuln = new Vulnerability();
        vuln.setVulnerableVersions("Vulnerable versions");
        Assertions.assertEquals("Vulnerable versions", vuln.getVulnerableVersions());
    } 

    @Test
    void testPatchedVersions() {
        Vulnerability vuln = new Vulnerability();
        vuln.setPatchedVersions("Patched versions");
        Assertions.assertEquals("Patched versions", vuln.getPatchedVersions());
    } 

    @Test
    void testSource() {
        Vulnerability vuln = new Vulnerability();
        vuln.setSource("My source");
        Assertions.assertEquals("My source", vuln.getSource());
        vuln.setSource(Vulnerability.Source.NPM);
        Assertions.assertEquals("NPM", vuln.getSource());
    } 

    @Test
    void testTitle() {
        Vulnerability vuln = new Vulnerability();
        vuln.setTitle("My title");
        Assertions.assertEquals("My title", vuln.getTitle());
    } 

    @Test
    void testSubTitle() {
        Vulnerability vuln = new Vulnerability();
        vuln.setSubTitle("My subtitle");
        Assertions.assertEquals("My subtitle", vuln.getSubTitle());
    } 

    @Test
    void testCwe() {
        Vulnerability vuln = new Vulnerability();
        List<Integer> cwes = List.of(80);
        vuln.setCwes(cwes);
        Assertions.assertEquals(cwes.get(0), vuln.getCwes().get(0));
    } 

    @Test
    void testCvssV2BaseScore() {
        Vulnerability vuln = new Vulnerability();
        vuln.setCvssV2BaseScore(new BigDecimal(5.0));
        Assertions.assertEquals(new BigDecimal(5.0), vuln.getCvssV2BaseScore());
    } 

    @Test
    void testCvssV2ImpactSubScore() {
        Vulnerability vuln = new Vulnerability();
        vuln.setCvssV2ImpactSubScore(new BigDecimal(5.1));
        Assertions.assertEquals(new BigDecimal(5.1), vuln.getCvssV2ImpactSubScore());
    } 

    @Test
    void testCvssV2ExploitabilitySubScore() {
        Vulnerability vuln = new Vulnerability();
        vuln.setCvssV2ExploitabilitySubScore(new BigDecimal(5.2));
        Assertions.assertEquals(new BigDecimal(5.2), vuln.getCvssV2ExploitabilitySubScore());
    }

    @Test
    void testCvssV2Vector() {
        Vulnerability vuln = new Vulnerability();
        vuln.setCvssV2Vector("CVSS vector");
        Assertions.assertEquals("CVSS vector", vuln.getCvssV2Vector());
    }

    @Test
    void testCvssV3BaseScore() {
        Vulnerability vuln = new Vulnerability();
        vuln.setCvssV3BaseScore(new BigDecimal(6.0));
        Assertions.assertEquals(new BigDecimal(6.0), vuln.getCvssV3BaseScore());
    } 

    @Test
    void testCvssV3ImpactSubScore() {
        Vulnerability vuln = new Vulnerability();
        vuln.setCvssV3ImpactSubScore(new BigDecimal(6.1));
        Assertions.assertEquals(new BigDecimal(6.1), vuln.getCvssV3ImpactSubScore());
    } 

    @Test
    void testCvssV3ExploitabilitySubScore() {
        Vulnerability vuln = new Vulnerability();
        vuln.setCvssV3ExploitabilitySubScore(new BigDecimal(6.2));
        Assertions.assertEquals(new BigDecimal(6.2), vuln.getCvssV3ExploitabilitySubScore());
    } 

    @Test
    void testCvssV3Vector() {
        Vulnerability vuln = new Vulnerability();
        vuln.setCvssV3Vector("CVSS vector");
        Assertions.assertEquals("CVSS vector", vuln.getCvssV3Vector());
    }

    @Test
    void testComponents() {
        List<Component> components = new ArrayList<>();
        Component component = new Component();
        components.add(component);
        Vulnerability vuln = new Vulnerability();
        vuln.setComponents(components);
        Assertions.assertEquals(1, vuln.getComponents().size());
        Assertions.assertEquals(component, vuln.getComponents().get(0));
    } 

    @Test
    void testUuid() {
        UUID uuid = UUID.randomUUID();
        Vulnerability vuln = new Vulnerability();
        vuln.setUuid(uuid);
        Assertions.assertEquals(uuid.toString(), vuln.getUuid().toString());
    }
} 
