From f6f366b5532c51d9166ed68073f611097aec1db4 Mon Sep 17 00:00:00 2001 From: Jakub Dudycz Date: Mon, 12 Mar 2018 16:05:57 +0100 Subject: [PATCH] MessageFormatter unit tests Improved code coverage Change-Id: I8ef81c6c8efaa796d62d8913e7dfbf160bf3c02d Issue-ID: APPC-724 Signed-off-by: Jakub Dudycz --- .../java/org/onap/appc/util/MessageFormatter.java | 12 ++-- .../org/onap/appc/util/MessageFormatterTest.java | 72 ++++++++++++++++++++-- 2 files changed, 71 insertions(+), 13 deletions(-) diff --git a/appc-common/src/main/java/org/onap/appc/util/MessageFormatter.java b/appc-common/src/main/java/org/onap/appc/util/MessageFormatter.java index 71ef40641..ba56867a0 100644 --- a/appc-common/src/main/java/org/onap/appc/util/MessageFormatter.java +++ b/appc-common/src/main/java/org/onap/appc/util/MessageFormatter.java @@ -32,13 +32,11 @@ import java.util.regex.Pattern; public class MessageFormatter { private final static String paramNameRegexGroupName = "paramName"; - private final static String paramRegex = "\\$\\{(?[^}$]+)\\}"; // start with ${ and - // after there is one - // or more characters - // that are not $ and - // not } and ended - // with } + /** + * start with ${ and after there is one or more characters that are not $ and not } and ended with } + */ + private final static String paramRegex = "\\$\\{(?[^}$]+)\\}"; public static String format(String messageTemplate, Map params) { if (StringUtils.isEmpty(messageTemplate)) @@ -69,7 +67,7 @@ public class MessageFormatter { public static List getParamsNamesList(String messageTemplate) { List paramsNames = null; if (!StringUtils.isEmpty(messageTemplate)) { - paramsNames = new ArrayList(); + paramsNames = new ArrayList<>(); Matcher m = Pattern.compile(paramRegex).matcher(messageTemplate); while (m.find()) { String paramName = m.group(paramNameRegexGroupName); diff --git a/appc-common/src/test/java/org/onap/appc/util/MessageFormatterTest.java b/appc-common/src/test/java/org/onap/appc/util/MessageFormatterTest.java index 27f309fa9..fe7d1a532 100644 --- a/appc-common/src/test/java/org/onap/appc/util/MessageFormatterTest.java +++ b/appc-common/src/test/java/org/onap/appc/util/MessageFormatterTest.java @@ -1,18 +1,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 org.junit.Assert; +import java.util.Set; +import org.apache.commons.lang3.StringUtils; import org.junit.Test; public class MessageFormatterTest { @Test - public void testEscapeDollarInMessgeFormatter() { - String msg = "${SYNC_NEW201}"; + 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 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 respMsg = new HashMap<>(); - respMsg.put("vnfid", msg); - String formattedMsg = MessageFormatter.format(msg, respMsg); - Assert.assertEquals(msg, formattedMsg); + 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 resultList = MessageFormatter.getParamsNamesList(message); + + assertEquals(2, resultList.size()); + assertTrue(resultList.contains("vnfid")); + assertTrue(resultList.contains("pnfid")); + + Set resultSet = MessageFormatter.getParamsNamesSet(message); + + assertEquals(2, resultList.size()); + assertTrue(resultSet.contains("vnfid")); + assertTrue(resultSet.contains("pnfid")); } } -- 2.16.6