Onboard PNF software version
[sdc.git] / openecomp-be / lib / openecomp-tosca-converter-lib / openecomp-tosca-converter-core / src / test / java / org / openecomp / core / converter / impl / pnfd / parser / ConversionDefinitionYamlParserTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.core.converter.impl.pnfd.parser;
21
22 import static org.hamcrest.Matchers.equalTo;
23 import static org.hamcrest.Matchers.notNullValue;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.fail;
27 import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.QUERY;
28 import static org.openecomp.core.converter.pnfd.model.PnfTransformationToken.TO_NAME;
29
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.util.HashMap;
33 import java.util.Map;
34 import java.util.Optional;
35 import org.junit.Test;
36 import org.openecomp.core.converter.pnfd.model.ConversionDefinition;
37 import org.openecomp.core.converter.pnfd.model.ConversionStrategyType;
38 import org.openecomp.core.util.TestResourcesUtil;
39 import org.openecomp.core.util.YamlTestUtil;
40
41 public class ConversionDefinitionYamlParserTest {
42
43     @Test
44     public void shouldBuildDefinition() {
45         final Map<String, Object> definitionYaml;
46         final String definitionYamlFilePath = "transformation/conversionDefinition/conversionDefinitionWithReplaceStrategy.yaml";
47         try (final InputStream resourceInputStream = TestResourcesUtil.getFileResourceAsStream(definitionYamlFilePath)) {
48             definitionYaml = (Map<String, Object>) YamlTestUtil.read(resourceInputStream);
49         } catch (final IOException e) {
50             fail(String.format("Could not load %s", definitionYamlFilePath));
51             return;
52         }
53         final ConversionDefinition conversionDefinition = ConversionDefinitionYamlParser.parse(definitionYaml).orElse(null);
54         assertConversionDefinition(definitionYaml, conversionDefinition);
55     }
56
57     @Test
58     public void shouldReturnEmpty() {
59         assertEquals(Optional.empty(), ConversionDefinitionYamlParser.parse(new HashMap<>()));
60     }
61
62     private void assertConversionDefinition(final Map<String, Object> definitionYaml,
63             final ConversionDefinition conversionDefinition) {
64         assertThat("The conversion definition should have been built"
65             , conversionDefinition, notNullValue());
66         assertThat("Should have initialized the conversion definition query"
67             , conversionDefinition.getConversionQuery(), notNullValue());
68         assertThat("The conversion definition should have been built"
69             , conversionDefinition.getConversionQuery().getQuery(), equalTo(definitionYaml.get(QUERY.getName())));
70         assertThat("Should have initialized the conversion definition to attribute name"
71             , conversionDefinition.getToAttributeName(), equalTo(definitionYaml.get(TO_NAME.getName())));
72         assertThat("Should have initialized the conversion definition strategy"
73             , conversionDefinition.getPnfdConversionStrategy(), notNullValue());
74         assertThat("Should have the expected strategy"
75             , conversionDefinition.getPnfdConversionStrategy().getStrategyType(), equalTo(ConversionStrategyType.REPLACE));
76     }
77 }