2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.dao.utils;
23 import com.google.common.collect.ImmutableMap;
24 import org.junit.Test;
27 import java.util.function.Function;
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.assertj.core.api.Assertions.assertThatThrownBy;
31 import static org.openecomp.sdc.be.dao.utils.MapUtil.mergeMaps;
33 public class MapUtilTest {
36 public void mergeMaps_whenBothMapsAreNull_returnEmptyMap() {
37 assertThat(mergeMaps(null, null)).isEmpty();
41 public void mergeMaps_whenFirstMapIsNull_returnSecondMap() {
42 ImmutableMap<String, String> second = ImmutableMap.of("a", "b", "c", "d");
43 assertThat(mergeMaps(null, second))
45 .containsAllEntriesOf(second);
49 public void mergeMaps_whenSecondMapsIsNull_returnFirstMap() {
50 ImmutableMap<String, String> first = ImmutableMap.of("a", "b", "c", "d");
51 assertThat(mergeMaps(first, null))
53 .containsAllEntriesOf(first);
57 public void mergeMaps_avoidDuplications_takeValFromFirstMap() {
58 ImmutableMap<String, String> first = ImmutableMap.of("key1", "val1", "key2", "val2");
59 ImmutableMap<String, String> second = ImmutableMap.of("key1", "val11", "key3", "val3");
60 assertThat(mergeMaps(first, second))
61 .containsEntry("key1", "val1")
62 .containsEntry("key2", "val2")
63 .containsEntry("key3", "val3");
66 public void testGet() throws Exception {
67 Map<String, ? extends Object> map = null;
72 result = MapUtil.get(map, path);
73 path = "\\mock\\mock";
74 result = MapUtil.get(map, path);
78 public void testGroupListBy() throws Exception {
79 Collection valuesToMap = new LinkedList<String>();
80 Function<String, String> groupingFunction = new Function<String, String>() {
83 public String apply(String t) {
87 Map<String, List<String>> result;
90 result = MapUtil.groupListBy(valuesToMap, groupingFunction);
94 public void testToMap() throws Exception {
95 Collection<String> valuesToMap = null;
96 Function<String, String> mappingFunction = null;
97 Map<String, String> result;
100 result = MapUtil.toMap(valuesToMap, mappingFunction);
104 public void testConvertMapKeys() throws Exception {
105 Map<String, List<String>> map = new HashMap<>();
106 Function<String, String> keyMappingFunction = new Function<String, String>() {
109 public String apply(String t) {
113 Map<String, List<String>> result;
116 result = MapUtil.convertMapKeys(map, keyMappingFunction);
120 public void testNewHashMap() throws Exception {
121 final String[] keys1 = new String[] { "mock" };
122 final String[] values1 = new String[] { "mock" };
123 Map<String, String> result;
126 result = MapUtil.newHashMap(keys1, values1);
129 final String[] keys2 = new String[] { "mock" };
130 final String[] values2 = null;
131 assertThatThrownBy(() -> MapUtil.newHashMap(keys2, values2))
132 .isInstanceOf(IllegalArgumentException.class);
135 final String[] keys3 = null;
136 final String[] values3 = null;
137 assertThatThrownBy(() -> MapUtil.newHashMap(keys3, values3))
138 .isInstanceOf(IllegalArgumentException.class);
141 final String[] values4 = new String[] { "mock" };
142 final String[] keys4 = null;
143 assertThatThrownBy(() -> MapUtil.newHashMap(keys4, values4))
144 .isInstanceOf(IllegalArgumentException.class);