BaseNotificationMessageRenderer implements NotificationMessageRenderer<T> { public NotificationMessage render( T entity, NotificationTemplate template ) { final String collatedTemplate = template.getSubjectTemplate() + " " + template.getMessageTemplate(); Map<String, String> expressionToValueMap = extractExpressionsByType( collatedTemplate ).entrySet().stream() .map( entry -> resolveValuesFromExpressions( entry.getValue(), entry.getKey(), entity ) ) .collect( HashMap::new, Map::putAll, Map::putAll ); return createNotificationMessage( template, expressionToValueMap ); }  NotificationMessage render( T entity, NotificationTemplate template );  }
@Test public void testRenderVariablesRendersVariables() { Entity e = entity(); String templateString = "V{a} V{b}"; NotificationTemplate template = template( templateString ); NotificationMessage message = renderer.render( e, template ); String expected = format( "%s %s", e.propertyA, e.propertyB ); assertNotNull( message ); assertEquals( expected, message.getMessage() ); }
@Test public void testRenderAttributesRendersAttributes() { Entity e = entity(); String templateString = format( "A{%s} A{%s}", ATTR_A.getKey(), ATTR_B.getKey() ); NotificationTemplate template = template( templateString ); NotificationMessage message = renderer.render( e, template ); String expected = format( "%s %s", ATTR_A.getValue(), ATTR_B.getValue() ); assertNotNull( message ); assertEquals( expected, message.getMessage() ); }
@Test public void testRenderMixedContentRendersBoth() { Entity e = entity(); String templateMessage = format( "Boom V{a} shake A{%s} the room", ATTR_A.getKey() ); NotificationTemplate template = template( templateMessage, templateMessage ); NotificationMessage message = renderer.render( e, template ); String expected = format( "Boom %s shake %s the room", e.propertyA, ATTR_A.getValue() ); assertNotNull( message ); assertEquals( expected, message.getMessage() ); }
@Test public void testRenderNullValuesAreReplacedWithEmptyStrings() { Entity e = entity( "Halvdan was here", null ); String templateString = format( "V{a} V{b} A{%s}", ATTR_NULL.getKey() ); NotificationTemplate template = template( templateString ); NotificationMessage message = renderer.render( e, template ); String expected = format( "%s ", e.propertyA ); assertNotNull( message ); assertEquals( expected, message.getMessage() ); }
@Test public void testNonSupportedExpressionsAreIgnored() { Entity e = entity(); String templateString = "X{abc} V{a} Y{}"; NotificationTemplate template = template( templateString ); NotificationMessage message = renderer.render( e, template ); String expected = format( "X{abc} %s Y{}", e.propertyA ); assertNotNull( message ); assertEquals( expected, message.getMessage() ); }
@Test public void testNonExistingVariablesAreReplacedWithEmptyStrings() { Entity e = entity(); String templateString = "V{b} V{does_not_exist} V{a}"; NotificationTemplate template = template( templateString ); NotificationMessage message = renderer.render( e, template ); String expected = format( "%s %s", e.propertyB, e.propertyA ); assertNotNull( message ); assertEquals( expected, message.getMessage() ); }
@Test public void testSubjectLengthIsLimited() { Entity e = entity(); String templateString = CodeGenerator.generateCode( 101 ); NotificationTemplate template = template( templateString ); NotificationMessage message = renderer.render( e, template ); assertNotNull( message ); assertEquals( BaseNotificationMessageRenderer.SUBJECT_CHAR_LIMIT, message.getSubject().length() ); }
@Test public void testMessageLengthIsLimitedWhenHasSmsRecipient() { Entity e = entity(); int tooLong = RandomUtils.nextInt( BaseNotificationMessageRenderer.SMS_CHAR_LIMIT + 1, BaseNotificationMessageRenderer.SMS_CHAR_LIMIT + 100 ); String templateString = CodeGenerator.generateCode( tooLong ); NotificationTemplate template = template( templateString ); NotificationMessage message = renderer.render( e, template ); assertNotNull( message ); assertEquals( BaseNotificationMessageRenderer.SMS_CHAR_LIMIT, message.getMessage().length() ); }
@Test public void testMessageLengthIsLimitedForEmailRecipients() { Entity e = entity(); int tooLong = RandomUtils.nextInt( BaseNotificationMessageRenderer.EMAIL_CHAR_LIMIT + 1, BaseNotificationMessageRenderer.EMAIL_CHAR_LIMIT + 100 ); String templateString = CodeGenerator.generateCode( tooLong ); NotificationTemplate template = Mockito.spy( template( templateString ) ); Mockito.when( template.getDeliveryChannels() ).thenReturn( Sets.newHashSet( DeliveryChannel.EMAIL ) ); NotificationMessage message = renderer.render( e, template ); assertNotNull( message ); assertEquals( BaseNotificationMessageRenderer.EMAIL_CHAR_LIMIT, message.getMessage().length() ); }