3d4469bf2feaededefa3f48b33f3e8b2706708c2
[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  * Modifications Copyright (C) 2018 IBM.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23  
24 package org.onap.ccsdk.sli.core.slipluginutils;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
28 import java.util.HashMap;
29 import java.util.Random;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicConstants;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 @SuppressWarnings("unused")
39 public class SliPluginUtils_ctxSortList {
40         private static final Logger LOG = LoggerFactory.getLogger(SliPluginUtils_ctxSortList.class);
41         SliPluginUtils utils = new SliPluginUtils();
42         SvcLogicContext ctx;
43         HashMap<String, String> parameters;
44         Random rand = new Random();
45
46         @Before
47         public void setUp() throws Exception {
48                 this.ctx = new SvcLogicContext();
49                 this.parameters = new HashMap<String, String>();
50         }
51
52         @Test
53         public final void list_of_containers() throws SvcLogicException {
54                 this.parameters.put("list", "input.list");
55                 this.parameters.put("sort-fields", "sort-key");
56                 this.parameters.put("delimiter", ",");
57
58                 ctx.setAttribute("input.list_length", "10");
59                 for (int i = 0; i < 10; i++) {
60                         this.ctx.setAttribute("input.list[" + i + "].sort-key", Integer.toString(rand.nextInt(10)));
61                         this.ctx.setAttribute("input.list[" + i + "].value", Integer.toString(rand.nextInt(10)));
62                 }
63
64                 LOG.trace("BEFORE SORT:");
65                 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
66
67                 utils.ctxSortList(this.parameters, this.ctx);
68
69                 LOG.trace("AFTER SORT:");
70                 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
71
72                 for (int i = 0; i < 9; i++) {
73                         assertTrue(this.ctx.getAttribute("input.list[" + i + "].sort-key")
74                                         .compareTo(this.ctx.getAttribute("input.list[" + (i + 1) + "].sort-key")) < 1);
75                 }
76         }
77
78         @Test
79         public final void list_of_elements() throws SvcLogicException {
80                 this.parameters.put("list", "input.list");
81                 this.parameters.put("delimiter", ",");
82
83                 this.ctx.setAttribute("input.list_length", "10");
84                 for (int i = 0; i < 10; i++) {
85                         this.ctx.setAttribute("input.list[" + i + ']', Integer.toString(rand.nextInt(10)));
86                 }
87
88                 LOG.trace("BEFORE SORT:");
89                 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
90
91                 utils.ctxSortList(this.parameters, this.ctx);
92
93                 LOG.trace("AFTER SORT:");
94                 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
95
96                 for (int i = 0; i < 9; i++) {
97                         assertTrue(this.ctx.getAttribute("input.list[" + i + ']')
98                                         .compareTo(this.ctx.getAttribute("input.list[" + (i + 1) + ']')) < 1);
99                 }
100         }
101
102         @Test
103         public void testGenerateUUID() throws SvcLogicException {
104                 SliPluginUtils utils = new SliPluginUtils();
105                 this.parameters.put("ctx-destination", "testDestination");
106                 utils.generateUUID(this.parameters, ctx);
107         }
108
109         @Test
110         public void testSubstring() throws SvcLogicException {
111                 SliPluginUtils utils = new SliPluginUtils();
112                 this.parameters.put("string", "testString");
113                 this.parameters.put("begin-index", "1");
114                 this.parameters.put("result", "testResult");
115                 this.parameters.put("end-index", "5");
116                 utils.substring(this.parameters, ctx);
117                 assertEquals("estS", ctx.getAttribute("testResult"));
118         }
119
120         @Test
121         public void testSubstringForNullEndIndex() throws SvcLogicException {
122                 SliPluginUtils utils = new SliPluginUtils();
123                 this.parameters.put("string", "testString");
124                 this.parameters.put("begin-index", "1");
125                 this.parameters.put("result", "testResult");
126                 utils.substring(this.parameters, ctx);
127                 assertEquals("estString", ctx.getAttribute("testResult"));
128         }
129
130         @Test
131         public void testCtxBulkCopy() {
132                 ctx.setAttribute("Mykey1", "MyValue1");
133                 ctx.setAttribute("Mykey2", "MyValue2");
134                 SliPluginUtils.ctxBulkCopy(ctx, "Mykey", "test.");
135                 assertEquals("MyValue1", ctx.getAttribute("test.1"));
136                 assertEquals("MyValue2", ctx.getAttribute("test.2"));
137         }
138
139         @Test
140         public void testSetPropertiesForList() {
141                 parameters.put("prefixKey", "testPrefixKey");
142                 parameters.put("valuePrefixKey", "testPrefixValue");
143                 parameters.put("keyName", "testKey");
144                 parameters.put("keyValue", "testValue");
145
146                 assertEquals(SvcLogicConstants.SUCCESS, SliPluginUtils.setPropertiesForList(parameters, ctx));
147
148         }
149 }