Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present |
| 3 | * Open Networking Laboratory |
| 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 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | package org.onosproject.netconf.client; |
| 19 | |
| 20 | import com.google.common.annotations.Beta; |
| 21 | import org.junit.Test; |
| 22 | import org.onosproject.netconf.client.impl.NetconfTranslatorImpl; |
| 23 | |
| 24 | import java.util.regex.Matcher; |
| 25 | import java.util.regex.Pattern; |
| 26 | |
| 27 | import static org.junit.Assert.assertEquals; |
| 28 | |
| 29 | /** |
| 30 | * Test class for {@link NetconfTranslatorImpl} |
| 31 | */ |
| 32 | @Beta |
| 33 | public class NetconfTranslatorImplTest { |
| 34 | private static final String CORE_GET_CONFIG_MESSAGE_REGEX = |
| 35 | "<data>\n?\\s*(.*?)\n?\\s*</data>"; |
| 36 | private static final int GET_CONFIG_CORE_MESSAGE_GROUP = 1; |
| 37 | private static final Pattern GET_CONFIG_CORE_MESSAGE_PATTERN = |
| 38 | Pattern.compile(CORE_GET_CONFIG_MESSAGE_REGEX, Pattern.DOTALL); |
| 39 | private static final String GET_CORE_MESSAGE_REGEX = "<data>\n?\\s*(.*?)\n?\\s*</data>"; |
| 40 | private static final int GET_CORE_MESSAGE_GROUP = 1; |
| 41 | private static final Pattern GET_CORE_MESSAGE_PATTERN = |
| 42 | Pattern.compile(GET_CORE_MESSAGE_REGEX, Pattern.DOTALL); |
| 43 | |
| 44 | private static final String SAMPLE_GET_REPLY = "<rpc-reply message-id=\"101\"\n" + |
| 45 | " xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" + |
| 46 | " <data>\n" + |
| 47 | " <t:top xmlns:t=\"http://example.com/schema/1.2/stats\">\n" + |
| 48 | " <t:interfaces>\n" + |
| 49 | " <t:interface t:ifName=\"eth0\">\n" + |
| 50 | " <t:ifInOctets>45621</t:ifInOctets>\n" + |
| 51 | " <t:ifOutOctets>774344</t:ifOutOctets>\n" + |
| 52 | " </t:interface>\n" + |
| 53 | " </t:interfaces>\n" + |
| 54 | " </t:top>\n" + |
| 55 | " </data>\n" + |
| 56 | " </rpc-reply>"; |
| 57 | private static final String CORRECT_FILTERED_GET_REPLY = "<t:top xmlns:t=\"http://example.com/schema/1.2/stats\">\n" + |
| 58 | " <t:interfaces>\n" + |
| 59 | " <t:interface t:ifName=\"eth0\">\n" + |
| 60 | " <t:ifInOctets>45621</t:ifInOctets>\n" + |
| 61 | " <t:ifOutOctets>774344</t:ifOutOctets>\n" + |
| 62 | " </t:interface>\n" + |
| 63 | " </t:interfaces>\n" + |
| 64 | " </t:top>"; |
| 65 | private static final String SAMPLE_GET_CONFIG_REPLY = "<rpc-reply message-id=\"101\"\n" + |
| 66 | " xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" + |
| 67 | " <data>\n" + |
| 68 | " <top xmlns=\"http://example.com/schema/1.2/config\">\n" + |
| 69 | " <users>\n" + |
| 70 | " <user>\n" + |
| 71 | " <name>root</name>\n" + |
| 72 | " <type>superuser</type>\n" + |
| 73 | " <full-name>Charlie Root</full-name> <company-info>\n" + |
| 74 | " <dept>1</dept>\n" + |
| 75 | " <id>1</id>\n" + |
| 76 | " </company-info>\n" + |
| 77 | " </user>\n" + |
| 78 | " <!-- additional <user> elements appear here... -->\n" + |
| 79 | " </users>\n" + |
| 80 | " </top>\n" + |
| 81 | " </data>\n" + |
| 82 | " </rpc-reply>"; |
| 83 | private static final String CORRECT_FILTERED_GET_CONFIG_REPLY = |
| 84 | "<top xmlns=\"http://example.com/schema/1.2/config\">\n" + |
| 85 | " <users>\n" + |
| 86 | " <user>\n" + |
| 87 | " <name>root</name>\n" + |
| 88 | " <type>superuser</type>\n" + |
| 89 | " <full-name>Charlie Root</full-name> <company-info>\n" + |
| 90 | " <dept>1</dept>\n" + |
| 91 | " <id>1</id>\n" + |
| 92 | " </company-info>\n" + |
| 93 | " </user>\n" + |
| 94 | " <!-- additional <user> elements appear here... -->\n" + |
| 95 | " </users>\n" + |
| 96 | " </top>"; |
| 97 | |
| 98 | |
| 99 | @Test |
| 100 | public void testRegex() { |
| 101 | //Basic check for the getConfig regex. |
| 102 | Matcher matcher = GET_CONFIG_CORE_MESSAGE_PATTERN.matcher(SAMPLE_GET_CONFIG_REPLY); |
| 103 | matcher.find(); |
| 104 | // System.out.println(matcher.group(1)); |
| 105 | // System.out.println(DESIRED_SUBSTRING_GET_CONFIG); |
| 106 | assertEquals("Messages did not match", CORRECT_FILTERED_GET_CONFIG_REPLY, matcher.group(GET_CONFIG_CORE_MESSAGE_GROUP)); |
| 107 | //Basic check for the get regex. |
| 108 | matcher = GET_CORE_MESSAGE_PATTERN.matcher(SAMPLE_GET_REPLY); |
| 109 | matcher.find(); |
| 110 | // System.out.println(matcher.group(1)); |
| 111 | // System.out.println(DESIRED_SUBSTRING_GET_CONFIG); |
| 112 | assertEquals("Messages did not match", CORRECT_FILTERED_GET_REPLY, matcher.group(GET_CORE_MESSAGE_GROUP)); |
| 113 | } |
| 114 | } |