summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authork.kedron <k.kedron@partner.samsung.com>2019-08-16 16:27:45 +0200
committerOfir Sonsino <ofir.sonsino@intl.att.com>2019-08-26 08:34:15 +0000
commit5a4e1827b867a2de46c14f32449b37d0ff60d1fd (patch)
tree4d38b78195c36fde5ca1342b97f97fba74f7a350
parenta0418bb757fbd35ac69131b1daaa6b0deeb1eb4b (diff)
Added unit tests fo MappingRulesValidator
Added a new unit tests. Checkstyle for MappingRulesValidator, VesStructureLoader. Sonar fixes -> adding a private constructor to ValidationUtils. Issue-ID: SDC-2327 Signed-off-by: Krystian Kedron <k.kedron@partner.samsung.com> Change-Id: Ib8199c2d76263dc96c6918ed57c1bfc6f223ee62
-rw-r--r--dcaedt_be/pom.xml12
-rw-r--r--dcaedt_be/src/main/java/org/onap/sdc/dcae/rule/editor/utils/ValidationUtils.java3
-rw-r--r--dcaedt_be/src/main/java/org/onap/sdc/dcae/rule/editor/validators/MappingRulesValidator.java51
-rw-r--r--dcaedt_be/src/main/java/org/onap/sdc/dcae/ves/VesStructureLoader.java3
-rw-r--r--dcaedt_be/src/test/java/org/onap/sdc/dcae/rule/editor/validators/MappingRulesValidatorTest.java236
5 files changed, 284 insertions, 21 deletions
diff --git a/dcaedt_be/pom.xml b/dcaedt_be/pom.xml
index 3c0e4b9..1688f4d 100644
--- a/dcaedt_be/pom.xml
+++ b/dcaedt_be/pom.xml
@@ -112,6 +112,18 @@
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>org.powermock</groupId>
+ <artifactId>powermock-module-junit4</artifactId>
+ <version>1.7.4</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.powermock</groupId>
+ <artifactId>powermock-api-mockito</artifactId>
+ <version>1.7.4</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<profiles>
<profile>
diff --git a/dcaedt_be/src/main/java/org/onap/sdc/dcae/rule/editor/utils/ValidationUtils.java b/dcaedt_be/src/main/java/org/onap/sdc/dcae/rule/editor/utils/ValidationUtils.java
index 74324d3..e70531f 100644
--- a/dcaedt_be/src/main/java/org/onap/sdc/dcae/rule/editor/utils/ValidationUtils.java
+++ b/dcaedt_be/src/main/java/org/onap/sdc/dcae/rule/editor/utils/ValidationUtils.java
@@ -27,6 +27,9 @@ public class ValidationUtils {
private static final String EXPLICIT_EMPTY = "\"\"";
+ private ValidationUtils() {
+ }
+
public static boolean validateNotEmpty(String value){
return StringUtils.isNoneBlank(value);
}
diff --git a/dcaedt_be/src/main/java/org/onap/sdc/dcae/rule/editor/validators/MappingRulesValidator.java b/dcaedt_be/src/main/java/org/onap/sdc/dcae/rule/editor/validators/MappingRulesValidator.java
index fd78561..75f7338 100644
--- a/dcaedt_be/src/main/java/org/onap/sdc/dcae/rule/editor/validators/MappingRulesValidator.java
+++ b/dcaedt_be/src/main/java/org/onap/sdc/dcae/rule/editor/validators/MappingRulesValidator.java
@@ -51,34 +51,45 @@ public class MappingRulesValidator implements IRuleElementValidator<MappingRules
valid = false;
errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.INVALID_RULE_FORMAT, "", "no rules found"));
}
+ // TODO consider using 'allMatch' which will stop on the first error
return rules.getRules().values().stream().map(r -> ruleValidator.validate(r, errors))
.reduce(true, (x,y) -> x && y) && valid;
- // TODO consider using 'allMatch' which will stop on the first error
}
- public boolean validateVersionAndType(MappingRules rules) {
- Map<String, Set<String>> supportedVersions = VesStructureLoader.getAvailableVersionsAndEventTypes();
- return ValidationUtils.validateNotEmpty(rules.getVersion()) && supportedVersions.containsKey(rules.getVersion()) && ValidationUtils.validateNotEmpty(rules.getEventType()) && supportedVersions.get(rules.getVersion()).contains(rules.getEventType());
- }
+ public boolean validateVersionAndType(MappingRules rules) {
+ Map<String, Set<String>> supportedVersions =
+ VesStructureLoader.getAvailableVersionsAndEventTypes();
+
+ return ValidationUtils.validateNotEmpty(rules.getVersion())
+ && supportedVersions.containsKey(rules.getVersion())
+ && ValidationUtils.validateNotEmpty(rules.getEventType())
+ && supportedVersions.get(rules.getVersion()).contains(rules.getEventType());
+ }
public boolean validateGroupDefinitions(MappingRules rules) {
- return rules.getRules().values().stream().allMatch(r -> ValidationUtils.validateNotEmpty(r.getGroupId()) && ValidationUtils.validateNotEmpty(r.getPhase()))
- && rules.getRules().values().stream().collect(Collectors.groupingBy(Rule::getGroupId, Collectors.mapping(Rule::getPhase, Collectors.toSet()))).values().stream().allMatch(p -> 1 == p.size());
+ return rules.getRules().values().stream().allMatch(r -> ValidationUtils.validateNotEmpty(r.getGroupId()) &&
+ ValidationUtils.validateNotEmpty(r.getPhase())) &&
+ rules.getRules().values().stream().collect(
+ Collectors.groupingBy(Rule::getGroupId, Collectors.mapping(Rule::getPhase, Collectors.toSet())))
+ .values().stream().allMatch(p -> 1 == p.size());
}
- public boolean validateTranslationPhaseNames(MappingRules rules, List<ResponseFormat> errors) {
- boolean valid = true;
- Set<String> phases = rules.getRules().values().stream().map(Rule::getPhase).collect(Collectors.toSet());
- if(phases.contains(rules.getEntryPhase())) {
- valid = false;
- errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.TRANSLATE_FAILED, null, "entry phase name already exists"));
- }
- if(phases.contains(rules.getPublishPhase())) {
- valid = false;
- errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.TRANSLATE_FAILED, null, "publish phase name already exists"));
- }
- return valid;
- }
+ public boolean validateTranslationPhaseNames(MappingRules rules, List<ResponseFormat> errors) {
+ boolean valid = true;
+ Set<String> phases =
+ rules.getRules().values().stream().map(Rule::getPhase).collect(Collectors.toSet());
+ if (phases.contains(rules.getEntryPhase())) {
+ valid = false;
+ errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.TRANSLATE_FAILED, null,
+ "entry phase name already exists"));
+ }
+ if (phases.contains(rules.getPublishPhase())) {
+ valid = false;
+ errors.add(ErrConfMgr.INSTANCE.getResponseFormat(ActionStatus.TRANSLATE_FAILED, null,
+ "publish phase name already exists"));
+ }
+ return valid;
+ }
}
diff --git a/dcaedt_be/src/main/java/org/onap/sdc/dcae/ves/VesStructureLoader.java b/dcaedt_be/src/main/java/org/onap/sdc/dcae/ves/VesStructureLoader.java
index 7c18b5b..9558b13 100644
--- a/dcaedt_be/src/main/java/org/onap/sdc/dcae/ves/VesStructureLoader.java
+++ b/dcaedt_be/src/main/java/org/onap/sdc/dcae/ves/VesStructureLoader.java
@@ -58,7 +58,8 @@ public class VesStructureLoader {
private VesStructureLoader() {
}
- @PostConstruct public void init() {
+ @PostConstruct
+ public void init() {
debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "VesStructureLoader: Trying to load json schemas");
String jettyBase = System.getProperty("jetty.base");
diff --git a/dcaedt_be/src/test/java/org/onap/sdc/dcae/rule/editor/validators/MappingRulesValidatorTest.java b/dcaedt_be/src/test/java/org/onap/sdc/dcae/rule/editor/validators/MappingRulesValidatorTest.java
new file mode 100644
index 0000000..dfe0960
--- /dev/null
+++ b/dcaedt_be/src/test/java/org/onap/sdc/dcae/rule/editor/validators/MappingRulesValidatorTest.java
@@ -0,0 +1,236 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * SDC
+ * ================================================================================
+ * Copyright (C) 2019 Samsung. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.sdc.dcae.rule.editor.validators;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import com.google.gson.GsonBuilder;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import org.onap.sdc.dcae.composition.restmodels.ruleeditor.ActionDeserializer;
+import org.onap.sdc.dcae.composition.restmodels.ruleeditor.BaseAction;
+import org.onap.sdc.dcae.composition.restmodels.ruleeditor.BaseCondition;
+import org.onap.sdc.dcae.composition.restmodels.ruleeditor.ConditionDeserializer;
+import org.onap.sdc.dcae.composition.restmodels.ruleeditor.MappingRules;
+import org.onap.sdc.dcae.composition.restmodels.ruleeditor.Rule;
+import org.onap.sdc.dcae.errormng.ErrorConfigurationLoader;
+import org.onap.sdc.dcae.errormng.ResponseFormat;
+import org.onap.sdc.dcae.errormng.ServiceException;
+import org.onap.sdc.dcae.ves.VesStructureLoader;
+
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+@RunWith(PowerMockRunner.class)
+public class MappingRulesValidatorTest {
+
+ private static final MappingRulesValidator validator = MappingRulesValidator.getInstance();
+ private List<ResponseFormat> errors;
+
+ private static final String FORMAT_ERROR_MESSAGE_ID = "SVC6035";
+ private static final String FORMAT_ERROR_TEXT = "Error - Rule format is invalid: %1.";
+ private static final String FORMAT_ERROR_VARIABLE_TEXT = "no rules found";
+
+ private static final String TRANSLATION_ERROR_MESSAGE_ID = "SVC6116";
+ private static final String TRANSLATION_ERROR_TEXT = "Translation failed. Reason: %1";
+ private static final String TRANSLATION_ERROR_ENTRY_VARIABLE_TEXT =
+ "entry phase name already exists";
+ private static final String TRANSLATION_ERROR_PUBLISH_VARIABLE_TEXT =
+ "publish phase name already exists";
+
+ private static final String BAD_RULE_VERSION = "1234";
+ private static final String RULE_VERSION = "4.1";
+ private static final String RULE_EVENT_TYPE = "syslogFields";
+ private static final String RULE_GROUP_ID = "4321";
+ private static final String RULE_PHASE = "test";
+
+ private static final String BASE_JSON =
+ String.format("{version:%s,eventType:%s,notifyId:.1.3.6.1.4.1.26878.200.2}", RULE_VERSION,
+ RULE_EVENT_TYPE);
+
+ private static final String RULE_OBJECT_TEMPLATE =
+ "{version:%s,eventType:%s,notifyId:.1.3.6.1.4.1.26878.200.2,description:newRule,"
+ + "actions:[{from:{state:closed,value:fromField,regex:\"\"},target:event.commonEventHeader.target,id:id,actionType:copy},"
+ + "{actionType:concat,id:id1,from:{state:closed,values:[{value:concat1},{value:_concat2}]},target:concatTargetField},"
+ + "{actionType:copy,id:id2,from:{state:open,value:extractFromHere,regex:\"([^:]*):.*\"},target:regexTargetField}],"
+ + "condition:{id:idc,level:0,name:elvis,left:\"${leftOperand}\",operator:contains,right:[rightOperand1,rightOperand2]},"
+ + "groupId: %s, phase:%s, entryPhase:%s, publishPhase:%s}";
+
+ private static final String RULE_JSON = createRuleJSON(null, null, null, null);
+
+ private static final String RULE_WITH_GROUP_JSON =
+ createRuleJSON(RULE_GROUP_ID, RULE_PHASE, null, null);
+
+ @BeforeClass
+ public static void setup() {
+ new ErrorConfigurationLoader(System.getProperty("user.dir") + "/src/main/webapp/WEB-INF");
+ }
+
+ @Before
+ public void prepare() {
+ errors = new ArrayList<>();
+ }
+
+ @Test
+ public void validateMappingRulesTest() {
+ // given
+ MappingRules mappingRules = createMappingRules();
+ // when - then
+ assertTrue(validator.validate(mappingRules, errors));
+ }
+
+ @Test
+ public void validateMappingRulesWithNoRulesTest() {
+ // given
+ MappingRules mappingRules = createMappingRulesWithoutRules();
+ // when
+ boolean result = validator.validate(mappingRules, errors);
+ ResponseFormat error = errors.get(0);
+ ServiceException exception = error.getRequestError().getServiceException();
+ // then
+ assertFalse(result);
+ assertEquals(400, error.getStatus().intValue());
+ assertEquals(FORMAT_ERROR_MESSAGE_ID, exception.getMessageId());
+ assertEquals(FORMAT_ERROR_TEXT, exception.getText());
+ assertEquals(FORMAT_ERROR_VARIABLE_TEXT, exception.getVariables()[0]);
+ }
+
+ @Test
+ @PrepareForTest(VesStructureLoader.class)
+ public void validateVersionAndTypeTest() {
+ // given
+ MappingRules mappingRules = createMappingRules();
+ // when
+ mockAvailableVersionsAndEventTypes(RULE_VERSION);
+ boolean shouldValidate = validator.validateVersionAndType(mappingRules);
+ mockAvailableVersionsAndEventTypes(BAD_RULE_VERSION);
+ boolean shouldNotValidate = validator.validateVersionAndType(mappingRules);
+ // then
+ assertTrue(shouldValidate);
+ assertFalse(shouldNotValidate);
+ }
+
+ @Test
+ public void validateGroupDefinitionsTest() {
+ // given
+ MappingRules mappingRules = createMappingRules();
+ MappingRules mappingRulesWithGroup = createMappingRulesWithGroup();
+ // then
+ assertFalse(validator.validateGroupDefinitions(mappingRules));
+ assertTrue(validator.validateGroupDefinitions(mappingRulesWithGroup));
+ }
+
+ @Test
+ public void validateTranslationPhaseNamesTest() {
+ // given
+ MappingRules mappingRules = createMappingRulesWithGroup();
+
+ Rule rule = createRule(createRuleJSON(RULE_GROUP_ID, RULE_PHASE, RULE_PHASE, null));
+ MappingRules mappingRulesWithEntryPhase = new MappingRules(rule);
+ rule = createRule(createRuleJSON(RULE_GROUP_ID, RULE_PHASE, null, RULE_PHASE));
+ MappingRules mappingRulesWithPublishPhase = new MappingRules(rule);
+
+ // when
+ boolean normalRulesValidation =
+ validator.validateTranslationPhaseNames(mappingRules, errors);
+
+ boolean rulesWithTheSameEntryPhase =
+ validator.validateTranslationPhaseNames(mappingRulesWithEntryPhase, errors);
+ ResponseFormat errorEntryPhase = errors.get(0);
+ ServiceException exceptionEntryPhase =
+ errorEntryPhase.getRequestError().getServiceException();
+
+ boolean rulesWithTheSamePublishPhase =
+ validator.validateTranslationPhaseNames(mappingRulesWithPublishPhase, errors);
+ ResponseFormat errorPublishPhase = errors.get(1);
+ ServiceException exceptionPublishPhase =
+ errorPublishPhase.getRequestError().getServiceException();
+ // then
+ assertTrue(normalRulesValidation);
+
+ assertFalse(rulesWithTheSameEntryPhase);
+ assertEquals(400, errorEntryPhase.getStatus().intValue());
+ assertEquals(TRANSLATION_ERROR_MESSAGE_ID, exceptionEntryPhase.getMessageId());
+ assertEquals(TRANSLATION_ERROR_TEXT, exceptionEntryPhase.getText());
+ assertEquals(TRANSLATION_ERROR_ENTRY_VARIABLE_TEXT, exceptionEntryPhase.getVariables()[0]);
+
+ assertFalse(rulesWithTheSamePublishPhase);
+ assertEquals(400, errorPublishPhase.getStatus().intValue());
+ assertEquals(TRANSLATION_ERROR_MESSAGE_ID, exceptionPublishPhase.getMessageId());
+ assertEquals(TRANSLATION_ERROR_TEXT, exceptionPublishPhase.getText());
+ assertEquals(TRANSLATION_ERROR_PUBLISH_VARIABLE_TEXT,
+ exceptionPublishPhase.getVariables()[0]);
+ }
+
+ private void mockAvailableVersionsAndEventTypes(String version) {
+ PowerMockito.mockStatic(VesStructureLoader.class);
+ Map<String, Set<String>> availableVersionsAndEventTypes = new HashMap<>();
+ Set<String> eventTypes = new HashSet<>();
+ eventTypes.add(RULE_EVENT_TYPE);
+ availableVersionsAndEventTypes.put(version, eventTypes);
+ PowerMockito.when(VesStructureLoader.getAvailableVersionsAndEventTypes())
+ .thenReturn(availableVersionsAndEventTypes);
+ }
+
+ private Rule createRule(String body) {
+ GsonBuilder gsonBuilder = new GsonBuilder();
+ gsonBuilder.registerTypeAdapter(BaseAction.class, new ActionDeserializer());
+ gsonBuilder.registerTypeAdapter(BaseCondition.class, new ConditionDeserializer());
+ return gsonBuilder.create().fromJson(body, Rule.class);
+ }
+
+ private MappingRules createMappingRulesWithoutRules() {
+ GsonBuilder gsonBuilder = new GsonBuilder();
+ gsonBuilder.registerTypeAdapter(BaseAction.class, new ActionDeserializer());
+ gsonBuilder.registerTypeAdapter(BaseCondition.class, new ConditionDeserializer());
+ return gsonBuilder.create().fromJson(BASE_JSON, MappingRules.class);
+ }
+
+ private MappingRules createMappingRules() {
+ Rule rule = createRule(RULE_JSON);
+ return new MappingRules(rule);
+ }
+
+ private MappingRules createMappingRulesWithGroup() {
+ Rule rule = createRule(RULE_WITH_GROUP_JSON);
+ return new MappingRules(rule);
+ }
+
+ private static String createRuleJSON(String ruleGroupId, String rulePhase,
+ String ruleEntryPhase, String rulePublishPhase) {
+ return String.format(RULE_OBJECT_TEMPLATE, RULE_VERSION, RULE_EVENT_TYPE, ruleGroupId,
+ rulePhase, ruleEntryPhase, rulePublishPhase);
+ }
+}