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