github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/com/sap/piper/MapUtilsTest.groovy (about)

     1  package com.sap.piper
     2  
     3  import org.junit.Assert
     4  import org.junit.Test
     5  
     6  import static org.hamcrest.Matchers.is
     7  import static org.junit.Assert.assertThat
     8  
     9  class MapUtilsTest {
    10  
    11      @Test
    12      void testIsMap(){
    13          Assert.assertTrue('Map is not recognized as Map', MapUtils.isMap([:]))
    14          Assert.assertTrue('String is recognized as Map', !MapUtils.isMap('I am not a Map'))
    15          Assert.assertFalse('Null value is recognized as Map', MapUtils.isMap(null))
    16      }
    17  
    18      @Test
    19      void testMergeMapStraightForward(){
    20  
    21          Map a = [a: '1',
    22                   c: [d: '1',
    23                       e: '2']],
    24               b = [b: '2',
    25                    c: [d: 'x']]
    26  
    27          Map merged = MapUtils.merge(a, b)
    28  
    29          assert merged == [a: '1',
    30                            b: '2',
    31                            c: [d: 'x', e: '2']]
    32      }
    33  
    34      @Test
    35      void testMergeMapWithConflict(){
    36  
    37          Map a = [a: '1',
    38                   b: [c: 1]],
    39              b = [a: '2',
    40                   b: [c: 2]]
    41  
    42          Map merged = MapUtils.merge(a, b)
    43  
    44          assert merged == [a: '2',
    45                            b: [c: 2]]
    46      }
    47  
    48      @Test
    49      void testPruneNulls() {
    50  
    51          Map m = [a: '1',
    52                   b: 2,
    53                   c: [ d: 'abc',
    54                        e: '',
    55                        n2: null],
    56                   n1: null]
    57  
    58          assert MapUtils.pruneNulls(m) == [ a: '1',
    59                                             b: 2,
    60                                             c: [ d: 'abc',
    61                                                  e: '']]
    62      }
    63  
    64      @Test
    65      void testTraverse() {
    66          Map m = [a: 'x1', m:[b: 'x2', c: 'otherString']]
    67          MapUtils.traverse(m, { s -> (s.startsWith('x')) ? "replaced" : s})
    68          assert m == [a: 'replaced', m: [b: 'replaced', c: 'otherString']]
    69      }
    70  
    71      @Test
    72      void testGetByPath() {
    73          Map m = [trees: [oak: 5, beech :1], flowers:[rose: 23]]
    74  
    75          assertThat(MapUtils.getByPath(m, 'flowers'), is([rose: 23]))
    76          assertThat(MapUtils.getByPath(m, 'trees/oak'), is(5))
    77          assertThat(MapUtils.getByPath(m, 'trees/palm'), is(null))
    78      }
    79  
    80      @Test
    81      void testDeepCopy() {
    82  
    83          List l = ['a', 'b', 'c']
    84  
    85          def original = [
    86                  list: l,
    87                  set: (Set)['1', '2'],
    88                  nextLevel: [
    89                      list: ['x', 'y'],
    90                      duplicate: l,
    91                      set: (Set)[9, 8, 7]
    92                  ]
    93              ]
    94  
    95          def copy = MapUtils.deepCopy(original)
    96  
    97          assert ! copy.is(original)
    98          assert ! copy.list.is(original.list)
    99          assert ! copy.set.is(original.set)
   100          assert ! copy.nextLevel.list.is(original.nextLevel.list)
   101          assert ! copy.nextLevel.set.is(original.nextLevel.set)
   102          assert ! copy.nextLevel.duplicate.is(original.nextLevel.duplicate)
   103  
   104          // Within the original identical list is used twice, but the
   105          // assuption is that there are different lists in the copy.
   106          assert ! copy.nextLevel.duplicate.is(copy.list)
   107  
   108          assert copy == original
   109      }
   110  }