Fix request handler class error
[appc.git] / appc-common / src / test / java / org / onap / appc / util / MessageFormatterTest.java
1 package org.onap.appc.util;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5
6 import com.google.common.collect.Maps;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11 import org.apache.commons.lang3.StringUtils;
12 import org.junit.Test;
13
14 public class MessageFormatterTest {
15
16     @Test
17     public void format_should_return_empty_string_when_given_null_or_empty_message_template() {
18         assertEquals(StringUtils.EMPTY, MessageFormatter.format(null, Maps.newHashMap()));
19         assertEquals(StringUtils.EMPTY, MessageFormatter.format(StringUtils.EMPTY, Maps.newHashMap()));
20     }
21
22     @Test
23     public void should_return_same_string_when_given_null_or_empty_params() {
24         String message = "message";
25
26         assertEquals(message, MessageFormatter.format(message, null));
27         assertEquals(message, MessageFormatter.format(message, Maps.newHashMap()));
28     }
29
30     @Test
31     public void should_return_same_string_when_given_non_dollar_string() {
32         String msg = "vnfid";
33
34         Map<String, Object> respMsg = new HashMap<>();
35         respMsg.put("vnfid", "SYNC_NEW201");
36
37         assertEquals(msg, MessageFormatter.format(msg, respMsg));
38     }
39
40
41     @Test
42     public void should_replace_dollar_sign_statement_with_map_value() {
43         String message = "${vnfid} some sample text ${pnfid} additional sample text";
44
45         Map<String, Object> respMsg = new HashMap<>();
46         respMsg.put("vnfid", "SYNC_NEW201");
47         respMsg.put("pnfid", "TEST-ID");
48
49         assertEquals("SYNC_NEW201 some sample text TEST-ID additional sample text",
50             MessageFormatter.format(message, respMsg));
51     }
52
53     @Test
54     public void getParamsNamesList_should_return_null_when_given_null_or_empty_message_template() {
55         assertEquals(null, MessageFormatter.getParamsNamesList(null));
56         assertEquals(null, MessageFormatter.getParamsNamesList(StringUtils.EMPTY));
57
58         assertEquals(null, MessageFormatter.getParamsNamesSet(null));
59         assertEquals(null, MessageFormatter.getParamsNamesSet(StringUtils.EMPTY));
60     }
61
62     @Test
63     public void should_recognize_params_inside_message_string() {
64         String message = "${vnfid} some sample text ${pnfid} additional sample text";
65
66         List<String> resultList = MessageFormatter.getParamsNamesList(message);
67
68         assertEquals(2, resultList.size());
69         assertTrue(resultList.contains("vnfid"));
70         assertTrue(resultList.contains("pnfid"));
71
72         Set<String> resultSet = MessageFormatter.getParamsNamesSet(message);
73
74         assertEquals(2, resultList.size());
75         assertTrue(resultSet.contains("vnfid"));
76         assertTrue(resultSet.contains("pnfid"));
77     }
78 }