-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
182 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
.../java/io/wcm/devops/conga/generator/plugins/handlebars/helper/DisallowPropertyHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* #%L | ||
* wcm.io | ||
* %% | ||
* Copyright (C) 2023 wcm.io | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
package io.wcm.devops.conga.generator.plugins.handlebars.helper; | ||
|
||
import java.io.IOException; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import com.github.jknack.handlebars.Options; | ||
|
||
import io.wcm.devops.conga.generator.spi.handlebars.HelperPlugin; | ||
import io.wcm.devops.conga.generator.spi.handlebars.context.HelperContext; | ||
|
||
/** | ||
* Handlebars helper that ensures a given property is not set/present. | ||
* If it is, an exception is thrown with the property name. | ||
* Optionally, a second parameter can be provided with a custom error message. | ||
*/ | ||
public final class DisallowPropertyHelper implements HelperPlugin<Object> { | ||
|
||
/** | ||
* Plugin/Helper name | ||
*/ | ||
public static final String NAME = "disallowProperty"; | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public Object apply(Object context, Options options, HelperContext pluginContext) throws IOException { | ||
if (isPropertyPresent(context, options)) { | ||
String errorMessage = "Disallowed property is set: " + context; | ||
if (options.params.length > 0 && options.params[0] != null) { | ||
errorMessage = options.params[0].toString(); | ||
} | ||
throw new IOException(errorMessage); | ||
} | ||
return null; | ||
} | ||
|
||
private boolean isPropertyPresent(Object propertyNameExpression, Options options) { | ||
if (propertyNameExpression == null) { | ||
return false; | ||
} | ||
String propertyName = propertyNameExpression.toString(); | ||
if (StringUtils.isBlank(propertyName)) { | ||
return false; | ||
} | ||
return options.get(propertyName) != null; | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...ain/resources/META-INF/services/io.wcm.devops.conga.generator.spi.handlebars.HelperPlugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...a/io/wcm/devops/conga/generator/plugins/handlebars/helper/DisallowPropertyHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* #%L | ||
* wcm.io | ||
* %% | ||
* Copyright (C) 2023 wcm.io | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
package io.wcm.devops.conga.generator.plugins.handlebars.helper; | ||
|
||
import static io.wcm.devops.conga.generator.plugins.handlebars.helper.TestUtils.assertHelper; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.wcm.devops.conga.generator.spi.handlebars.HelperPlugin; | ||
import io.wcm.devops.conga.generator.util.PluginManagerImpl; | ||
|
||
class DisallowPropertyHelperTest { | ||
|
||
private HelperPlugin<Object> helper; | ||
|
||
@SuppressWarnings("unchecked") | ||
@BeforeEach | ||
void setUp() { | ||
helper = new PluginManagerImpl().get(DisallowPropertyHelper.NAME, HelperPlugin.class); | ||
} | ||
|
||
@Test | ||
void testSetCase1() throws Exception { | ||
assertThrows(IOException.class, () -> { | ||
assertHelper(null, helper, "p1", new MockOptions() | ||
.withProperty("p1", "v1")); | ||
}); | ||
} | ||
|
||
@Test | ||
void testSetCase2() throws Exception { | ||
IOException ex = assertThrows(IOException.class, () -> { | ||
assertHelper(null, helper, "p1", new MockOptions("Custom Error Message") | ||
.withProperty("p1", "v1") | ||
.withProperty("p2", "v2") | ||
.withProperty("p3", "v3")); | ||
}); | ||
assertEquals("Custom Error Message", ex.getMessage()); | ||
} | ||
|
||
@Test | ||
void testNotSetCase1() throws Exception { | ||
assertHelper(null, helper, "p1", new MockOptions()); | ||
} | ||
|
||
@Test | ||
void testNotSetCase2() throws Exception { | ||
assertHelper(null, helper, "p1", new MockOptions("Custom Error Message")); | ||
} | ||
|
||
@Test | ||
void testNotSetCase3() throws Exception { | ||
assertHelper(null, helper, "p1", new MockOptions("Custom Error Message") | ||
.withProperty("p2", "v1")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters