/*
 * 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) Steve Springett. All Rights Reserved.
 */
package org.dependencytrack.model;

import org.junit.Assert;
import org.junit.Test;

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

public class VulnerabilityTest { 

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

    @Test
    public void testSeverity() {
        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));
        Assert.assertEquals(Severity.MEDIUM, vuln.getSeverity());
        vuln.setSeverity(Severity.HIGH);
        Assert.assertNull(vuln.getCvssV2Vector());
        Assert.assertNull(vuln.getCvssV2BaseScore());
        Assert.assertNull(vuln.getCvssV2ImpactSubScore());
        Assert.assertNull(vuln.getCvssV2ExploitabilitySubScore());
        Assert.assertNull(vuln.getCvssV3Vector());
        Assert.assertNull(vuln.getCvssV3BaseScore());
        Assert.assertNull(vuln.getCvssV3ImpactSubScore());
        Assert.assertNull(vuln.getCvssV3ExploitabilitySubScore());
        Assert.assertEquals(Severity.HIGH, vuln.getSeverity());
    } 

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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