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