|
17 | 17 | package org.apache.cloudstack.tosca.parser; |
18 | 18 |
|
19 | 19 | import com.cloud.exception.InvalidParameterValueException; |
| 20 | +import org.apache.cloudstack.tosca.functions.ToscaBooleanFunctions; |
| 21 | +import org.apache.cloudstack.tosca.model.ToscaInputDefinition; |
| 22 | +import org.apache.cloudstack.tosca.model.ToscaPrimitiveType; |
| 23 | +import org.apache.cloudstack.tosca.model.ToscaTypeDefinition; |
| 24 | +import org.junit.Assert; |
20 | 25 | import org.junit.Before; |
21 | 26 | import org.junit.Test; |
22 | 27 | import org.junit.runner.RunWith; |
@@ -64,6 +69,58 @@ public void parseServiceTemplateTestThrowExceptionWhenUnknownKeysOfTheServiceTem |
64 | 69 | toscaServiceTemplateParserSpy.parseServiceTemplate(content, null, null); |
65 | 70 | } |
66 | 71 |
|
| 72 | + @Test |
| 73 | + public void parseInputsTestAllInputsAreParsedSuccessfullyAndAddedToTheParsingContext() { |
| 74 | + String content = "{type: {type: string, validation: {$valid_values: [$value, [validvalue1, validvalue2]]}}, enabled: {description: Enabled input description, type: boolean, default_value: false}}"; |
| 75 | + Map<String, ToscaInputDefinition> inputs = toscaServiceTemplateParserSpy.parseInputs(ToscaYamlHelper.asMap(ToscaYamlHelper.loadYaml(content)), parsingContextMock); |
| 76 | + Mockito.verify(parsingContextMock, Mockito.times(0)).addError(Mockito.anyString(), Mockito.anyString()); |
| 77 | + Mockito.verify(parsingContextMock).setInputs(inputs); |
| 78 | + Assert.assertEquals(2, inputs.size()); |
| 79 | + |
| 80 | + Assert.assertEquals("type", inputs.get("type").getName()); |
| 81 | + Assert.assertNull(inputs.get("type").getDescription()); |
| 82 | + Assert.assertNull(inputs.get("type").getDefaultValue()); |
| 83 | + Assert.assertEquals(ToscaPrimitiveType.STRING, inputs.get("type").getType().getPrimitiveType()); |
| 84 | + Assert.assertTrue(inputs.get("type").getValidation() instanceof ToscaBooleanFunctions.ValidValues); |
| 85 | + |
| 86 | + Assert.assertEquals("enabled", inputs.get("enabled").getName()); |
| 87 | + Assert.assertEquals("Enabled input description", inputs.get("enabled").getDescription()); |
| 88 | + Assert.assertFalse((Boolean) inputs.get("enabled").getDefaultValue()); |
| 89 | + Assert.assertEquals(ToscaPrimitiveType.BOOLEAN, inputs.get("enabled").getType().getPrimitiveType()); |
| 90 | + Assert.assertNull(inputs.get("enabled").getValidation()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void parseInputsTestAllCorrectInputDeclarationAreParsedAndErrorsOfUnknownKeysAndMissingInputTypeAreAddedToTheParsingContext() { |
| 95 | + String content = "{type: {unknownfield: string, validation: {$valid_values: [$value, [validvalue1, validvalue2]]}}, enabled: {description: Enabled input description, type: boolean, default_value: false}}"; |
| 96 | + Map<String, ToscaInputDefinition> inputs = toscaServiceTemplateParserSpy.parseInputs(ToscaYamlHelper.asMap(ToscaYamlHelper.loadYaml(content)), parsingContextMock); |
| 97 | + Mockito.verify(parsingContextMock).addError(Mockito.eq("Unknown key [unknownfield]."), Mockito.anyString()); |
| 98 | + Mockito.verify(parsingContextMock).addError(Mockito.eq("The type of the input [type] was not specified or it is not supported."), Mockito.anyString()); |
| 99 | + Assert.assertFalse(inputs.containsKey("type")); |
| 100 | + Assert.assertTrue(inputs.containsKey("enabled")); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void parseInputsTestAllCorrectInputDeclarationAreParsedAndErrorsOfDefaultValueTypeIncompatibilityAreAddedToTheParsingContext() { |
| 105 | + String content = "{type: {type: string, validation: {$valid_values: [$value, [validvalue1, validvalue2]]}}, enabled: {description: Enabled input description, type: boolean, default_value: nonbooleanvalue}}"; |
| 106 | + Map<String, ToscaInputDefinition> inputs = toscaServiceTemplateParserSpy.parseInputs(ToscaYamlHelper.asMap(ToscaYamlHelper.loadYaml(content)), parsingContextMock); |
| 107 | + |
| 108 | + String expectedErrorMessage = String.format("The provided default value [nonbooleanvalue] for the input [enabled] is not compatible with the input type [%s].", ToscaTypeDefinition.ofPrimitive(ToscaPrimitiveType.BOOLEAN)); |
| 109 | + Mockito.verify(parsingContextMock).addError(Mockito.eq(expectedErrorMessage), Mockito.anyString()); |
| 110 | + Assert.assertTrue(inputs.containsKey("type")); |
| 111 | + Assert.assertFalse(inputs.containsKey("enabled")); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void parseInputsTestAllCorrectInputDeclarationAreParsedAndErrorsOfInvalidValidationFunctionAreAddedToTheParsingContext() { |
| 116 | + String content = "{type: {type: string, validation: {$notvalidfunction: [$value, [validvalue1, validvalue2]]}}, enabled: {description: Enabled input description, type: boolean, default_value: false}}"; |
| 117 | + Map<String, ToscaInputDefinition> inputs = toscaServiceTemplateParserSpy.parseInputs(ToscaYamlHelper.asMap(ToscaYamlHelper.loadYaml(content)), parsingContextMock); |
| 118 | + |
| 119 | + Mockito.verify(parsingContextMock).addError(Mockito.eq("The validation function of the input [type] is not valid."), Mockito.anyString()); |
| 120 | + Assert.assertFalse(inputs.containsKey("type")); |
| 121 | + Assert.assertTrue(inputs.containsKey("enabled")); |
| 122 | + } |
| 123 | + |
67 | 124 | @Test |
68 | 125 | public void validateRequiredToscaKeysTestAddErrorToTheParsingContextWhenRequiredKeysAreMissing() { |
69 | 126 | Map<String, Object> content = ToscaYamlHelper.asMap(ToscaYamlHelper.loadYaml("{not-required-key: value}")); |
|
0 commit comments