78e846aef2e13d3f913e3d72fa8235d3458a0098
[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
29 import java.util.HashMap;
30 import java.util.Random;
31
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 @SuppressWarnings("unused")
40 public class SliPluginUtils_ctxSortList {
41         private static final Logger LOG = LoggerFactory.getLogger(SliPluginUtils_ctxSortList.class);
42         SliPluginUtils utils = new SliPluginUtils();
43         SvcLogicContext ctx;
44         HashMap<String, String> parameters;
45         Random rand = new Random();
46
47         @Before
48         public void setUp() throws Exception {
49                 this.ctx = new SvcLogicContext();
50                 this.parameters = new HashMap<String, String>();
51         }
52
53         @Test
54         public final void list_of_containers() throws SvcLogicException {
55                 this.parameters.put("list", "input.list");
56                 this.parameters.put("sort-fields", "sort-key");
57                 this.parameters.put("delimiter", ",");
58
59                 ctx.setAttribute("input.list_length", "10");
60                 for (int i = 0; i < 10; i++) {
61                         this.ctx.setAttribute("input.list[" + i + "].sort-key", Integer.toString(rand.nextInt(10)));
62                         this.ctx.setAttribute("input.list[" + i + "].value", Integer.toString(rand.nextInt(10)));
63                 }
64
65                 LOG.trace("BEFORE SORT:");
66                 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
67
68                 utils.ctxSortList(this.parameters, this.ctx);
69
70                 LOG.trace("AFTER SORT:");
71                 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
72
73                 for (int i = 0; i < 9; i++) {
74                         assertTrue(this.ctx.getAttribute("input.list[" + i + "].sort-key")
75                                         .compareTo(this.ctx.getAttribute("input.list[" + (i + 1) + "].sort-key")) < 1);
76                 }
77         }
78
79         @Test
80         public final void list_of_elements() throws SvcLogicException {
81                 this.parameters.put("list", "input.list");
82                 this.parameters.put("delimiter", ",");
83
84                 this.ctx.setAttribute("input.list_length", "10");
85                 for (int i = 0; i < 10; i++) {
86                         this.ctx.setAttribute("input.list[" + i + ']', Integer.toString(rand.nextInt(10)));
87                 }
88
89                 LOG.trace("BEFORE SORT:");
90                 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
91
92                 utils.ctxSortList(this.parameters, this.ctx);
93
94                 LOG.trace("AFTER SORT:");
95                 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
96
97                 for (int i = 0; i < 9; i++) {
98                         assertTrue(this.ctx.getAttribute("input.list[" + i + ']')
99                                         .compareTo(this.ctx.getAttribute("input.list[" + (i + 1) + ']')) < 1);
100                 }
101         }
102
103         @Test
104         public void testGenerateUUID() throws SvcLogicException {
105                 SliPluginUtils utils = new SliPluginUtils();
106                 this.parameters.put("ctx-destination", "testDestination");
107                 utils.generateUUID(this.parameters, ctx);
108         }
109
110         @Test
111         public void testSubstring() throws SvcLogicException {
112                 SliPluginUtils utils = new SliPluginUtils();
113                 this.parameters.put("string", "testString");
114                 this.parameters.put("begin-index", "1");
115                 this.parameters.put("result", "testResult");
116                 this.parameters.put("end-index", "5");
117                 utils.substring(this.parameters, ctx);
118                 assertEquals("estS", ctx.getAttribute("testResult"));
119         }
120
121         @Test
122         public void testSubstringForNullEndIndex() throws SvcLogicException {
123                 SliPluginUtils utils = new SliPluginUtils();
124                 this.parameters.put("string", "testString");
125                 this.parameters.put("begin-index", "1");
126                 this.parameters.put("result", "testResult");
127                 utils.substring(this.parameters, ctx);
128                 assertEquals("estString", ctx.getAttribute("testResult"));
129         }
130
131         @Test
132         public void testCtxBulkCopy() {
133                 ctx.setAttribute("Mykey1", "MyValue1");
134                 ctx.setAttribute("Mykey2", "MyValue2");
135                 SliPluginUtils.ctxBulkCopy(ctx, "Mykey", "test.");
136                 assertEquals("MyValue1", ctx.getAttribute("test.1"));
137                 assertEquals("MyValue2", ctx.getAttribute("test.2"));
138         }
139
140         @Test
141         public void testSetPropertiesForList() {
142                 parameters.put("prefixKey", "testPrefixKey");
143                 parameters.put("valuePrefixKey", "testPrefixValue");
144                 parameters.put("keyName", "testKey");
145                 parameters.put("keyValue", "testValue");
146
147                 assertEquals("success", SliPluginUtils.setPropertiesForList(parameters, ctx));
148
149         }
150 }