Refactor dblib
[ccsdk/sli/core.git] / sliPluginUtils / provider / src / test / java / org / openecomp / sdnc / sli / SliPluginUtils / SliPluginUtils_ctxSortList.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 ONAP
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.openecomp.sdnc.sli.SliPluginUtils;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.util.HashMap;
26 import java.util.Random;
27
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 @SuppressWarnings("unused")
36 public class SliPluginUtils_ctxSortList {
37         private static final Logger LOG = LoggerFactory.getLogger(SliPluginUtils_ctxSortList.class);
38         SliPluginUtils utils = new SliPluginUtils();
39         SvcLogicContext ctx;
40         HashMap<String,String> parameters;
41         Random rand = new Random();
42
43         @Before
44         public void setUp() throws Exception {
45                 this.ctx = new SvcLogicContext();
46                 this.parameters = new HashMap<String,String>();
47         }
48
49         @Test
50         public final void list_of_containers() throws SvcLogicException {
51                 this.parameters.put("list", "input.list");
52                 this.parameters.put("sort-fields", "sort-key");
53                 this.parameters.put("delimiter",",");
54
55                 ctx.setAttribute("input.list_length", "10");
56                 for( int i = 0; i < 10; i++ ) {
57                         this.ctx.setAttribute("input.list[" + i + "].sort-key", Integer.toString( rand.nextInt(10) ));
58                         this.ctx.setAttribute("input.list[" + i + "].value", Integer.toString( rand.nextInt(10) ));
59                 }
60
61                 LOG.trace("BEFORE SORT:");
62                 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
63
64                 utils.ctxSortList(this.parameters, this.ctx);
65
66                 LOG.trace("AFTER SORT:");
67                 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
68
69                 for( int i = 0; i < 9; i++ ) {
70                         assertTrue(this.ctx.getAttribute("input.list[" + i + "].sort-key").compareTo(this.ctx.getAttribute("input.list[" + (i+1) + "].sort-key")) < 1 );
71                 }
72         }
73
74         @Test public final void list_of_elements() throws SvcLogicException {
75                 this.parameters.put("list", "input.list");
76                 this.parameters.put("delimiter",",");
77
78                 this.ctx.setAttribute("input.list_length", "10");
79                 for( int i = 0; i < 10; i++ ) {
80                         this.ctx.setAttribute("input.list[" + i + ']', Integer.toString( rand.nextInt(10) ));
81                 }
82
83                 LOG.trace("BEFORE SORT:");
84                 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
85
86                 utils.ctxSortList(this.parameters, this.ctx);
87
88                 LOG.trace("AFTER SORT:");
89                 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
90
91                 for( int i = 0; i < 9; i++ ) {
92                         assertTrue(this.ctx.getAttribute("input.list[" + i + ']').compareTo(this.ctx.getAttribute("input.list[" + (i+1) + ']')) < 1 );
93                 }
94         }
95 }