diff options
author | Pavel Aharoni <pa0916@att.com> | 2017-12-13 17:01:19 +0200 |
---|---|---|
committer | Pavel Aharoni <pa0916@att.com> | 2017-12-13 17:01:19 +0200 |
commit | 7dd51a2e187a6cfea05ed46004a25152d687197f (patch) | |
tree | 55becadeb979807820a71f2dc6cf536a97d51f9b /src | |
parent | 05b2a83f8c47883fba48b1d8c2586752aef9192f (diff) |
sdc-tosca tests change and refactoring
Change-Id: Ibffb597a5197675499c7fe544fba953065f2f9bf
Issue-ID: SDC-752
Signed-off-by: Pavel Aharoni <pa0916@att.com>
Diffstat (limited to 'src')
6 files changed, 149 insertions, 59 deletions
diff --git a/src/main/java/org/openecomp/sdc/tosca/parser/config/ConfigurationManager.java b/src/main/java/org/openecomp/sdc/tosca/parser/config/ConfigurationManager.java index 4e09222..72c06ed 100644 --- a/src/main/java/org/openecomp/sdc/tosca/parser/config/ConfigurationManager.java +++ b/src/main/java/org/openecomp/sdc/tosca/parser/config/ConfigurationManager.java @@ -18,12 +18,9 @@ public class ConfigurationManager { private static final String CONFIGURATION_DIR = "config/"; private static volatile ConfigurationManager instance; -// private Configuration configuration; -// private ErrorConfiguration errorConfiguration; - - Map<String, Object> configurations = new HashMap<String, Object>(); + Map<String, Object> configurations = new HashMap<String, Object>(); private ConfigurationManager() { initialConfigObjectsFromFiles(); @@ -40,18 +37,26 @@ public class ConfigurationManager { configurations.put(clazz.getSimpleName(), object); } + private <T> void loadConfigurationClass(Class<T> clazz, String fileName) { + T object = getObjectFromYaml(clazz,fileName); + configurations.put(clazz.getSimpleName(), object); + } + + public static <T> T getObjectFromYaml(Class<T> className) { + return getObjectFromYaml(className, null); + } - public <T> T getObjectFromYaml(Class<T> className) { + public static <T> T getObjectFromYaml(Class<T> className, String fileName) { - String configFileName = calculateFileName(className); + String configFileName = fileName != null ? fileName : calculateFileName(className); ; URL url = Resources.getResource(CONFIGURATION_DIR + configFileName); String configFileContents = null; try { configFileContents = Resources.toString(url, Charsets.UTF_8); } catch (IOException e) { - log.error("ConfigurationManager - Failed to load configuration file"); + log.error("ConfigurationManager - Failed to load configuration file {}", configFileName, e); } YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter(); T object = yamlToObjectConverter.convertFromString(configFileContents, className); @@ -61,13 +66,13 @@ public class ConfigurationManager { public static ConfigurationManager getInstance() { - // if (instance == null) { - // synchronized (ConfigurationManager.class) { - // if (instance == null) { + if (instance == null) { + synchronized (ConfigurationManager.class) { + if (instance == null) { instance = new ConfigurationManager(); - // } - // } - // } + } + } + } return instance; } @@ -107,7 +112,9 @@ public class ConfigurationManager { public JtoscaValidationIssueConfiguration getJtoscaValidationIssueConfiguration() { return (JtoscaValidationIssueConfiguration) configurations.get((JtoscaValidationIssueConfiguration.class.getSimpleName())); } - + public void setJtoscaValidationIssueConfiguration(String fileName) { + loadConfigurationClass(JtoscaValidationIssueConfiguration.class, fileName); + } public Configuration getConfiguration() { return (Configuration) configurations.get((Configuration.class.getSimpleName())); } diff --git a/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcToscaParserFactory.java b/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcToscaParserFactory.java index c9cac1c..f1a03b7 100644 --- a/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcToscaParserFactory.java +++ b/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcToscaParserFactory.java @@ -18,17 +18,13 @@ import org.slf4j.LoggerFactory; public class SdcToscaParserFactory {
private static Logger log = LoggerFactory.getLogger(SdcToscaParserFactory.class.getName());
-
+
+ private static ConfigurationManager configurationManager;
private static volatile SdcToscaParserFactory instance;
- private static Configuration configuration;
- private static ErrorConfiguration errorConfiguration;
- private static JtoscaValidationIssueConfiguration jtoscaValidationIssueConfiguration;
private List<JToscaValidationIssue> criticalExceptions = new ArrayList<>();
private List<JToscaValidationIssue> warningExceptions = new ArrayList<>();
private List<JToscaValidationIssue> notAnalyzadExceptions = new ArrayList<>();
- private SdcToscaParserFactory() {
-
- }
+ private SdcToscaParserFactory() {}
/**
* Get an SdcToscaParserFactory instance.
@@ -39,15 +35,17 @@ public class SdcToscaParserFactory { synchronized (SdcToscaParserFactory.class) {
if (instance == null) {
instance = new SdcToscaParserFactory();
- configuration = ConfigurationManager.getInstance().getConfiguration();
- errorConfiguration = ConfigurationManager.getInstance().getErrorConfiguration();
- jtoscaValidationIssueConfiguration = ConfigurationManager.getInstance().getJtoscaValidationIssueConfiguration();
+ configurationManager = ConfigurationManager.getInstance();
}
}
}
return instance;
}
+ public static void setConfigurationManager(ConfigurationManager configurationManager) {
+ SdcToscaParserFactory.configurationManager = configurationManager;
+ }
+
/**
* Get an ISdcCsarHelper object for this CSAR file.
*
@@ -85,8 +83,7 @@ public class SdcToscaParserFactory { try {
handleErrorsByTypes(csarPath, cSarConformanceLevel);
} catch (JToscaException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ throwSdcToscaParserException(e);
}
return sdcCsarHelperImpl;
}
@@ -95,7 +92,7 @@ public class SdcToscaParserFactory { private void handleErrorsByTypes(String csarPath, String cSarConformanceLevel) throws JToscaException {
clearValidationIssuesLists();
for(JToscaValidationIssue toscaValidationIssue : ThreadLocalsHolder.getCollector().getValidationIssues().values()){
- List<JToscaValidationIssueInfo> issueInfos = jtoscaValidationIssueConfiguration.getValidationIssues().get(toscaValidationIssue.getCode());
+ List<JToscaValidationIssueInfo> issueInfos = configurationManager.getJtoscaValidationIssueConfiguration().getValidationIssues().get(toscaValidationIssue.getCode());
if(issueInfos != null && !issueInfos.isEmpty()){
JToscaValidationIssueInfo issueInfo = null;
issueInfo = issueInfos.stream()
@@ -167,7 +164,7 @@ public class SdcToscaParserFactory { private void validateCsarVersion(String cSarVersion) throws SdcToscaParserException {
- ConformanceLevel level = configuration.getConformanceLevel();
+ ConformanceLevel level = configurationManager.getConfiguration().getConformanceLevel();
String minVersion = level.getMinVersion();
String maxVersion = level.getMaxVersion();
if (cSarVersion != null) {
@@ -188,13 +185,15 @@ public class SdcToscaParserFactory { return false;
}
private void throwConformanceLevelException(String minVersion, String maxVersion) throws SdcToscaParserException {
- ErrorInfo errorInfo = errorConfiguration.getErrorInfo(SdcToscaParserErrors.CONFORMANCE_LEVEL_ERROR.toString());
+ ErrorInfo errorInfo = configurationManager.getErrorConfiguration().getErrorInfo(SdcToscaParserErrors.CONFORMANCE_LEVEL_ERROR.toString());
throw new SdcToscaParserException(String.format(errorInfo.getMessage(), minVersion, maxVersion), errorInfo.getCode());
}
private void throwSdcToscaParserException(JToscaException e) throws SdcToscaParserException {
- ErrorInfo errorInfo = errorConfiguration.getErrorInfo(SdcToscaParserErrors.getSdcErrorByJToscaError(JToscaErrorCodes.getByCode(e.getCode())).toString());
+ ErrorInfo errorInfo = configurationManager.getErrorConfiguration().getErrorInfo(SdcToscaParserErrors.getSdcErrorByJToscaError(JToscaErrorCodes.getByCode(e.getCode())).toString());
throw new SdcToscaParserException(errorInfo.getMessage(), errorInfo.getCode());
}
+
+
}
\ No newline at end of file diff --git a/src/test/java/org/openecomp/sdc/impl/SdcToscaParserBasicTest.java b/src/test/java/org/openecomp/sdc/impl/SdcToscaParserBasicTest.java index 90124c5..74e52db 100644 --- a/src/test/java/org/openecomp/sdc/impl/SdcToscaParserBasicTest.java +++ b/src/test/java/org/openecomp/sdc/impl/SdcToscaParserBasicTest.java @@ -15,6 +15,7 @@ import org.openecomp.sdc.toscaparser.api.common.JToscaException; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; +import org.testng.annotations.BeforeSuite; public abstract class SdcToscaParserBasicTest { @@ -38,7 +39,7 @@ public abstract class SdcToscaParserBasicTest { static Map<String, HashMap<String, List<String>>> fdntCsarHelper_Data; - @BeforeClass + @BeforeSuite public static void init() throws SdcToscaParserException, JToscaException, IOException { factory = SdcToscaParserFactory.getInstance(); diff --git a/src/test/java/org/openecomp/sdc/impl/ToscaParserValidationIssueTest.java b/src/test/java/org/openecomp/sdc/impl/ToscaParserValidationIssueTest.java index 288cea6..87d8f09 100644 --- a/src/test/java/org/openecomp/sdc/impl/ToscaParserValidationIssueTest.java +++ b/src/test/java/org/openecomp/sdc/impl/ToscaParserValidationIssueTest.java @@ -1,16 +1,40 @@ package org.openecomp.sdc.impl; -import static org.testng.Assert.assertEquals; - -import java.util.List; -import java.util.stream.Collectors; - +import org.apache.commons.io.IOUtils; import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper; +import org.openecomp.sdc.tosca.parser.config.ConfigurationManager; +import org.openecomp.sdc.tosca.parser.config.JtoscaValidationIssueConfiguration; import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException; import org.openecomp.sdc.toscaparser.api.common.JToscaValidationIssue; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import java.io.IOException; +import java.net.URL; +import java.util.List; +import java.util.stream.Collectors; + +import static org.testng.Assert.assertEquals; + public class ToscaParserValidationIssueTest extends SdcToscaParserBasicTest { + protected static ConfigurationManager configurationManager = ConfigurationManager.getInstance(); + + @BeforeClass + public void loadJtoscaValidationIssueConfiguration() throws IOException { + //load the tests dedicated configuration + configurationManager.setJtoscaValidationIssueConfiguration( "jtosca-validation-issue-configuration-test.yaml"); + factory.setConfigurationManager(configurationManager); + } + + @AfterClass + public void loadJtoscaValidationIssueOriginalConfiguration() throws IOException { + //load the tests dedicated configuration + configurationManager.setJtoscaValidationIssueConfiguration("jtosca-validation-issue-configuration.yaml"); + factory.setConfigurationManager(configurationManager); + + } + @Test public void testNoValidationIssues() throws SdcToscaParserException { @@ -42,15 +66,25 @@ public class ToscaParserValidationIssueTest extends SdcToscaParserBasicTest { List<JToscaValidationIssue> criticalsReport = factory.getCriticalExceptions(); assertEquals( criticalsReport.size(),0); } + + @Test(expectedExceptions = SdcToscaParserException.class) + public void testCriticalIssueThrowsSdcToscaParserException() throws SdcToscaParserException { + getCsarHelper("csars/service-Nfod2images-csar.csar");//conformance level 4.0 + } + @Test - public void testMultiSinceConformanceLevelIssues() throws SdcToscaParserException { - ISdcCsarHelper Nfod2images = getCsarHelper("csars/service-Nfod2images-csar.csar");//conformance level 4.0 + public void testMultiSinceConformanceLevelIssues() { + try { + ISdcCsarHelper Nfod2images = getCsarHelper("csars/service-Nfod2images-csar.csar");//conformance level 4.0 + } catch (SdcToscaParserException e) { + System.out.println("SdcToscaParserException is caught here - this is WAD in this specific test."); + } List<JToscaValidationIssue> notAnalyzedReport = factory.getNotAnalyzadExceptions(); - assertEquals( notAnalyzedReport.size(),2); + assertEquals(2, notAnalyzedReport.size()); List<JToscaValidationIssue> warningsReport = factory.getWarningExceptions(); - assertEquals( warningsReport.size(),0); + assertEquals( 0, warningsReport.size()); List<JToscaValidationIssue> criticalsReport = factory.getCriticalExceptions(); - assertEquals( criticalsReport.size(),24); + assertEquals( 24, criticalsReport.size()); //JE006 multy values sinceCsarConformanceLevel assertEquals( criticalsReport.stream().filter(c->c.getCode().equals("JE006")).collect(Collectors.toList()).size(), 18); assertEquals( criticalsReport.stream().filter(c->c.getCode().equals("JE003")).collect(Collectors.toList()).size(), 6); diff --git a/src/test/resources/config/jtosca-validation-issue-configuration-test.yaml b/src/test/resources/config/jtosca-validation-issue-configuration-test.yaml new file mode 100644 index 0000000..1378ca7 --- /dev/null +++ b/src/test/resources/config/jtosca-validation-issue-configuration-test.yaml @@ -0,0 +1,57 @@ +# jTosca validation issues +#by error code, type the validation issue to be CRITICAL/WARNING +# since Conformance level considered to this type. for example: +#JE001: +# - type: WARNING, +# sinceCsarConformanceLevel: 3.0 +# - type: WARNING, +# sinceCsarConformanceLevel: 5.0 +validationIssues: + # TypeMismatchError + JE001: + - issueType: WARNING + sinceCsarConformanceLevel: 3.0 + - issueType: CRITICAL + sinceCsarConformanceLevel: 4.0 + - issueType: WARNING + sinceCsarConformanceLevel: 5.0 + # MissingType + JE002: + - issueType: WARNING + sinceCsarConformanceLevel: 3.0 + #MissingRequiredFieldError + JE003: + - issueType: CRITICAL + sinceCsarConformanceLevel: 4.0 + #MissingRequiredFieldError2 + JE004: + - issueType: WARNING + sinceCsarConformanceLevel: 2.0 + #InvalidGroupTargetException + JE005: + - issueType: WARNING + sinceCsarConformanceLevel: 3.0 + #Schema definition of \"%s\" has \"status\" attribute with an invalid value + JE006: + - issueType: WARNING + sinceCsarConformanceLevel: 3.0 + - issueType: CRITICAL + sinceCsarConformanceLevel: 4.0 + - issueType: WARNING + sinceCsarConformanceLevel: 5.0 + #The unit \"%s\" is not valid + JE007: + - issueType: WARNING + sinceCsarConformanceLevel: 3.0 + #ValidationError + JE008: + - issueType: WARNING + sinceCsarConformanceLevel: 3.0 + #ValueError: Expected max 2 arguments for function \"get_input\" but received \"%s\"",args.size()) + JE009: + - issueType: WARNING + sinceCsarConformanceLevel: 3.0 + #MissingRequiredFieldError3 + JE010: + - issueType: WARNING + sinceCsarConformanceLevel: 3.0
\ No newline at end of file diff --git a/src/test/resources/config/jtosca-validation-issue-configuration.yaml b/src/test/resources/config/jtosca-validation-issue-configuration.yaml index 1378ca7..f4bb949 100644 --- a/src/test/resources/config/jtosca-validation-issue-configuration.yaml +++ b/src/test/resources/config/jtosca-validation-issue-configuration.yaml @@ -1,32 +1,28 @@ -# jTosca validation issues -#by error code, type the validation issue to be CRITICAL/WARNING +# jTosca validation issues +#by error code, type the validation issue to be CRITICAL/WARNING # since Conformance level considered to this type. for example: -#JE001: -# - type: WARNING, -# sinceCsarConformanceLevel: 3.0 -# - type: WARNING, +#JE001: +# - issueType: WARNING +# sinceCsarConformanceLevel: 3.0 +# - issueType: WARNING # sinceCsarConformanceLevel: 5.0 validationIssues: # TypeMismatchError - JE001: + JE001: - issueType: WARNING sinceCsarConformanceLevel: 3.0 - - issueType: CRITICAL - sinceCsarConformanceLevel: 4.0 - - issueType: WARNING - sinceCsarConformanceLevel: 5.0 # MissingType JE002: - issueType: WARNING sinceCsarConformanceLevel: 3.0 #MissingRequiredFieldError JE003: - - issueType: CRITICAL - sinceCsarConformanceLevel: 4.0 + - issueType: WARNING + sinceCsarConformanceLevel: 3.0 #MissingRequiredFieldError2 JE004: - issueType: WARNING - sinceCsarConformanceLevel: 2.0 + sinceCsarConformanceLevel: 3.0 #InvalidGroupTargetException JE005: - issueType: WARNING @@ -35,10 +31,6 @@ validationIssues: JE006: - issueType: WARNING sinceCsarConformanceLevel: 3.0 - - issueType: CRITICAL - sinceCsarConformanceLevel: 4.0 - - issueType: WARNING - sinceCsarConformanceLevel: 5.0 #The unit \"%s\" is not valid JE007: - issueType: WARNING |