Bug fix to add anyxml node.
[ccsdk/sli/plugins.git] / restconf-client / provider / src / test / java / org / onap / ccsdk / sli / plugins / yangserializers / dfserializer / DataFormatSerializerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - CCSDK
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.ccsdk.sli.plugins.yangserializers.dfserializer;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.invocation.InvocationOnMock;
29 import org.mockito.stubbing.Answer;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
32 import org.onap.ccsdk.sli.plugins.restapicall.HttpResponse;
33 import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode;
34 import org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiCallNode;
35 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
36
37 import static org.hamcrest.MatcherAssert.assertThat;
38 import static org.hamcrest.core.Is.is;
39 import static org.mockito.Matchers.any;
40 import static org.mockito.Mockito.doAnswer;
41 import static org.mockito.Mockito.doCallRealMethod;
42 import static org.mockito.Mockito.doReturn;
43 import static org.mockito.Mockito.mock;
44 import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.GET;
45 import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PATCH;
46 import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.POST;
47 import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PUT;
48 import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.parseUrl;
49 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.DECODE_ANYXML_RESPONSE;
50 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.DECODE_FROM_JSON_RPC;
51 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.DECODE_FROM_XML_RPC;
52 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_ANYXML;
53 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_ID;
54 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_ID_PUT;
55 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_RPC;
56 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_YANG;
57 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_YANG_AUG_POST;
58 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_YANG_PUT;
59 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_ID;
60 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_ID_PUT;
61 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_RPC;
62 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_YANG;
63 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_YANG_AUG_POST;
64 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_YANG_PUT;
65
66
67 /**
68  * Unit test cases for data format serialization and restconf api call node.
69  */
70 public class DataFormatSerializerTest {
71
72     private Map<String, String> p;
73
74     private RestconfApiCallNode restconf;
75
76     private RestapiCallNode restApi;
77
78     private DfCaptor dfCaptor;
79
80     /**
81      * Sets up the pre-requisite for each test case.
82      *
83      * @throws SvcLogicException when test case fails
84      */
85     @Before
86     public void setUp() throws SvcLogicException {
87         p = new HashMap<>();
88         p.put("restapiUser", "user1");
89         p.put("restapiPassword", "abc123");
90         p.put("responsePrefix", "response");
91         p.put("skipSending", "true");
92         restApi = new RestapiCallNode();
93         restconf = mock(RestconfApiCallNode.class);
94         dfCaptor = new DfCaptor();
95         createMethodMocks();
96     }
97
98     /**
99      * Creates method mocks using mockito for RestconfApiCallNode class.
100      *
101      * @throws SvcLogicException when test case fails
102      */
103     private void createMethodMocks() throws SvcLogicException {
104         doReturn(restApi).when(restconf).getRestapiCallNode();
105         doCallRealMethod().when(restconf).sendRequest(
106                 any(Map.class), any(SvcLogicContext.class));
107         doCallRealMethod().when(restconf).sendRequest(
108                 any(Map.class), any(SvcLogicContext.class), any(Integer.class));
109         doAnswer(dfCaptor).when(restconf).serializeRequest(
110                 any(Map.class), any(YangParameters.class), any(String.class),
111                 any(InstanceIdentifierContext.class));
112         doAnswer(dfCaptor).when(restconf).updateReq(
113                 any(String.class), any(YangParameters.class),
114                 any(InstanceIdentifierContext.class));
115     }
116
117     /**
118      * Creates mock using mockito with input data for decoding.
119      *
120      * @param decodeData input data
121      * @throws SvcLogicException when test case fails
122      */
123     private void createMockForDecode(String decodeData)
124             throws SvcLogicException {
125         doReturn(decodeData).when(restconf).getResponse(
126                 any(SvcLogicContext.class), any(YangParameters.class),
127                 any(String.class), any(HttpResponse.class));
128         doCallRealMethod().when(restconf).serializeResponse(
129                 any(YangParameters.class), any(String.class), any(String.class),
130                 any(InstanceIdentifierContext.class));
131     }
132
133     /**
134      * Verifies encoding of parameters to JSON data format with identity-ref
135      * and inter-file linking.
136      *
137      * @throws SvcLogicException when test case fails
138      */
139     @Test
140     public void encodeToJsonId() throws SvcLogicException {
141         String pre = "identity-test_test.";
142         SvcLogicContext ctx = createAttList(pre);
143         ctx.setAttribute(pre + "l", "abc");
144         p.put("dirPath", "src/test/resources");
145         p.put("format", "json");
146         p.put("httpMethod", "post");
147         p.put("restapiUrl", "http://echo.getpostman" +
148                 ".com/restconf/operations/identity-test:test");
149         restconf.sendRequest(p, ctx);
150         assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_ID));
151     }
152
153     /**
154      * Verifies encoding of parameters to JSON data format any xml in it.
155      *
156      * @throws SvcLogicException when test case fails
157      */
158     @Test
159     public void encodeForAnyXml() throws SvcLogicException {
160         String pre = "execution-service_process.";
161         SvcLogicContext ctx = createAnyXmlAttList(pre);
162         p.put("dirPath", "src/test/resources");
163         p.put("format", "json");
164         p.put("httpMethod", "post");
165         p.put("restapiUrl", "http://echo.getpostman" +
166                 ".com/api/v1/execution-service/process");
167         restconf.sendRequest(p, ctx);
168         assertThat(dfCaptor.getResult(), is(ENCODE_TO_ANYXML));
169     }
170
171     /**
172      * Verifies encoding of parameters to JSON data format with identity-ref
173      * and inter-file linking for put operation-type.
174      *
175      * @throws SvcLogicException when test case fails
176      */
177     @Test
178     public void encodeToJsonIdWithPut() throws SvcLogicException {
179         String pre = "identity-test_test.";
180         SvcLogicContext ctx = createAttList(pre);
181         ctx.setAttribute(pre + "l", "abc");
182         p.put("dirPath", "src/test/resources");
183         p.put("format", "json");
184         p.put("httpMethod", "put");
185         p.put("restapiUrl", "http://echo.getpostman" +
186                 ".com/restconf/operations/identity-test:test");
187         restconf.sendRequest(p, ctx);
188         assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_ID_PUT));
189     }
190
191     /**
192      * Verifies encoding of parameters to JSON data format with identity-ref
193      * and inter-file linking for patch operation-type.
194      *
195      * @throws SvcLogicException when test case fails
196      */
197     @Test
198     public void encodeToJsonIdWithPatch() throws SvcLogicException {
199         String pre = "identity-test_test.";
200         SvcLogicContext ctx = createAttList(pre);
201         ctx.setAttribute(pre + "l", "abc");
202         p.put("dirPath", "src/test/resources");
203         p.put("format", "json");
204         p.put("httpMethod", "patch");
205         p.put("restapiUrl", "http://echo.getpostman" +
206                 ".com/restconf/operations/identity-test:test");
207         restconf.sendRequest(p, ctx);
208         assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_ID_PUT));
209     }
210
211     /**
212      * Verifies encoding of parameters to XML data format with identity-ref
213      * and inter-file linking.
214      *
215      * @throws SvcLogicException when test case fails
216      */
217     @Test
218     public void encodeToXmlId() throws SvcLogicException {
219         String pre = "identity-test_test.";
220         SvcLogicContext ctx = createAttList(pre);
221         p.put("dirPath", "src/test/resources");
222         p.put("format", "xml");
223         p.put("httpMethod", "post");
224         p.put("restapiUrl", "http://echo.getpostman" +
225                 ".com/restconf/operations/identity-test:test");
226         restconf.sendRequest(p, ctx);
227         assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_ID));
228     }
229
230     /**
231      * Verifies encoding of parameters to XML data format with identity-ref
232      * and inter-file linking for put operation-type.
233      *
234      * @throws SvcLogicException when test case fails
235      */
236     @Test
237     public void encodeToXmlIdWithPut() throws SvcLogicException {
238         String pre = "identity-test_test.";
239         SvcLogicContext ctx = createAttList(pre);
240         p.put("dirPath", "src/test/resources");
241         p.put("format", "xml");
242         p.put("httpMethod", "put");
243         p.put("restapiUrl", "http://echo.getpostman" +
244                 ".com/restconf/operations/identity-test:test");
245         restconf.sendRequest(p, ctx);
246         assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_ID_PUT));
247     }
248
249     /**
250      * Verifies encoding of parameters to XML data format with identity-ref
251      * and inter-file linking for patch operation-type.
252      *
253      * @throws SvcLogicException when test case fails
254      */
255     @Test
256     public void encodeToXmlIdWithPatch() throws SvcLogicException {
257         String pre = "identity-test_test.";
258         SvcLogicContext ctx = createAttList(pre);
259         p.put("dirPath", "src/test/resources");
260         p.put("format", "xml");
261         p.put("httpMethod", "patch");
262         p.put("restapiUrl", "http://echo.getpostman" +
263                 ".com/restconf/operations/identity-test:test");
264         restconf.sendRequest(p, ctx);
265         assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_ID_PUT));
266     }
267
268     /**
269      * Verifies decoding of parameters from JSON data format with identity-ref
270      * and inter-file linking.
271      *
272      * @throws SvcLogicException when test case fails
273      */
274     @Test
275     public void decodeToJsonId() throws SvcLogicException {
276         createMockForDecode(ENCODE_TO_JSON_ID);
277         SvcLogicContext ctx = new SvcLogicContext();
278         String pre = "identity-test_test.";
279         p.put("dirPath", "src/test/resources");
280         p.put("format", "json");
281         p.put("httpMethod", "get");
282         p.put("restapiUrl", "http://echo.getpostman" +
283                 ".com/restconf/operations/identity-test:test");
284         restconf.sendRequest(p, ctx);
285         assertThat(ctx.getAttribute(pre + "l"), is("abc"));
286         verifyAttList(ctx, pre);
287     }
288
289     /**
290      * Verifies decoding of parameters from XML data format with identity-ref
291      * and inter-file linking.
292      *
293      * @throws SvcLogicException when test case fails
294      */
295     @Test
296     public void decodeToXmlId() throws SvcLogicException {
297         createMockForDecode(ENCODE_TO_XML_ID);
298         SvcLogicContext ctx = new SvcLogicContext();
299         String pre = "identity-test_test.";
300         p.put("dirPath", "src/test/resources");
301         p.put("format", "xml");
302         p.put("httpMethod", "get");
303         p.put("restapiUrl", "http://echo.getpostman" +
304                 ".com/restconf/operations/identity-test:test");
305         restconf.sendRequest(p, ctx);
306         verifyAttList(ctx, pre);
307     }
308
309     /**
310      * Verifies encoding of parameters to JSON data format with containers,
311      * grouping and augment.
312      *
313      * @throws SvcLogicException when test case fails
314      */
315     @Test
316     public void encodeToJsonYang() throws SvcLogicException {
317         String pre = "test-yang_cont1.cont2.";
318         SvcLogicContext ctx = createAttListYang(pre);
319         p.put("dirPath", "src/test/resources");
320         p.put("format", "json");
321         p.put("httpMethod", "post");
322         p.put("restapiUrl", "http://echo.getpostman" +
323                 ".com/restconf/operations/test-yang:cont1");
324         restconf.sendRequest(p, ctx);
325         assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG));
326     }
327
328     /**
329      * Verifies encoding of parameters to JSON data format with containers,
330      * grouping and augment for put operation-type.
331      *
332      * @throws SvcLogicException when test case fails
333      */
334     @Test
335     public void encodeToJsonYangWithPut() throws SvcLogicException {
336         String pre = "test-yang_cont1.cont2.";
337         SvcLogicContext ctx = createAttListYang(pre);
338         p.put("dirPath", "src/test/resources");
339         p.put("format", "json");
340         p.put("httpMethod", "put");
341         p.put("restapiUrl", "http://echo.getpostman" +
342                 ".com/restconf/operations/test-yang:cont1/cont2/cont4");
343         restconf.sendRequest(p, ctx);
344         assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG_PUT));
345     }
346
347     /**
348      * Verifies encoding of parameters to JSON data format with containers,
349      * grouping and augment for patch operation-type.
350      *
351      * @throws SvcLogicException when test case fails
352      */
353     @Test
354     public void encodeToJsonYangWithPatch() throws SvcLogicException {
355         String pre = "test-yang_cont1.cont2.";
356         SvcLogicContext ctx = createAttListYang(pre);
357         p.put("dirPath", "src/test/resources");
358         p.put("format", "json");
359         p.put("httpMethod", "patch");
360         p.put("restapiUrl", "http://echo.getpostman" +
361                 ".com/restconf/operations/test-yang:cont1/cont2/cont4");
362         restconf.sendRequest(p, ctx);
363         assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG_PUT));
364     }
365
366     /**
367      * Verifies encoding of parameters to JSON data format with augment as
368      * root child.
369      *
370      * @throws SvcLogicException when test case fails
371      */
372     @Test
373     public void encodeToJsonWithAugAsRootChild() throws SvcLogicException {
374         String pre = "test-yang_cont1.cont2.";
375         SvcLogicContext ctx = createAttListYang(pre);
376         p.put("dirPath", "src/test/resources");
377         p.put("format", "json");
378         p.put("httpMethod", "post");
379         p.put("restapiUrl", "http://echo.getpostman" +
380                 ".com/restconf/operations/test-yang:cont1/cont2/cont4");
381         restconf.sendRequest(p, ctx);
382         assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG_AUG_POST));
383     }
384
385     /**
386      * Verifies decoding of parameters from JSON data format with containers,
387      * grouping and augment.
388      *
389      * @throws SvcLogicException when test case fails
390      */
391     @Test
392     public void decodeToJsonYang() throws SvcLogicException {
393         createMockForDecode(ENCODE_TO_JSON_YANG);
394         SvcLogicContext ctx = new SvcLogicContext();
395         String pre = "test-yang_cont1.cont2.";
396         p.put("dirPath", "src/test/resources");
397         p.put("format", "json");
398         p.put("httpMethod", "get");
399         p.put("restapiUrl", "http://echo.getpostman" +
400                 ".com/restconf/operations/test-yang:cont1");
401         restconf.sendRequest(p, ctx);
402         verifyAttListYang(ctx, pre);
403     }
404
405     /**
406      * Verifies encoding of parameters to XML data format with containers,
407      * grouping and augment.
408      *
409      * @throws SvcLogicException when test case fails
410      */
411     @Test
412     public void encodeToXmlYang() throws SvcLogicException {
413         String pre = "test-yang_cont1.cont2.";
414         SvcLogicContext ctx = createAttListYang(pre);
415         p.put("dirPath", "src/test/resources");
416         p.put("format", "xml");
417         p.put("httpMethod", "post");
418         p.put("restapiUrl", "http://echo.getpostman" +
419                 ".com/restconf/operations/test-yang:cont1");
420         restconf.sendRequest(p, ctx);
421         assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_YANG));
422     }
423
424     /**
425      * Verifies encoding of parameters to XML data format with containers,
426      * grouping and augment for put operation-type
427      *
428      * @throws SvcLogicException when test case fails
429      */
430     @Test
431     public void encodeToXmlYangWithPut() throws SvcLogicException {
432         String pre = "test-yang_cont1.cont2.";
433         SvcLogicContext ctx = createAttListYang(pre);
434         p.put("dirPath", "src/test/resources");
435         p.put("format", "xml");
436         p.put("httpMethod", "put");
437         p.put("restapiUrl", "http://echo.getpostman" +
438                 ".com/restconf/operations/test-yang:cont1/cont2/cont4");
439         restconf.sendRequest(p, ctx);
440         assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_YANG_PUT));
441     }
442
443     /**
444      * Verifies encoding of parameters to XML data format with containers,
445      * grouping and augment for patch operation-type
446      *
447      * @throws SvcLogicException when test case fails
448      */
449     @Test
450     public void encodeToXmlYangWithPatch() throws SvcLogicException {
451         String pre = "test-yang_cont1.cont2.";
452         SvcLogicContext ctx = createAttListYang(pre);
453         p.put("dirPath", "src/test/resources");
454         p.put("format", "xml");
455         p.put("httpMethod", "put");
456         p.put("restapiUrl", "http://echo.getpostman" +
457                 ".com/restconf/operations/test-yang:cont1/cont2/cont4");
458         restconf.sendRequest(p, ctx);
459         assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_YANG_PUT));
460     }
461
462     /**
463      * Verifies encoding of parameters to XML data format with augment as
464      * root child.
465      *
466      * @throws SvcLogicException when test case fails
467      */
468     @Test
469     public void encodeToXmlWithAugAsRootChild() throws SvcLogicException {
470         String pre = "test-yang_cont1.cont2.";
471         SvcLogicContext ctx = createAttListYang(pre);
472         p.put("dirPath", "src/test/resources");
473         p.put("format", "xml");
474         p.put("httpMethod", "post");
475         p.put("restapiUrl", "http://echo.getpostman" +
476                 ".com/restconf/operations/test-yang:cont1/cont2/cont4");
477         restconf.sendRequest(p, ctx);
478         assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_YANG_AUG_POST));
479     }
480
481     /**
482      * Verifies decoding of parameters from XML data format with containers,
483      * grouping and augment.
484      *
485      * @throws SvcLogicException when test case fails
486      */
487     @Test
488     public void decodeToXmlYang() throws SvcLogicException {
489         createMockForDecode(ENCODE_TO_XML_YANG);
490         SvcLogicContext ctx = new SvcLogicContext();
491         String pre = "test-yang_cont1.cont2.";
492         p.put("dirPath", "src/test/resources");
493         p.put("format", "xml");
494         p.put("httpMethod", "get");
495         p.put("restapiUrl", "http://echo.getpostman" +
496                 ".com/restconf/operations/test-yang:cont1");
497         restconf.sendRequest(p, ctx);
498         verifyAttListYang(ctx, pre);
499     }
500
501     /**
502      * Verifies encoding of and decoding from, JSON respectively for data
503      * format with containers, grouping and augment.
504      *
505      * @throws SvcLogicException when test case fails
506      */
507     @Test
508     public void codecToJsonRpc() throws SvcLogicException {
509         createMockForDecode(DECODE_FROM_JSON_RPC);
510         String inPre = "test-yang_create-sfc.input.";
511         String outPre = "test-yang_create-sfc.output.";
512         SvcLogicContext ctx = createAttListRpc(inPre);
513         p.put("dirPath", "src/test/resources");
514         p.put("format", "json");
515         p.put("httpMethod", "post");
516         p.put("restapiUrl", "http://echo.getpostman" +
517                 ".com/restconf/operations/test-yang:create-sfc");
518         restconf.sendRequest(p, ctx);
519         assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_RPC));
520         verifyAttListRpc(ctx, outPre);
521     }
522
523     /**
524      * Verifies encoding of and decoding from, JSON for ANYXML.
525      *
526      * @throws SvcLogicException when test case fails
527      */
528     @Test
529     public void codecForNormalAnyXml() throws SvcLogicException {
530         createMockForDecode(DECODE_ANYXML_RESPONSE);
531         String inPre = "execution-service_process.";
532         SvcLogicContext ctx = createAnyXmlAttList(inPre);
533         p.put("dirPath", "src/test/resources");
534         p.put("format", "json");
535         p.put("httpMethod", "post");
536         p.put("responsePrefix", "pp");
537         p.put("restapiUrl", "http://echo.getpostman" +
538                 ".com/api/v1/execution-service/process");
539         restconf.sendRequest(p, ctx);
540         assertThat(dfCaptor.getResult(), is(ENCODE_TO_ANYXML));
541         verifyOutputOfAnyXml(ctx);
542     }
543
544     /**
545      * Verifies encoding of and decoding from, XML respectively for data
546      * format with containers, grouping and augment.
547      *
548      * @throws SvcLogicException when test case fails
549      */
550     @Test
551     public void codecToXmlRpc() throws SvcLogicException {
552         createMockForDecode(DECODE_FROM_XML_RPC);
553         String inPre = "test-yang_create-sfc.input.";
554         String outPre = "test-yang_create-sfc.output.";
555         SvcLogicContext ctx = createAttListRpc(inPre);
556         p.put("dirPath", "src/test/resources");
557         p.put("format", "xml");
558         p.put("httpMethod", "post");
559         p.put("restapiUrl", "http://echo.getpostman" +
560                 ".com/restconf/operations/test-yang:create-sfc");
561         restconf.sendRequest(p, ctx);
562         assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_RPC));
563         verifyAttListRpc(ctx, outPre);
564     }
565
566     /**
567      * Verifies URL parser returning path with only schema information for all
568      * kind of URL.
569      *
570      * @throws SvcLogicException when test case fails
571      */
572     @Test
573     public void validateUrlParser() throws SvcLogicException {
574         String actVal = "identity-test:test";
575         String putId = "/for-put";
576         String url1 = "http://echo.getpostman.com/restconf/operations/" +
577                 actVal;
578         String url2 = "http://echo.getpostman.com/restconf/data/" + actVal;
579         String url3 = "https://echo.getpostman.com/restconf/operations/" +
580                 actVal;
581         String url4 = "https://echo.getpostman.com/restconf/data/" + actVal +
582                 putId;
583         String url5 = "http://localhost:8282/restconf/operations/" + actVal;
584         String url6 = "https://localhost:8282/restconf/operations/" + actVal;
585         String url7 = "http://localhost:8282/restconf/data/" + actVal +
586                 putId;
587         String url8 = "https://localhost:8282/restconf/data/" + actVal;
588         String url9 = "http://182.2.61.24:2250/restconf/data/" + actVal;
589         String url10 = "https://182.2.61.24:2250/restconf/operations/" + actVal;
590         String url11 = "https://182.2.61.24:2250/api/v1/execution-service" +
591                 "/process";
592         String url12 = "https://182.2.61.24:2250/api/v1/execution-service" +
593                 "/process/payload";
594         String url13 = "https://182.2.61.24:2250/api/v1/execution-service" +
595                 "/process/payload/";
596         String val1 = parseUrl(url1, POST);
597         String val2 = parseUrl(url2, GET);
598         String val3 = parseUrl(url3, PATCH);
599         String val4 = parseUrl(url4, PUT);
600         String val5 = parseUrl(url5, GET);
601         String val6 = parseUrl(url6, POST);
602         String val7 = parseUrl(url7, PUT);
603         String val8 = parseUrl(url8, POST);
604         String val9 = parseUrl(url9, GET);
605         String val10 = parseUrl(url10, POST);
606         String val11 = parseUrl(url11, POST);
607         String val12 = parseUrl(url12, POST);
608         String val13 = parseUrl(url13, POST);
609         assertThat(val1, is(actVal));
610         assertThat(val2, is(actVal));
611         assertThat(val3, is(actVal));
612         assertThat(val4, is(actVal + putId));
613         assertThat(val5, is(actVal));
614         assertThat(val6, is(actVal));
615         assertThat(val7, is(actVal + putId));
616         assertThat(val8, is(actVal));
617         assertThat(val9, is(actVal));
618         assertThat(val10, is(actVal));
619         assertThat(val11, is("execution-service:process"));
620         assertThat(val12, is("execution-service:process/payload"));
621         assertThat(val13, is("execution-service:process/payload/"));
622     }
623
624     /**
625      * Creates attribute list for encoding JSON or XML with ANYXML YANG
626      * file.
627      *
628      * @param pre prefix
629      * @return service logic context
630      */
631     private SvcLogicContext createAnyXmlAttList(String pre) {
632         SvcLogicContext ctx = new SvcLogicContext();
633         String pre1 = pre + "commonHeader.";
634         String pre2 = pre + "actionIdentifiers.";
635         ctx.setAttribute(pre + "isNonAppend", "true");
636         ctx.setAttribute(pre1 + "originatorId", "SDNC_DG");
637         ctx.setAttribute(pre1 + "requestId", "123456-1000");
638         ctx.setAttribute(pre1 + "subRequestId", "sub-123456-1000");
639         ctx.setAttribute(pre2 + "blueprintName",
640                          "baseconfiguration");
641         ctx.setAttribute(pre2 + "blueprintVersion", "1.0.0");
642         ctx.setAttribute(pre2 + "actionName", "assign-activate");
643         ctx.setAttribute(pre2 + "mode", "sync");
644         ctx.setAttribute(pre + "payload." +
645                                  "template-prefix", "vDNS-test");
646         ctx.setAttribute(pre + "payload.resource-assignment-request" +
647                                  ".resource-assignment-properties",
648                          "{\n" +
649                                  "                \"service-instance-id\": " +
650                                  "\"1234\",\n" +
651                                  "                \"vnf-id\": \"3526\",\n" +
652                                  "                \"customer-name\": \"htipl\",\n" +
653                                  "                \"subscriber-name\": \"huawei\"\n" +
654                                  "            }");
655         return ctx;
656     }
657
658     /**
659      * Creates attribute list for encoding JSON or XML with identity-ref YANG
660      * file.
661      *
662      * @param pre prefix
663      * @return service logic context
664      */
665     private SvcLogicContext createAttList(String pre) {
666         SvcLogicContext ctx = new SvcLogicContext();
667         String pre1 = pre + "con1.interfaces.";
668         ctx.setAttribute(pre + "con1.interface", "identity-types:physical");
669         ctx.setAttribute(pre1 + "int-list[0].iden", "optical");
670         ctx.setAttribute(pre1 + "int-list[0].available.ll[0]", "Giga");
671         ctx.setAttribute(pre1 + "int-list[0].available.ll[1]",
672                          "identity-types:Loopback");
673         ctx.setAttribute(pre1 + "int-list[0].available.ll[2]",
674                          "identity-types-second:Ethernet");
675         ctx.setAttribute(pre1 + "int-list[0].available.leaf1", "58");
676         ctx.setAttribute(pre1 + "int-list[0].available.leaf2",
677                          "identity-types-second:iden2");
678
679         ctx.setAttribute(pre1 + "int-list[1].iden", "214748364");
680         ctx.setAttribute(pre1 + "int-list[1].available.ll[0]", "Giga");
681         ctx.setAttribute(pre1 + "int-list[1].available.ll[1]",
682                          "identity-types:Loopback");
683         ctx.setAttribute(pre1 + "int-list[1].available.ll[2]",
684                          "identity-types-second:Ethernet");
685         ctx.setAttribute(pre1 + "int-list[1].available.leaf1",
686                          "8888");
687         ctx.setAttribute(pre1 + "int-list[1].available.leaf2",
688                          "identity-types-second:iden2");
689         return ctx;
690     }
691
692     /**
693      * Creates attribute list for encoding JSON or XML with container,
694      * grouping and augmented YANG file.
695      *
696      * @param pre prefix
697      * @return service logic context
698      */
699     private SvcLogicContext createAttListYang(String pre) {
700         SvcLogicContext ctx = new SvcLogicContext();
701         ctx.setAttribute(pre + "cont3.leaf10", "abc");
702         ctx.setAttribute(pre + "list1[0].leaf1", "true");
703         ctx.setAttribute(pre + "list1[0].leaf2", "abc");
704         ctx.setAttribute(pre + "list1[0].leaf3", "abc");
705         ctx.setAttribute(pre + "list1[0].ll1[0]", "abc");
706         ctx.setAttribute(pre + "list1[0].ll1[1]", "abc");
707         ctx.setAttribute(pre + "list1[0].ll2[0]", "abc");
708         ctx.setAttribute(pre + "list1[0].ll2[1]", "abc");
709         ctx.setAttribute(pre + "list1[0].cont4.leaf11", "abc");
710         ctx.setAttribute(pre + "list1[0].list4[0].leaf8", "abc");
711         ctx.setAttribute(pre + "list1[0].list4[1].leaf8", "abc");
712         ctx.setAttribute(pre + "list1[0].list5[0].leaf9", "abc");
713         ctx.setAttribute(pre + "list1[0].list5[1].leaf9", "abc");
714         ctx.setAttribute(pre + "list1[1].leaf1", "true");
715         ctx.setAttribute(pre + "list1[1].leaf2", "abc");
716         ctx.setAttribute(pre + "list1[1].leaf3", "abc");
717         ctx.setAttribute(pre + "list1[1].ll1[0]", "abc");
718         ctx.setAttribute(pre + "list1[1].ll1[1]", "abc");
719         ctx.setAttribute(pre + "list1[1].ll2[0]", "abc");
720         ctx.setAttribute(pre + "list1[1].ll2[1]", "abc");
721         ctx.setAttribute(pre + "list1[1].cont4.leaf11", "abc");
722         ctx.setAttribute(pre + "list1[1].list4[0].leaf8", "abc");
723         ctx.setAttribute(pre + "list1[1].list4[1].leaf8", "abc");
724         ctx.setAttribute(pre + "list1[1].list5[0].leaf9", "abc");
725         ctx.setAttribute(pre + "list1[1].list5[1].leaf9", "abc");
726         ctx.setAttribute(pre + "list2[0].leaf4", "abc");
727         ctx.setAttribute(pre + "list2[1].leaf4", "abc");
728         ctx.setAttribute(pre + "leaf5", "abc");
729         ctx.setAttribute(pre + "leaf6", "abc");
730         ctx.setAttribute(pre + "ll3[0]", "abc");
731         ctx.setAttribute(pre + "ll3[1]", "abc");
732         ctx.setAttribute(pre + "ll4[0]", "abc");
733         ctx.setAttribute(pre + "ll4[1]", "abc");
734         ctx.setAttribute(pre + "cont4.leaf10", "abc");
735         ctx.setAttribute(pre + "list6[0].leaf11", "abc");
736         ctx.setAttribute(pre + "list6[1].leaf11", "abc");
737         ctx.setAttribute(pre + "leaf12", "abc");
738         ctx.setAttribute(pre + "ll5[0]", "abc");
739         ctx.setAttribute(pre + "ll5[1]", "abc");
740         ctx.setAttribute(pre + "cont4.test-augment_cont5.leaf13", "true");
741         ctx.setAttribute(pre + "cont4.test-augment_list7[0].leaf14", "test");
742         ctx.setAttribute(pre + "cont4.test-augment_list7[1].leaf14", "create");
743         ctx.setAttribute(pre + "cont4.test-augment_leaf15", "abc");
744         ctx.setAttribute(pre + "cont4.test-augment_ll6[0]", "unbounded");
745         ctx.setAttribute(pre + "cont4.test-augment_ll6[1]", "8");
746         ctx.setAttribute(pre + "cont4.test-augment_cont13.cont12.leaf26",
747                          "abc");
748         ctx.setAttribute(pre + "cont4.test-augment_cont13.list9[0].leaf27",
749                          "abc");
750         ctx.setAttribute(pre + "cont4.test-augment_cont13.list9[1].leaf27",
751                          "abc");
752         ctx.setAttribute(pre + "cont4.test-augment_cont13.leaf28", "abc");
753         ctx.setAttribute(pre + "cont4.test-augment_cont13.ll9[0]", "abc");
754         ctx.setAttribute(pre + "cont4.test-augment_cont13.ll9[1]", "abc");
755         return ctx;
756     }
757
758     /**
759      * Creates attribute list for encoding JSON or XML with RPC YANG file.
760      *
761      * @param pre prefix
762      * @return service logic context
763      */
764     private SvcLogicContext createAttListRpc(String pre) {
765         SvcLogicContext ctx = new SvcLogicContext();
766         ctx.setAttribute(pre + "cont14.leaf28", "abc");
767         ctx.setAttribute(pre + "list10[0].leaf29", "abc");
768         ctx.setAttribute(pre + "list10[1].leaf29", "abc");
769         ctx.setAttribute(pre + "leaf30", "abc");
770         ctx.setAttribute(pre + "ll10[0]", "abc");
771         ctx.setAttribute(pre + "ll10[1]", "abc");
772         ctx.setAttribute(pre + "cont15.leaf31", "abc");
773         ctx.setAttribute(pre + "cont13.list9[0].leaf27", "abc");
774         ctx.setAttribute(pre + "cont13.list9[1].leaf27", "abc");
775         ctx.setAttribute(pre + "cont13.leaf28", "abc");
776         ctx.setAttribute(pre + "cont13.ll9[0]", "abc");
777         ctx.setAttribute(pre + "cont13.ll9[1]", "abc");
778         return ctx;
779     }
780
781     /**
782      * Verifies the attribute list for decoding from JSON or XML with
783      * identity-ref YANG file.
784      *
785      * @param ctx service logic context
786      * @param pre prefix
787      */
788     private void verifyAttList(SvcLogicContext ctx, String pre) {
789         String pre1 = pre + "con1.interfaces.";
790         assertThat(ctx.getAttribute(pre + "con1.interface"), is(
791                 "identity-types:physical"));
792         assertThat(ctx.getAttribute(pre + "con1.interface"), is(
793                 "identity-types:physical"));
794         assertThat(ctx.getAttribute(pre1 + "int-list[0].iden"), is("optical"));
795         assertThat(ctx.getAttribute(pre1 + "int-list[0].available.ll[0]"), is(
796                 "Giga"));
797         assertThat(ctx.getAttribute(pre1 + "int-list[0].available.ll[1]"), is(
798                 "identity-types:Loopback"));
799         assertThat(ctx.getAttribute(pre1 + "int-list[0].available.ll[2]"), is(
800                 "identity-types-second:Ethernet"));
801         assertThat(ctx.getAttribute(pre1 + "int-list[0].available.leaf1"), is(
802                 "58"));
803         assertThat(ctx.getAttribute(pre1 + "int-list[0].available.leaf2"), is(
804                 "identity-types-second:iden2"));
805
806         assertThat(ctx.getAttribute(pre1 + "int-list[1].iden"), is(
807                 "214748364"));
808         assertThat(ctx.getAttribute(pre1 + "int-list[1].available.ll[0]"), is(
809                 "Giga"));
810         assertThat(ctx.getAttribute(pre1 + "int-list[1].available.ll[1]"), is(
811                 "identity-types:Loopback"));
812         assertThat(ctx.getAttribute(pre1 + "int-list[1].available.ll[2]"), is(
813                 "identity-types-second:Ethernet"));
814         assertThat(ctx.getAttribute(pre1 + "int-list[1].available.leaf1"), is(
815                 "8888"));
816         assertThat(ctx.getAttribute(pre1 + "int-list[1].available.leaf2"), is(
817                 "identity-types-second:iden2"));
818     }
819
820     /**
821      * Verifies the attribute list for decoding from JSON or XML with
822      * container, grouping and augmented file.
823      *
824      * @param ctx service logic context
825      * @param pre prefix
826      */
827     private void verifyAttListYang(SvcLogicContext ctx, String pre) {
828         assertThat(ctx.getAttribute(pre + "cont3.leaf10"), is("abc"));
829         assertThat(ctx.getAttribute(pre + "list1[0].leaf1"), is("true"));
830         assertThat(ctx.getAttribute(pre + "list1[0].leaf2"), is("abc"));
831         assertThat(ctx.getAttribute(pre + "list1[0].leaf3"), is("abc"));
832         assertThat(ctx.getAttribute(pre + "list1[0].ll1[0]"), is("abc"));
833         assertThat(ctx.getAttribute(pre + "list1[0].ll1[1]"), is("abc"));
834         assertThat(ctx.getAttribute(pre + "list1[0].ll2[0]"), is("abc"));
835         assertThat(ctx.getAttribute(pre + "list1[0].ll2[1]"), is("abc"));
836         assertThat(ctx.getAttribute(pre + "list1[0].cont4.leaf11"), is("abc"));
837         assertThat(ctx.getAttribute(pre + "list1[0].list4[0].leaf8"),
838                    is("abc"));
839         assertThat(ctx.getAttribute(pre + "list1[0].list4[1].leaf8"),
840                    is("abc"));
841         assertThat(ctx.getAttribute(pre + "list1[0].list5[0].leaf9"),
842                    is("abc"));
843         assertThat(ctx.getAttribute(pre + "list1[0].list5[1].leaf9"),
844                    is("abc"));
845         assertThat(ctx.getAttribute(pre + "list1[1].leaf1"), is("true"));
846         assertThat(ctx.getAttribute(pre + "list1[1].leaf2"), is("abc"));
847         assertThat(ctx.getAttribute(pre + "list1[1].leaf3"), is("abc"));
848         assertThat(ctx.getAttribute(pre + "list1[1].ll1[0]"), is("abc"));
849         assertThat(ctx.getAttribute(pre + "list1[1].ll1[1]"), is("abc"));
850         assertThat(ctx.getAttribute(pre + "list1[1].ll2[0]"), is("abc"));
851         assertThat(ctx.getAttribute(pre + "list1[1].ll2[1]"), is("abc"));
852         assertThat(ctx.getAttribute(pre + "list1[1].cont4.leaf11"), is("abc"));
853         assertThat(ctx.getAttribute(pre + "list1[1].list4[0].leaf8"),
854                    is("abc"));
855         assertThat(ctx.getAttribute(pre + "list1[1].list4[1].leaf8"),
856                    is("abc"));
857         assertThat(ctx.getAttribute(pre + "list1[1].list5[0].leaf9"),
858                    is("abc"));
859         assertThat(ctx.getAttribute(pre + "list1[1].list5[1].leaf9"),
860                    is("abc"));
861         assertThat(ctx.getAttribute(pre + "list2[0].leaf4"), is("abc"));
862         assertThat(ctx.getAttribute(pre + "list2[1].leaf4"), is("abc"));
863         assertThat(ctx.getAttribute(pre + "leaf5"), is("abc"));
864         assertThat(ctx.getAttribute(pre + "leaf6"), is("abc"));
865         assertThat(ctx.getAttribute(pre + "ll3[0]"), is("abc"));
866         assertThat(ctx.getAttribute(pre + "ll3[1]"), is("abc"));
867         assertThat(ctx.getAttribute(pre + "ll4[0]"), is("abc"));
868         assertThat(ctx.getAttribute(pre + "ll4[1]"), is("abc"));
869         assertThat(ctx.getAttribute(pre + "cont4.leaf10"), is( "abc"));
870         assertThat(ctx.getAttribute(pre + "list6[0].leaf11"), is("abc"));
871         assertThat(ctx.getAttribute(pre + "list6[1].leaf11"), is("abc"));
872         assertThat(ctx.getAttribute(pre + "leaf12"), is("abc"));
873         assertThat(ctx.getAttribute(pre + "ll5[0]"), is("abc"));
874         assertThat(ctx.getAttribute(pre + "ll5[1]"), is("abc"));
875         assertThat(ctx.getAttribute(pre + "cont4.test-augment_cont5.leaf13"),
876                    is("true"));
877         assertThat(ctx.getAttribute(pre + "cont4.test-augment_list7[0].leaf14"),
878                    is("test"));
879         assertThat(ctx.getAttribute(pre + "cont4.test-augment_list7[1].leaf14"),
880                    is("create"));
881         assertThat(ctx.getAttribute(pre + "cont4.test-augment_leaf15"),
882                    is("abc"));
883         assertThat(ctx.getAttribute(pre + "cont4.test-augment_ll6[0]"),
884                    is("unbounded"));
885         assertThat(ctx.getAttribute(pre + "cont4.test-augment_ll6[1]"),
886                    is("8"));
887         assertThat(ctx.getAttribute(pre + "cont4.test-augment_cont13" +
888                                             ".cont12.leaf26"), is("abc"));
889         assertThat(ctx.getAttribute(pre + "cont4.test-augment_cont13.list9[0]" +
890                                             ".leaf27"), is("abc"));
891         assertThat(ctx.getAttribute(pre + "cont4.test-augment_cont13.list9[1]" +
892                                             ".leaf27"), is("abc"));
893         assertThat(ctx.getAttribute(pre + "cont4.test-augment_cont13.leaf28"),
894                    is("abc"));
895         assertThat(ctx.getAttribute(pre + "cont4.test-augment_cont13.ll9[0]"),
896                    is("abc"));
897         assertThat(ctx.getAttribute(pre + "cont4.test-augment_cont13.ll9[1]"),
898                    is("abc"));
899     }
900
901     /**
902      * Verifies the attribute list for decoding from JSON or XML with
903      * RPC YANG file.
904      *
905      * @param ctx service logic context
906      * @param pre prefix
907      */
908     private void verifyAttListRpc(SvcLogicContext ctx, String pre) {
909         assertThat(ctx.getAttribute(pre + "cont16.leaf32"), is("abc"));
910         assertThat(ctx.getAttribute(pre + "list11[0].leaf33"), is("abc"));
911         assertThat(ctx.getAttribute(pre + "list11[1].leaf33"), is("abc"));
912         assertThat(ctx.getAttribute(pre + "leaf34"), is("abc"));
913         assertThat(ctx.getAttribute(pre + "ll11[0]"), is("abc"));
914         assertThat(ctx.getAttribute(pre + "ll11[1]"), is("abc"));
915         assertThat(ctx.getAttribute(pre + "cont17.leaf35"), is("abc"));
916         assertThat(ctx.getAttribute(pre + "cont13.cont12.leaf26"), is("abc"));
917         assertThat(ctx.getAttribute(pre + "cont13.list9[0].leaf27"), is("abc"));
918         assertThat(ctx.getAttribute(pre + "cont13.list9[1].leaf27"), is("abc"));
919         assertThat(ctx.getAttribute(pre + "cont13.ll9[0]"), is("abc"));
920         assertThat(ctx.getAttribute(pre + "cont13.ll9[1]"), is("abc"));
921         assertThat(ctx.getAttribute(pre + "cont13.leaf28"), is("abc"));
922     }
923
924     /**
925      * Verifies the attribute list for decoding from JSON or XML with
926      * ANYXML YANG file.
927      *
928      * @param ctx service logic context
929      */
930     private void verifyOutputOfAnyXml(SvcLogicContext ctx) {
931         System.out.println(ctx.getAttribute("pp.status.eventType"));
932         assertThat(ctx.getAttribute("pp.status.eventType"), is(
933                 "EVENT_COMPONENT_EXECUTED"));
934         assertThat(ctx.getAttribute("pp.actionIdentifiers.blueprintName"),
935                    is("golden"));
936         assertThat(ctx.getAttribute("pp.actionIdentifiers.mode"),
937                    is("sync"));
938         assertThat(ctx.getAttribute("pp.stepData.name"),
939                    is("resource-assignment"));
940         assertThat(ctx.getAttribute("pp.status.message"),
941                    is("success"));
942         assertThat(ctx.getAttribute("pp.commonHeader.originatorId"),
943                    is("System"));
944         assertThat(ctx.getAttribute("pp.status.code"),
945                    is("200"));
946         assertThat(ctx.getAttribute("pp.commonHeader.requestId"),
947                    is("1234"));
948         assertThat(ctx.getAttribute("pp.commonHeader.subRequestId"),
949                    is("1234-12234"));
950         assertThat(ctx.getAttribute("pp.commonHeader.timestamp"),
951                    is("2019-05-18T23:42:41.658Z"));
952         assertThat(ctx.getAttribute("pp.status.timestamp"),
953                    is("2019-05-18T23:42:41.950Z"));
954         assertThat(ctx.getAttribute("pp.actionIdentifiers.blueprintV" +
955                                             "ersion"), is("1.0.0"));
956         assertThat(ctx.getAttribute("pp.actionIdentifiers.actionName"),
957                    is("resource-assignment"));
958         assertThat(ctx.getAttribute("pp.payload.resource-assignment-resp" +
959                                             "onse.meshed-template.vf-module-1"),
960                    is("<interface>\n    <description>This i" +
961                               "s the Virtual Firewall entity</description>\n" +
962                               "    <vfw>10.0.101.20/24</vfw>\n" +
963                               "</interface>"));
964         assertThat(ctx.getAttribute("pp.actionIdentifiers.actionName"),
965                    is("resource-assignment"));
966     }
967
968
969     /**
970      * Captures the data format messages by mocking it, which can be used in
971      * testing the value.
972      *
973      * @param <String> capturing data format
974      */
975     public class DfCaptor<String> implements Answer {
976
977         private String result;
978
979         /**
980          * Returns the captured data format message.
981          *
982          * @return data format message.
983          */
984         public String getResult() {
985             return result;
986         }
987
988         @Override
989         public String answer(InvocationOnMock invocationOnMock)
990                 throws Throwable {
991             result = (String) invocationOnMock.callRealMethod();
992             return result;
993         }
994     }
995
996 }