Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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 | |
| 17 | package org.onosproject.netconf.client; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import org.junit.Test; |
| 21 | import org.onosproject.netconf.client.impl.NetconfTranslatorImpl; |
| 22 | |
| 23 | import java.util.regex.Matcher; |
| 24 | import java.util.regex.Pattern; |
| 25 | |
| 26 | import static org.junit.Assert.assertEquals; |
| 27 | |
| 28 | /** |
Yuta HIGUCHI | 9312a80 | 2017-06-12 20:01:27 -0700 | [diff] [blame] | 29 | * Test class for {@link NetconfTranslatorImpl}. |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 30 | */ |
| 31 | @Beta |
| 32 | public class NetconfTranslatorImplTest { |
| 33 | private static final String CORE_GET_CONFIG_MESSAGE_REGEX = |
| 34 | "<data>\n?\\s*(.*?)\n?\\s*</data>"; |
| 35 | private static final int GET_CONFIG_CORE_MESSAGE_GROUP = 1; |
| 36 | private static final Pattern GET_CONFIG_CORE_MESSAGE_PATTERN = |
| 37 | Pattern.compile(CORE_GET_CONFIG_MESSAGE_REGEX, Pattern.DOTALL); |
| 38 | private static final String GET_CORE_MESSAGE_REGEX = "<data>\n?\\s*(.*?)\n?\\s*</data>"; |
| 39 | private static final int GET_CORE_MESSAGE_GROUP = 1; |
| 40 | private static final Pattern GET_CORE_MESSAGE_PATTERN = |
| 41 | Pattern.compile(GET_CORE_MESSAGE_REGEX, Pattern.DOTALL); |
| 42 | |
| 43 | private static final String SAMPLE_GET_REPLY = "<rpc-reply message-id=\"101\"\n" + |
| 44 | " xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" + |
| 45 | " <data>\n" + |
| 46 | " <t:top xmlns:t=\"http://example.com/schema/1.2/stats\">\n" + |
| 47 | " <t:interfaces>\n" + |
| 48 | " <t:interface t:ifName=\"eth0\">\n" + |
| 49 | " <t:ifInOctets>45621</t:ifInOctets>\n" + |
| 50 | " <t:ifOutOctets>774344</t:ifOutOctets>\n" + |
| 51 | " </t:interface>\n" + |
| 52 | " </t:interfaces>\n" + |
| 53 | " </t:top>\n" + |
| 54 | " </data>\n" + |
| 55 | " </rpc-reply>"; |
Yuta HIGUCHI | 9312a80 | 2017-06-12 20:01:27 -0700 | [diff] [blame] | 56 | private static final String CORRECT_FILTERED_GET_REPLY = |
| 57 | "<t:top xmlns:t=\"http://example.com/schema/1.2/stats\">\n" + |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 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); |
Yuta HIGUCHI | 9312a80 | 2017-06-12 20:01:27 -0700 | [diff] [blame] | 106 | assertEquals("Messages did not match", |
| 107 | CORRECT_FILTERED_GET_CONFIG_REPLY, |
| 108 | matcher.group(GET_CONFIG_CORE_MESSAGE_GROUP)); |
Aaron Kruglikov | bc26a52 | 2017-02-21 11:27:49 -0800 | [diff] [blame] | 109 | //Basic check for the get regex. |
| 110 | matcher = GET_CORE_MESSAGE_PATTERN.matcher(SAMPLE_GET_REPLY); |
| 111 | matcher.find(); |
| 112 | // System.out.println(matcher.group(1)); |
| 113 | // System.out.println(DESIRED_SUBSTRING_GET_CONFIG); |
| 114 | assertEquals("Messages did not match", CORRECT_FILTERED_GET_REPLY, matcher.group(GET_CORE_MESSAGE_GROUP)); |
| 115 | } |
| 116 | } |