From cad33e5d92653debf181bfc9c8d27728a37afb30 Mon Sep 17 00:00:00 2001 From: kingthorin Date: Wed, 27 Nov 2024 13:59:42 -0500 Subject: [PATCH] commonlib: Add methods to get filename & policy name for PolicyTag tags Signed-off-by: kingthorin --- .../zaproxy/addon/commonlib/PolicyTag.java | 36 ++++++++++++++----- .../addon/commonlib/PolicyTagUnitTest.java | 20 +++++++++-- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/addOns/commonlib/src/main/java/org/zaproxy/addon/commonlib/PolicyTag.java b/addOns/commonlib/src/main/java/org/zaproxy/addon/commonlib/PolicyTag.java index 26af6ce640..721303da0d 100644 --- a/addOns/commonlib/src/main/java/org/zaproxy/addon/commonlib/PolicyTag.java +++ b/addOns/commonlib/src/main/java/org/zaproxy/addon/commonlib/PolicyTag.java @@ -29,25 +29,45 @@ * @since 1.29.0 */ public enum PolicyTag { - DEV_CICD, - DEV_STD, - DEV_FULL, - QA_STD, - QA_FULL, - SEQUENCE, - API; + DEV_CICD("Dev CICD.policy", "Developer CI/CD", "scanpolicies"), + DEV_STD("Dev Standard.policy", "Developer Standard", "scanpolicies"), + DEV_FULL("Dev Full.policy", "Developer Full", "scanpolicies"), + QA_STD("QA Standard.policy", "QA Standard", "scanpolicies"), + QA_FULL("QA Full.policy", "QA Full", "scanpolicies"), + API("API.policy", "API", "scanpolicies"), + + SEQUENCE("Sequence.policy", "Sequence", "sequence"); protected static final String PREFIX = "POLICY_"; + private final String tag; + private final String fileName; + private final String policyName; + private final String addonId; - private PolicyTag() { + private PolicyTag(String fileName, String policyName, String addonId) { this.tag = PREFIX + this.name(); + this.fileName = fileName; + this.policyName = policyName; + this.addonId = addonId; } public String getTag() { return this.tag; } + public String getFileName() { + return fileName; + } + + public String getPolicyName() { + return policyName; + } + + public String getAddonId() { + return addonId; + } + public static List getAllTags() { return Stream.of(PolicyTag.values()).map(PolicyTag::getTag).collect(Collectors.toList()); } diff --git a/addOns/commonlib/src/test/java/org/zaproxy/addon/commonlib/PolicyTagUnitTest.java b/addOns/commonlib/src/test/java/org/zaproxy/addon/commonlib/PolicyTagUnitTest.java index 58022ac5e5..9abbb06006 100644 --- a/addOns/commonlib/src/test/java/org/zaproxy/addon/commonlib/PolicyTagUnitTest.java +++ b/addOns/commonlib/src/test/java/org/zaproxy/addon/commonlib/PolicyTagUnitTest.java @@ -20,7 +20,9 @@ package org.zaproxy.addon.commonlib; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.in; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.startsWith; @@ -33,19 +35,31 @@ class PolicyTagUnitTest { + private static final String FILE_EXT = ".policy"; + private static final List CURRENT_ADDONS = List.of("scanpolicies", "sequence"); + @ParameterizedTest @EnumSource(PolicyTag.class) void shouldHaveAllTagsStartingWithPolicyyUnderscoreInCapsEnumNamesWithout(PolicyTag tag) { // Given / When / Then + assertThat( + tag.name(), + describedAs( + "Enum element name should not start with prefix", + not(startsWith(PolicyTag.PREFIX)))); assertThat( tag.getTag(), describedAs( "Tag should start with expected prefix", is(startsWith(PolicyTag.PREFIX)))); assertThat( - tag.name(), + tag.getFileName(), describedAs( - "Enum element name should not start with prefix", - not(startsWith(PolicyTag.PREFIX)))); + "Enum filenames should all end with '.policy'", is(endsWith(FILE_EXT)))); + assertThat( + tag.getAddonId(), + describedAs( + "Enum values should be attributed to expected addons", + is(in(CURRENT_ADDONS)))); } @Test