Eliminate ranges
[sdnc/core.git] / sliPluginUtils / provider / src / test / java / org / openecomp / sdnc / sli / SliPluginUtils / SliPluginUtils_ctxSortList.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdnc.sli.SliPluginUtils;
23
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.HashMap;
27 import java.util.Random;
28
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.openecomp.sdnc.sli.SvcLogicContext;
32 import org.openecomp.sdnc.sli.SvcLogicException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 @SuppressWarnings("unused")
37 public class SliPluginUtils_ctxSortList {
38         private static final Logger LOG = LoggerFactory.getLogger(SliPluginUtils_ctxSortList.class);
39         SliPluginUtils utils = new SliPluginUtils();
40         SvcLogicContext ctx;
41         HashMap<String,String> parameters;
42         Random rand = new Random();
43
44         @Before
45         public void setUp() throws Exception {
46                 this.ctx = new SvcLogicContext();
47                 this.parameters = new HashMap<String,String>();
48         }
49
50         @Test
51         public final void list_of_containers() throws SvcLogicException {
52                 this.parameters.put("list", "input.list");
53                 this.parameters.put("sort-fields", "sort-key");
54                 this.parameters.put("delimiter",",");
55
56                 ctx.setAttribute("input.list_length", "10");
57                 for( int i = 0; i < 10; i++ ) {
58                         this.ctx.setAttribute("input.list[" + i + "].sort-key", Integer.toString( rand.nextInt(10) ));
59                         this.ctx.setAttribute("input.list[" + i + "].value", Integer.toString( rand.nextInt(10) ));
60                 }
61
62                 LOG.trace("BEFORE SORT:");
63                 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
64
65                 utils.ctxSortList(this.parameters, this.ctx);
66
67                 LOG.trace("AFTER SORT:");
68                 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
69
70                 for( int i = 0; i < 9; i++ ) {
71                         assertTrue(this.ctx.getAttribute("input.list[" + i + "].sort-key").compareTo(this.ctx.getAttribute("input.list[" + (i+1) + "].sort-key")) < 1 );
72                 }
73         }
74
75         @Test public final void list_of_elements() throws SvcLogicException {
76                 this.parameters.put("list", "input.list");
77                 this.parameters.put("delimiter",",");
78
79                 this.ctx.setAttribute("input.list_length", "10");
80                 for( int i = 0; i < 10; i++ ) {
81                         this.ctx.setAttribute("input.list[" + i + ']', Integer.toString( rand.nextInt(10) ));
82                 }
83
84                 LOG.trace("BEFORE SORT:");
85                 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
86
87                 utils.ctxSortList(this.parameters, this.ctx);
88
89                 LOG.trace("AFTER SORT:");
90                 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
91
92                 for( int i = 0; i < 9; i++ ) {
93                         assertTrue(this.ctx.getAttribute("input.list[" + i + ']').compareTo(this.ctx.getAttribute("input.list[" + (i+1) + ']')) < 1 );
94                 }
95         }
96 }