fcfa12570972e519237d181e25c66259b6df6817
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / netconf-executor / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / netconf / executor / utils / NetconfMessageUtilsTest.kt
1 /*
2  * Copyright © 2019 Bell Canada
3  * Modifications Copyright (c) 2019 IBM
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 package org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.utils
18
19 import org.junit.Assert.assertEquals
20 import org.onap.ccsdk.cds.blueprintsprocessor.functions.netconf.executor.api.NetconfException
21 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
22 import kotlin.test.Test
23 import kotlin.test.assertFailsWith
24
25 class NetconfMessageUtilsTest {
26
27     @Test
28     fun `test getConfig with all parameters present`() {
29         val outcome = NetconfMessageUtils.getConfig("customMessageId", "customConfigType", "customFilterContent")
30         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/getConfig-response-all-parameters.xml")
31         assertEquals("getConfig return was not correct", expectation, outcome)
32     }
33
34     @Test
35     fun `test get operational`() {
36         val outcome = NetconfMessageUtils.get("customMessageId", "customConfigType")
37         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/get-response.xml")
38         assertEquals("get return was not correct", expectation, outcome)
39     }
40
41     @Test
42     fun `test getConfig with filterContent parameter null`() {
43         val outcome = NetconfMessageUtils.getConfig("customMessageId", "customConfigType",null)
44         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/getConfig-response-filterContent-null.xml")
45         assertEquals("getConfig return was not correct", expectation, outcome)
46     }
47
48     @Test
49     fun `test doWrappedRpc`() {
50         val outcome = NetconfMessageUtils.doWrappedRpc("customMessageId", "customRequest")
51         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/doWrappedRpc-response.xml")
52         assertEquals("doWrappedRpc return was not correct", expectation, outcome)
53     }
54
55     @Test
56     fun `test editConfig with all parameters present`() {
57         val outcome = NetconfMessageUtils.editConfig("customMessageId", "customConfigType", "customDefaultOperation",
58                 "customNewConfiguration")
59         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/editConfig-response-all-parameters.xml")
60         assertEquals("editConfig return was not correct", expectation, outcome)
61     }
62
63     @Test
64     fun `test editConfig with defaultOperation parameter null`() {
65         val outcome = NetconfMessageUtils.editConfig("customMessageId", "customConfigType", null,
66                 "customNewConfiguration")
67         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/editConfig-response-defaultOperation-null.xml")
68         assertEquals("editConfig return was not correct", expectation, outcome)
69     }
70
71     @Test
72     fun `test validate`() {
73         val outcome = NetconfMessageUtils.validate("customMessageId", "customConfigType")
74         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/validate-response.xml")
75         assertEquals("validate return was not correct", expectation, outcome)
76     }
77
78     @Test
79     fun `test commit with both persistId and persist non-empty`() {
80         assertFailsWith(exceptionClass = NetconfException::class, message = "commit should have thrown an exception") {
81             NetconfMessageUtils.commit("customMessageId", false, 1, "customPersist", "customPersistId")
82         }
83     }
84
85     @Test
86     fun `test commit with confirmed true, persist empty and persistId non-empty`() {
87         assertFailsWith(exceptionClass = NetconfException::class, message = "commit should have thrown an exception") {
88             NetconfMessageUtils.commit("customMessageId", true, 1, "", "customPersistId")
89         }
90     }
91
92     @Test
93     fun `test commit with confirmed true, persistId empty and persist empty`() {
94         val outcome = NetconfMessageUtils.commit("customMessageId", true, 1, "", "")
95         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/commit-response-confirmed-true-and-persistId-empty-and-persist-empty.xml")
96         assertEquals("commit return was not correct", expectation, outcome)
97     }
98
99     @Test
100     fun `test commit with confirmed false, persistId non-empty and persist empty`() {
101         val outcome = NetconfMessageUtils.commit("customMessageId", false, 1, "", "customPersistId")
102         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/commit-response-confirmed-false-and-persistId-empty-and-persist-not-empty.xml")
103         assertEquals("commit return was not correct", expectation, outcome)
104     }
105
106     @Test
107     fun `test commit with confirmed false, persistId empty and persist non-empty`() {
108         val outcome = NetconfMessageUtils.commit("customMessageId", false, 1, "customPersist", "")
109         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/commit-response-confirmed-false-and-persistId-not-empty-and-persist-empty.xml")
110         assertEquals("commit return was not correct", expectation, outcome)
111     }
112
113     @Test
114     fun `test cancelCommit with all parameters not empty`() {
115         val outcome = NetconfMessageUtils.cancelCommit("customMessageId", "customPersistId")
116         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/cancelCommit-response-all-parameters-not-empty.xml")
117         assertEquals("cancelCommit return was not correct", expectation, outcome)
118     }
119
120     @Test
121     fun `test cancelCommit with persistId empty`() {
122         val outcome = NetconfMessageUtils.cancelCommit("customMessageId", "")
123         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/cancelCommit-response-persistId-empty.xml")
124         assertEquals("cancelCommit return was not correct", expectation, outcome)
125     }
126
127     @Test
128     fun `test unlock with all parameters not empty`() {
129         val outcome = NetconfMessageUtils.unlock("customMessageId", "customConfigType")
130         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/unlock-response-all-parameters-not-empty.xml")
131         assertEquals("unlock return was not correct", expectation, outcome)
132     }
133
134     @Test
135     fun `test deleteConfig with all parameters not empty`() {
136         val outcome = NetconfMessageUtils.deleteConfig("customMessageId", "customConfigType")
137         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/deleteConfig-response-all-parameters-not-empty.xml")
138         assertEquals("deleteConfig return was not correct", expectation, outcome)
139     }
140
141     @Test
142     fun `test deleteConfig with configType equals to NetconfDatastore_RUNNING_datastore`() {
143         assertFailsWith(exceptionClass = NetconfException::class, message = "deleteConfig should have thrown an exception") {
144             NetconfMessageUtils.deleteConfig("customMessageId", NetconfDatastore.RUNNING.datastore)
145         }
146     }
147
148     @Test
149     fun `test discardChanges with all parameters not empty`() {
150         val outcome = NetconfMessageUtils.discardChanges("customMessageId")
151         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/discardChanges-response-all-parameters-not-empty.xml")
152         assertEquals("discardChanges return was not correct", expectation, outcome)
153     }
154
155     @Test
156     fun `test lock with all parameters not empty`() {
157         val outcome = NetconfMessageUtils.lock("customMessageId", "customConfigType")
158         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/lock-response-all-parameters-not-empty.xml")
159         assertEquals("lock return was not correct", expectation, outcome)
160     }
161
162     @Test
163     fun `test closeSession with force true`() {
164         val outcome = NetconfMessageUtils.closeSession("customMessageId", true)
165         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/closeSession-response-force-true.xml")
166         assertEquals("closeSession return was not correct", expectation, outcome)
167     }
168
169     @Test
170     fun `test closeSession with force false`() {
171         val outcome = NetconfMessageUtils.closeSession("customMessageId", false)
172         val expectation = JacksonUtils.getClassPathFileContent("netconf-messages/closeSession-response-force-false.xml")
173         assertEquals("closeSession return was not correct", expectation, outcome)
174     }
175
176     //TODO validateRPCXML
177
178     @Test
179     fun `test getMsgId with valid message`() {
180         val messageId = "1234"
181         val outcome = NetconfMessageUtils.getMsgId("message-id=\"${messageId}\"")
182         val expectation = messageId
183         assertEquals("getMsgId return was not correct", expectation, outcome)
184     }
185
186     @Test
187     fun `test getMsgId with RpcMessageUtils_HELLO message`() {
188         val outcome = NetconfMessageUtils.getMsgId(RpcMessageUtils.HELLO)
189         val expectation = "-1"
190         assertEquals("getMsgId return was not correct", expectation, outcome)
191     }
192
193     @Test
194     fun `test getMsgId with invalid message`() {
195         val outcome = NetconfMessageUtils.getMsgId("invalid message")
196         val expectation = ""
197         assertEquals("getMsgId return was not correct", expectation, outcome)
198     }
199
200 //TODO validateChunkedFraming
201
202 }