}
String convertServiceTemplateImport(ToscaExtensionYamlUtil toscaExtensionYamlUtil, byte[] fileContent) {
-
Map serviceTemplateMap = toscaExtensionYamlUtil.yamlToObject(new String(fileContent), Map.class);
convertToscaImports(serviceTemplateMap, toscaExtensionYamlUtil);
return toscaExtensionYamlUtil.objectToYaml(serviceTemplateMap);
private void convertToscaImports(Map serviceTemplateMap, ToscaExtensionYamlUtil toscaExtensionYamlUtil) {
List<Map<String, Import>> convertedImport = new ArrayList<>();
Object importObj = serviceTemplateMap.get(IMPORTS);
- if (!(importObj instanceof List)) {
- throw new SdcRuntimeException("Illegal Statement");
+ if (Objects.nonNull(importObj) && !(importObj instanceof List)) {
+ throw new SdcRuntimeException(new IllegalStateException("Invalid TOSCA import section"));
}
List<Object> imports = (List) importObj;
if (CollectionUtils.isEmpty(imports)) {
Assert.assertNotNull(serviceTemplate.getImports().get(7).get("relationshipsExt"));
}
+ @Test
+ public void testConvertToscaImportForEmptyImport() throws Exception {
+ String inputResourceName = "/mock/analyzerService/importConvertTestNoImport.yml";
+ byte[] uploadedFileData = IOUtils.toByteArray(this.getClass().getResource(inputResourceName));
+
+ ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
+ ToscaAnalyzerServiceImpl toscaAnalyzerServiceImpl = new ToscaAnalyzerServiceImpl();
+ String convertServiceTemplateImport =
+ toscaAnalyzerServiceImpl.convertServiceTemplateImport(toscaExtensionYamlUtil, uploadedFileData);
+
+ Assert.assertNotNull(convertServiceTemplateImport);
+ ServiceTemplate serviceTemplate =
+ new YamlUtil().yamlToObject(convertServiceTemplateImport, ServiceTemplate.class);
+ Assert.assertNull(serviceTemplate.getImports());
+ }
+
+ @Test
+ public void testInvalidToscaImportSection() throws Exception {
+ thrown.expect(SdcRuntimeException.class);
+ thrown.expectMessage("Invalid TOSCA import section");
+ String inputResourceName = "/mock/analyzerService/invalidToscaImport.yml";
+ byte[] uploadedFileData = IOUtils.toByteArray(this.getClass().getResource(inputResourceName));
+
+ ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
+ ToscaAnalyzerServiceImpl toscaAnalyzerServiceImpl = new ToscaAnalyzerServiceImpl();
+ toscaAnalyzerServiceImpl.convertServiceTemplateImport(toscaExtensionYamlUtil, uploadedFileData);
+ }
+
@Test
public void loadValidToscaYamlFileTest() throws Exception {
String inputResourceName = "/mock/analyzerService/ServiceTemplateInterfaceInheritanceTest.yaml";