2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.common.test;
23 import org.apache.commons.codec.binary.Base64;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.openecomp.sdc.common.util.YamlToObjectConverter;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertTrue;
31 public class YamlTest {
33 private static YamlToObjectConverter yamlToObjectConverter;
34 private static String validYaml = "heat_template_version: 2013-05-23\r\ndescription: A load-balancer server\r\nparameters:\r\n image:\r\n type: string\r\n description: Image used for servers\r\n key_name:\r\n type: string\r\n description: SSH key to connect to the servers\r\n flavor:\r\n type: string\r\n description: flavor used by the servers\r\n pool_id:\r\n type: string\r\n description: Pool to contact\r\n user_data:\r\n type: string\r\n description: Server user_data\r\n metadata:\r\n type: json\r\n network:\r\n type: string\r\n description: Network used by the server\r\n\r\nresources:\r\n server:\r\n type: OS::Nova::Server\r\n properties:\r\n flavor: {get_param: flavor}\r\n image: {get_param: image}\r\n key_name: {get_param: key_name}\r\n metadata: {get_param: metadata}\r\n user_data: {get_param: user_data}\r\n user_data_format: RAW\r\n networks: [{network: {get_param: network} }]\r\n member:\r\n type: OS::Neutron::PoolMember\r\n properties:\r\n pool_id: {get_param: pool_id}\r\n address: {get_attr: [server, first_address]}\r\n protocol_port: 80\r\n\r\noutputs:\r\n server_ip:\r\n description: IP Address of the load-balanced server.\r\n value: { get_attr: [server, first_address] }\r\n lb_member:\r\n description: LB member details.\r\n value: { get_attr: [member, show] }";
35 // Missing square brackets at the end of string
36 private static String invalidYaml = "heat_template_version: 2013-05-23\r\ndescription: A load-balancer server\r\nparameters:\r\n image:\r\n type: string\r\n description: Image used for servers\r\n key_name:\r\n type: string\r\n description: SSH key to connect to the servers\r\n flavor:\r\n type: string\r\n description: flavor used by the servers\r\n pool_id:\r\n type: string\r\n description: Pool to contact\r\n user_data:\r\n type: string\r\n description: Server user_data\r\n metadata:\r\n type: json\r\n network:\r\n type: string\r\n description: Network used by the server\r\n\r\nresources:\r\n server:\r\n type: OS::Nova::Server\r\n properties:\r\n flavor: {get_param: flavor}\r\n image: {get_param: image}\r\n key_name: {get_param: key_name}\r\n metadata: {get_param: metadata}\r\n user_data: {get_param: user_data}\r\n user_data_format: RAW\r\n networks: [{network: {get_param: network} }]\r\n member:\r\n type: OS::Neutron::PoolMember\r\n properties:\r\n pool_id: {get_param: pool_id}\r\n address: {get_attr: [server, first_address]}\r\n protocol_port: 80\r\n\r\noutputs:\r\n server_ip:\r\n description: IP Address of the load-balanced server.\r\n value: { get_attr: [server, first_address] }\r\n lb_member:\r\n description: LB member details.\r\n value: { get_attr: [member, show}";
39 public static void setup() {
40 yamlToObjectConverter = new YamlToObjectConverter();
44 public void testValidYaml() {
45 assertTrue(yamlToObjectConverter.isValidYaml(validYaml.getBytes()));
49 public void testInvalidYaml() {
50 assertFalse(yamlToObjectConverter.isValidYaml(invalidYaml.getBytes()));
54 public void testValidYamlBase64() {
55 assertTrue(yamlToObjectConverter.isValidYamlEncoded64(Base64.encodeBase64(validYaml.getBytes())));
59 public void testInvalidYamlBase64() {
60 assertFalse(yamlToObjectConverter.isValidYamlEncoded64(Base64.encodeBase64(invalidYaml.getBytes())));