|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Red Hat, Inc. |
| 3 | + * Distributed under license by Red Hat, Inc. All rights reserved. |
| 4 | + * This program is made available under the terms of the |
| 5 | + * Eclipse Public License v2.0 which accompanies this distribution, |
| 6 | + * and is available at http://www.eclipse.org/legal/epl-v20.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Red Hat, Inc. - initial API and implementation |
| 10 | + ******************************************************************************/ |
| 11 | + |
| 12 | +package org.jboss.tools.intellij.image; |
| 13 | + |
| 14 | +import io.github.guacsec.trustifyda.api.v5.Severity; |
| 15 | +import io.github.guacsec.trustifyda.api.v5.Source; |
| 16 | +import io.github.guacsec.trustifyda.api.v5.SourceSummary; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertNull; |
| 21 | + |
| 22 | +/** |
| 23 | + * Tests for {@link DockerfileAnnotator#getSeverity(Source)} severity cascade logic. |
| 24 | + */ |
| 25 | +public class DockerfileAnnotatorTest { |
| 26 | + |
| 27 | + private static Source createSourceWithSummary(Integer critical, Integer high, Integer medium, Integer low, Integer unknown) { |
| 28 | + var summary = new SourceSummary(); |
| 29 | + summary.setCritical(critical); |
| 30 | + summary.setHigh(high); |
| 31 | + summary.setMedium(medium); |
| 32 | + summary.setLow(low); |
| 33 | + summary.setUnknown(unknown); |
| 34 | + var source = new Source(); |
| 35 | + source.setSummary(summary); |
| 36 | + return source; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Verifies that critical severity takes precedence over all others. |
| 41 | + */ |
| 42 | + @Test |
| 43 | + public void testGetSeverityCritical() { |
| 44 | + Source source = createSourceWithSummary(1, 2, 3, 4, 5); |
| 45 | + assertEquals(Severity.CRITICAL, DockerfileAnnotator.getSeverity(source)); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Verifies that high severity is returned when no critical vulnerabilities exist. |
| 50 | + */ |
| 51 | + @Test |
| 52 | + public void testGetSeverityHigh() { |
| 53 | + Source source = createSourceWithSummary(0, 1, 2, 3, 4); |
| 54 | + assertEquals(Severity.HIGH, DockerfileAnnotator.getSeverity(source)); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Verifies that medium severity is returned when no critical or high vulnerabilities exist. |
| 59 | + */ |
| 60 | + @Test |
| 61 | + public void testGetSeverityMedium() { |
| 62 | + Source source = createSourceWithSummary(0, 0, 1, 2, 3); |
| 63 | + assertEquals(Severity.MEDIUM, DockerfileAnnotator.getSeverity(source)); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Verifies that low severity is returned when no critical, high, or medium vulnerabilities exist. |
| 68 | + */ |
| 69 | + @Test |
| 70 | + public void testGetSeverityLow() { |
| 71 | + Source source = createSourceWithSummary(0, 0, 0, 1, 2); |
| 72 | + assertEquals(Severity.LOW, DockerfileAnnotator.getSeverity(source)); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Verifies that unknown severity is returned when only unknown-severity vulnerabilities exist. |
| 77 | + */ |
| 78 | + @Test |
| 79 | + public void testGetSeverityUnknown() { |
| 80 | + Source source = createSourceWithSummary(0, 0, 0, 0, 1); |
| 81 | + assertEquals(Severity.UNKNOWN, DockerfileAnnotator.getSeverity(source)); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Verifies that null is returned when no vulnerabilities of any severity exist. |
| 86 | + */ |
| 87 | + @Test |
| 88 | + public void testGetSeverityNone() { |
| 89 | + Source source = createSourceWithSummary(0, 0, 0, 0, 0); |
| 90 | + assertNull(DockerfileAnnotator.getSeverity(source)); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Verifies that null is returned when summary is null. |
| 95 | + */ |
| 96 | + @Test |
| 97 | + public void testGetSeverityNullSummary() { |
| 98 | + var source = new Source(); |
| 99 | + assertNull(DockerfileAnnotator.getSeverity(source)); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Verifies that null is returned when all severity counts are null. |
| 104 | + */ |
| 105 | + @Test |
| 106 | + public void testGetSeverityNullCounts() { |
| 107 | + Source source = createSourceWithSummary(null, null, null, null, null); |
| 108 | + assertNull(DockerfileAnnotator.getSeverity(source)); |
| 109 | + } |
| 110 | +} |
0 commit comments