summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/openecomp/sparky/util/NodeUtilsTest.java
blob: 23d9df37fda85c210fedf4c25d13b015fb283dc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/* 
* ============LICENSE_START=======================================================
* SPARKY (AAI UI service)
* ================================================================================
* Copyright © 2017 AT&T Intellectual Property.
* Copyright © 2017 Amdocs
* All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*      http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
* 
* ECOMP and OpenECOMP are trademarks
* and service marks of AT&T Intellectual Property.
*/

package org.openecomp.sparky.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.xml.stream.XMLStreamConstants;

import org.json.JSONException;
import org.junit.Before;
import org.junit.Test;
import org.openecomp.sparky.dal.rest.OperationResult;

import com.fasterxml.jackson.core.JsonProcessingException;

/**
 * The Class NodeUtilsTest.
 */
public class NodeUtilsTest {


  private static final String TEST_LINK1 =
      "https://aai-ext1.test.att.com:9292/aai/v7/network/generic-vnfs/generic-vnf/cafaeb02-b54d-4918-bd06-85406dad19e7/l-interfaces/l-interface/WAN1_1123_GAMMA2016.04_PWT/l3-interface-ipv4-address-list/155.196.36.1/";
  private static final String TEST_LINK2 =
      "https://aai-ext1.test.att.com:9292/aai/v7/network/generic-vnfs/generic-vnf/cafaeb02-b54d-4918-bd06-85406dad19e7/l-interfaces/l-interface/WAN1_1123_GAMMA2016.04_PWT/l3-interface-ipv4-address-list/155.196.36.1";
  private static final String TEST_LINK3 =
      "https://aai-ext1.test.att.com:9292/aai/v7/network/generic-vnfs/generic-vnf/cafaeb02-b54d-4918-bd06-85406dad19e7/l-interfaces/l-interface/WAN1_1123_GAMMA2016.04_PWT/l3-interface-ipv4-address-list/ge-0%2f1%2f0";
  private static final String TEST_LINK4 =
      "https://aai-ext1.test.att.com:9292/aai/v7/network/generic-vnfs/generic-vnf/cafaeb02-b54d-4918-bd06-85406dad19e7/l-interfaces/l-interface/WAN1_1123_GAMMA2016.04_PWT/l3-interface-ipv4-address-list/ge-%bad%wolf%timelord";
  private static final String TEST_LINK5_NO_RESOURCE_ID =
      "https://aai-ext1.test.att.com:9292/aai/v7/network/generic-vnfs/generic-vnf/cafaeb02-b54d-4918-bd06-85406dad19e7/l-interfaces/l-interface/WAN1_1123_GAMMA2016.04_PWT/l3-interface-ipv4-address-list//";
  private static final int NODE_UTILS_TAB_WIDTH = 3;

  /**
   * Inits the.
   *
   * @throws Exception the exception
   */
  @Before
  public void init() throws Exception {}

  /*
   * String buildDepthPadding(int depth)
   */

  /**
   * Builds the depth padding with negative depth.
   */
  @Test
  public void buildDepthPaddingWithNegativeDepth() {
    String paddingString = NodeUtils.buildDepthPadding(-1);
    assertEquals(paddingString.length(), 0);
  }

  /**
   * Builds the depth padding with zero depth.
   */
  @Test
  public void buildDepthPaddingWithZeroDepth() {
    String paddingString = NodeUtils.buildDepthPadding(0);
    assertEquals(paddingString.length(), 0);
  }

  /**
   * Builds the depth padding with small depth.
   */
  @Test
  public void buildDepthPaddingWithSmallDepth() {
    String paddingString = NodeUtils.buildDepthPadding(1);
    assertEquals(paddingString.length(), NODE_UTILS_TAB_WIDTH * 1);
  }

  /**
   * Builds the depth padding with large depth.
   */
  @Test
  public void buildDepthPaddingWithLargeDepth() {
    String paddingString = NodeUtils.buildDepthPadding(100);
    assertEquals(paddingString.length(), NODE_UTILS_TAB_WIDTH * 100);
  }

  /*
   * String buildEntityResourceKey(String entityType, String resourceId)
   */

  /*
   * TODO: we should probably throw an IllegalArgumentExecption or just return null if a required
   * parameter is passed to us with a null.
   */

  /**
   * Builds the entity resource key with null entity type.
   */
  @Test
  public void buildEntityResourceKeyWithNullEntityType() {
    String resourceId = NodeUtils.buildEntityResourceKey(null, "generic-vnf-123");
    assertEquals(resourceId, "null.generic-vnf-123");
  }

  /**
   * Builds the entity resource key with null resource id.
   */
  @Test
  public void buildEntityResourceKeyWithNullResourceId() {
    String resourceId = NodeUtils.buildEntityResourceKey("generic-vnf", null);
    assertEquals(resourceId, "generic-vnf.null");
  }

  /**
   * Builds the entity resource key success path.
   */
  @Test
  public void buildEntityResourceKeySuccessPath() {
    String resourceId = NodeUtils.buildEntityResourceKey("generic-vnf", "generic-vnf-123");
    assertEquals(resourceId, "generic-vnf.generic-vnf-123");
  }

  /*
   * String extractResourceIdFromLink(String link)
   */

  /**
   * Id extraction when url has trailing forward slash.
   */
  @Test
  public void idExtractionWhenUrlHasTrailingForwardSlash() {

    String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK1);

    if (!"155.196.36.1".equals(resourceId)) {
      fail("Failed to extract expected resourceId");
    }
  }

  /**
   * Id extraction when url does not have trailing forward slash.
   */
  @Test
  public void idExtractionWhenUrlDoesNotHaveTrailingForwardSlash() {

    String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK2);

    if (!"155.196.36.1".equals(resourceId)) {
      fail("Failed to extract expected resourceId");
    }
  }

  /**
   * Id extraction when url contains url encoded hex characters.
   */
  @Test
  public void idExtractionWhenUrlContainsUrlEncodedHexCharacters() {

    String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK3);

    if (!"ge-0/1/0".equals(resourceId)) {
      fail("Failed to extract expected resourceId");
    }

  }

  /**
   * Id extraction when url contains non standard hex characters.
   */
  @Test
  public void idExtractionWhenUrlContainsNonStandardHexCharacters() {

    String resourceId = NodeUtils.extractResourceIdFromLink(TEST_LINK4);

    /*
     * This is not an expected hex encoding, so the decode will fail and the original parameter will
     * be returned instead.
     */

    if (!"ge-%bad%wolf%timelord".equals(resourceId)) {
      fail("Failed to extract expected resourceId");
    }

  }

  /**
   * Id extraction when url is null.
   */
  @Test
  public void idExtractionWhenUrlIsNull() {
    String resourceId = NodeUtils.extractResourceIdFromLink(null);
    assertEquals(null, resourceId);
  }

  /**
   * Id extraction when url is empty string.
   */
  @Test
  public void idExtractionWhenUrlIsEmptyString() {
    String resourceId = NodeUtils.extractResourceIdFromLink("");
    assertEquals(null, resourceId);
  }

  /*
   * String getXMLStreamConstantAsStr(int c)
   */

  /**
   * Test string conversion of xml stream constants.
   */
  @Test
  public void testStringConversionOfXmlStreamConstants() {

    /*
     * Range of enum is 0 - 256
     */

    for (int id = 0; id <= 256; id++) {

      switch (id) {
        case XMLStreamConstants.ATTRIBUTE: {
          assertEquals("ATTRIBUTE", NodeUtils.getXmlStreamConstantAsStr(id));
          break;
        }

        case XMLStreamConstants.CDATA: {
          assertEquals("CDATA", NodeUtils.getXmlStreamConstantAsStr(id));
          break;
        }

        case XMLStreamConstants.CHARACTERS: {
          assertEquals("CHARACTERS", NodeUtils.getXmlStreamConstantAsStr(id));
          break;
        }

        case XMLStreamConstants.COMMENT: {
          assertEquals("COMMENT", NodeUtils.getXmlStreamConstantAsStr(id));
          break;
        }

        case XMLStreamConstants.DTD: {
          assertEquals("DTD", NodeUtils.getXmlStreamConstantAsStr(id));
          break;
        }

        case XMLStreamConstants.END_DOCUMENT: {
          assertEquals("END_DOCUMENT", NodeUtils.getXmlStreamConstantAsStr(id));
          break;
        }

        case XMLStreamConstants.END_ELEMENT: {
          assertEquals("END_ELEMENT", NodeUtils.getXmlStreamConstantAsStr(id));
          break;
        }

        case XMLStreamConstants.ENTITY_DECLARATION: {
          assertEquals("ENTITY_DECLARATION", NodeUtils.getXmlStreamConstantAsStr(id));
          break;
        }

        case XMLStreamConstants.ENTITY_REFERENCE: {
          assertEquals("ENTITY_REFERENCE", NodeUtils.getXmlStreamConstantAsStr(id));
          break;
        }

        case XMLStreamConstants.NAMESPACE: {
          assertEquals("NAMESPACE", NodeUtils.getXmlStreamConstantAsStr(id));
          break;
        }

        case XMLStreamConstants.NOTATION_DECLARATION: {
          assertEquals("NOTATION_DECLARATION", NodeUtils.getXmlStreamConstantAsStr(id));
          break;
        }

        case XMLStreamConstants.PROCESSING_INSTRUCTION: {
          assertEquals("PROCESSING_INSTRUCTION", NodeUtils.getXmlStreamConstantAsStr(id));
          break;
        }

        case XMLStreamConstants.SPACE: {
          assertEquals("SPACE", NodeUtils.getXmlStreamConstantAsStr(id));
          break;
        }

        case XMLStreamConstants.START_DOCUMENT: {
          assertEquals("START_DOCUMENT", NodeUtils.getXmlStreamConstantAsStr(id));
          break;
        }

        case XMLStreamConstants.START_ELEMENT: {
          assertEquals("START_ELEMENT", NodeUtils.getXmlStreamConstantAsStr(id));
          break;
        }

        default:
          String result = NodeUtils.getXmlStreamConstantAsStr(id);
          assertNotNull(result);
          if (!result.startsWith("Unknown")) {
            fail("Unexecpted XML Stream Constant definition for id = " + id);
          }

      }

    }
  }

  /**
   * Convert object to json successful.
   *
   * @throws JsonProcessingException the json processing exception
   */
  @Test
  public void convertObjectToJsonSuccessful() throws JsonProcessingException {

    OperationResult opResult = new OperationResult(200, "op result");
    String asJson = NodeUtils.convertObjectToJson(opResult, false);

    assertTrue("Doesn't contain result field", asJson.contains("result"));
    assertTrue("Doesn't contain resultCode field", asJson.contains("resultCode"));
    assertTrue("Doesn't contain resolvedLinkFailure field", asJson.contains("resolvedLinkFailure"));

  }

  /**
   * Convert object to json successful pretty.
   *
   * @throws JsonProcessingException the json processing exception
   */
  @Test
  public void convertObjectToJsonSuccessful_pretty() throws JsonProcessingException {

    OperationResult opResult = new OperationResult(200, "op result");
    String asJson = NodeUtils.convertObjectToJson(opResult, true);

    assertTrue("Doesn't contain result field", asJson.contains("result"));
    assertTrue("Doesn't contain resultCode field", asJson.contains("resultCode"));
    assertTrue("Doesn't contain resolvedLinkFailure field", asJson.contains("resolvedLinkFailure"));

  }

  /**
   * Convert object to json failure caused by null.
   *
   * @throws JsonProcessingException the json processing exception
   */
  @Test()
  public void convertObjectToJsonFailure_causedBy_null() throws JsonProcessingException {

    String asJson = NodeUtils.convertObjectToJson(null, true);

    assertTrue("Doesn't contain result field", !asJson.contains("result"));
    assertTrue("Doesn't contain resultCode field", !asJson.contains("resultCode"));
    assertTrue("Doesn't contain resolvedLinkFailure field",
        !asJson.contains("resolvedLinkFailure"));

  }

  /**
   * Convert object to xml successful.
   *
   * @throws JsonProcessingException the json processing exception
   */
  @Test
  public void convertObjectToXmlSuccessful() throws JsonProcessingException {

    OperationResult opResult = new OperationResult(200, "op result");
    String asXml = NodeUtils.convertObjectToXml(opResult);

    assertTrue("Doesn't contain result field", asXml.contains("result"));
    assertTrue("Doesn't contain resultCode field", asXml.contains("resultCode"));
    assertTrue("Doesn't contain resolvedLinkFailure field", asXml.contains("resolvedLinkFailure"));

  }

  /**
   * Convert object to xml failure caused by null.
   *
   * @throws JsonProcessingException the json processing exception
   */
  @Test(expected = JSONException.class)
  public void convertObjectToXmlFailure_causedBy_null() throws JsonProcessingException {

    String asXml = NodeUtils.convertObjectToXml(null);
    assertNull("Output should be null", asXml);

  }

  /**
   * Validate concatonate list empty list.
   *
   * @throws JsonProcessingException the json processing exception
   */
  @Test
  public void validateConcatonateList_EmptyList() throws JsonProcessingException {

    String[] array = null;
    String result = NodeUtils.concatArray(array);
    assertEquals("", result);

    List<String> emptyList = Collections.emptyList();
    result = NodeUtils.concatArray(emptyList);
    assertEquals("", result);
  }

  /**
   * Validate concatonate list multiple values.
   *
   * @throws JsonProcessingException the json processing exception
   */
  @Test
  public void validateConcatonateList_MultipleValues() throws JsonProcessingException {

    List<String> numberList = new ArrayList<String>();

    numberList.add("1");
    numberList.add("2");
    numberList.add("3");

    String result = NodeUtils.concatArray(numberList);
    assertEquals("1 2 3", result);
  }
 
  /**
   * Test format timestamp expect valid result.
   */
  @Test
  public void test_formatTimestamp_expectValidResult() {
    String validTimeStamp = "20170111T123116Z";
    String result = NodeUtils.formatTimestamp(validTimeStamp);

    assertEquals("2017-01-11T12:31:16Z", result);
  }

  /**
   * Test format timestamp expect invalid result.
   */
  @Test
  public void test_formatTimestamp_expectInvalidResult() {
    String validTimeStamp = "#20170011T123116Z";
    String result = NodeUtils.formatTimestamp(validTimeStamp);

    assertEquals(validTimeStamp, result);
  }
  
  /**
   * test calculate edit attributes urls
   */
  @Test
  public void validateCalculateEditAttributeLogic() {
    
    assertEquals(NodeUtils.calculateEditAttributeUri("https://localhost:9000/aai/v7/pservers/pserver/12345"),"pservers/pserver/12345");
    assertEquals(NodeUtils.calculateEditAttributeUri("https://localhost:9000/aai/v1/pservers/pserver/12345"),"pservers/pserver/12345");
    assertEquals(NodeUtils.calculateEditAttributeUri("https://localhost:9000/aai/v21/pservers/pserver/12345"),"pservers/pserver/12345");
    assertEquals(NodeUtils.calculateEditAttributeUri("https://localhost:9000/aai/v211/pservers/pserver/12345"),"pservers/pserver/12345");
    assertEquals(NodeUtils.calculateEditAttributeUri("https://localhost:9000/aai/v5252/pservers/pserver/12345"),"pservers/pserver/12345");
    assertNull(NodeUtils.calculateEditAttributeUri(null));
    assertNull(NodeUtils.calculateEditAttributeUri("https://localhost:9000/aai/noVersionTag/pservers/pserver/12345"));

  }


}