Skip to content

Commit

Permalink
Refactor critical notifications handling. (#16592)
Browse files Browse the repository at this point in the history
Change-Id: I235804a80b1d70a564a54953b9255416a7386fe6
  • Loading branch information
Saulis committed Feb 17, 2015
1 parent c417195 commit a6d6295
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 34 deletions.
45 changes: 22 additions & 23 deletions server/src/com/vaadin/server/VaadinService.java
Expand Up @@ -1574,30 +1574,11 @@ public static String createCriticalNotificationJSON(String caption,
String message, String details, String url) { String message, String details, String url) {
String returnString = ""; String returnString = "";
try { try {
if (message == null) {
message = details;
} else if (details != null) {
message += "<br/><br/>" + details;
}

JsonObject appError = Json.createObject(); JsonObject appError = Json.createObject();
if (caption == null) { putValueOrJsonNull(appError, "caption", caption);
appError.put("caption", Json.createNull()); putValueOrJsonNull(appError, "url", url);
} else { putValueOrJsonNull(appError, "message",
appError.put("caption", caption); createCriticalNotificationMessage(message, details));
}

if (message == null) {
appError.put("message", Json.createNull());
} else {
appError.put("message", message);
}

if (url == null) {
appError.put("url", Json.createNull());
} else {
appError.put("url", url);
}


JsonObject meta = Json.createObject(); JsonObject meta = Json.createObject();
meta.put("appError", appError); meta.put("appError", appError);
Expand All @@ -1617,6 +1598,24 @@ public static String createCriticalNotificationJSON(String caption,
return "for(;;);[" + returnString + "]"; return "for(;;);[" + returnString + "]";
} }


private static String createCriticalNotificationMessage(String message, String details) {
if(message == null) {
return details;
} else if(details != null) {
return message + "<br/><br/>" + details;
}

return message;
}

private static void putValueOrJsonNull(JsonObject json, String key, String value) {
if(value == null) {
json.put(key, Json.createNull());
} else {
json.put(key, value);
}
}

/** /**
* @deprecated As of 7.0. Will likely change or be removed in a future * @deprecated As of 7.0. Will likely change or be removed in a future
* version * version
Expand Down
80 changes: 69 additions & 11 deletions server/tests/src/com/vaadin/server/VaadinServiceTest.java
Expand Up @@ -15,6 +15,9 @@
*/ */
package com.vaadin.server; package com.vaadin.server;


import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;

import javax.servlet.ServletConfig; import javax.servlet.ServletConfig;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingEvent;
Expand All @@ -39,6 +42,11 @@ public void sessionDestroy(SessionDestroyEvent event) {
} }
} }


private String createCriticalNotification(String caption, String message, String details, String url) {
return VaadinService
.createCriticalNotificationJSON(caption, message, details, url);
}

@Test @Test
public void testFireSessionDestroy() throws ServletException { public void testFireSessionDestroy() throws ServletException {
ServletConfig servletConfig = new MockServletConfig(); ServletConfig servletConfig = new MockServletConfig();
Expand Down Expand Up @@ -68,16 +76,66 @@ public void testFireSessionDestroy() throws ServletException {
} }


@Test @Test
public void testCriticalNotificationNullHandling() { public void captionIsSetToACriticalNotification() {
for (String caption : new String[] { "some caption", null }) { String notification =
for (String message : new String[] { "some message", null }) { createCriticalNotification("foobar", "message", "details", "url");
for (String details : new String[] { "some details", null }) {
for (String url : new String[] { "some url", null }) { assertThat(notification, containsString("\"caption\":\"foobar\""));
VaadinService.createCriticalNotificationJSON(caption, }
message, details, url);
} @Test
} public void nullCaptionIsSetToACriticalNotification() {
} String notification =
} createCriticalNotification(null, "message", "details", "url");

assertThat(notification, containsString("\"caption\":null"));
}

@Test
public void messageWithDetailsIsSetToACriticalNotification() {
String notification =
createCriticalNotification("caption", "foo", "bar", "url");

assertThat(notification, containsString("\"message\":\"foo<br/><br/>bar\""));
}

@Test
public void nullMessageIsReplacedByDetailsInACriticalNotification() {
String notification =
createCriticalNotification("caption", null, "foobar", "url");

assertThat(notification, containsString("\"message\":\"foobar\""));
}

@Test
public void nullMessageIsSetToACriticalNotification() {
String notification =
createCriticalNotification("caption", null, null, "url");

assertThat(notification, containsString("\"message\":null"));
}

@Test
public void messageSetToACriticalNotification() {
String notification =
createCriticalNotification("caption", "foobar", null, "url");

assertThat(notification, containsString("\"message\":\"foobar\""));
}

@Test
public void urlIsSetToACriticalNotification() {
String notification =
createCriticalNotification("caption", "message", "details", "foobar");

assertThat(notification, containsString("\"url\":\"foobar\""));
}

@Test
public void nullUrlIsSetToACriticalNotification() {
String notification =
createCriticalNotification("caption", "message", "details", null);

assertThat(notification, containsString("\"url\":null"));
} }
} }

0 comments on commit a6d6295

Please sign in to comment.