aboutsummaryrefslogtreecommitdiffstats
path: root/appc-common/src/test/java/org/onap/appc/util/MessageFormatterTest.java
blob: fe7d1a532ad8b13253d83ccd9d1944426fd704bc (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
package org.onap.appc.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.google.common.collect.Maps;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

public class MessageFormatterTest {

    @Test
    public void format_should_return_empty_string_when_given_null_or_empty_message_template() {
        assertEquals(StringUtils.EMPTY, MessageFormatter.format(null, Maps.newHashMap()));
        assertEquals(StringUtils.EMPTY, MessageFormatter.format(StringUtils.EMPTY, Maps.newHashMap()));
    }

    @Test
    public void should_return_same_string_when_given_null_or_empty_params() {
        String message = "message";

        assertEquals(message, MessageFormatter.format(message, null));
        assertEquals(message, MessageFormatter.format(message, Maps.newHashMap()));
    }

    @Test
    public void should_return_same_string_when_given_non_dollar_string() {
        String msg = "vnfid";

        Map<String, Object> respMsg = new HashMap<>();
        respMsg.put("vnfid", "SYNC_NEW201");

        assertEquals(msg, MessageFormatter.format(msg, respMsg));
    }


    @Test
    public void should_replace_dollar_sign_statement_with_map_value() {
        String message = "${vnfid} some sample text ${pnfid} additional sample text";

        Map<String, Object> respMsg = new HashMap<>();
        respMsg.put("vnfid", "SYNC_NEW201");
        respMsg.put("pnfid", "TEST-ID");

        assertEquals("SYNC_NEW201 some sample text TEST-ID additional sample text",
            MessageFormatter.format(message, respMsg));
    }

    @Test
    public void getParamsNamesList_should_return_null_when_given_null_or_empty_message_template() {
        assertEquals(null, MessageFormatter.getParamsNamesList(null));
        assertEquals(null, MessageFormatter.getParamsNamesList(StringUtils.EMPTY));

        assertEquals(null, MessageFormatter.getParamsNamesSet(null));
        assertEquals(null, MessageFormatter.getParamsNamesSet(StringUtils.EMPTY));
    }

    @Test
    public void should_recognize_params_inside_message_string() {
        String message = "${vnfid} some sample text ${pnfid} additional sample text";

        List<String> resultList = MessageFormatter.getParamsNamesList(message);

        assertEquals(2, resultList.size());
        assertTrue(resultList.contains("vnfid"));
        assertTrue(resultList.contains("pnfid"));

        Set<String> resultSet = MessageFormatter.getParamsNamesSet(message);

        assertEquals(2, resultList.size());
        assertTrue(resultSet.contains("vnfid"));
        assertTrue(resultSet.contains("pnfid"));
    }
}