use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class PostRedirectGetFormHelperImplTest method shouldSerialiseFormInRedirectUrlParameter.
@Test
public void shouldSerialiseFormInRedirectUrlParameter() throws Exception {
request.setParameterMap(ImmutableMap.<String, Object>of(":form", "x", "hello", "world"));
final SlingHttpServletResponse spiedResponse = spy(response);
final Form form = formHelper.getForm("x", request, response);
form.setError("hello", "Error1");
formHelper.renderForm(form, requestResource, request, spiedResponse);
verify(spiedResponse).sendRedirect(redirectCaptor.capture());
String redirectLocation = redirectCaptor.getValue();
final String formString = decode(getFormParameter(redirectLocation));
final JSONObject formObject = toJSONObject(formString);
// form name
assertThat(formObject.getString(PostRedirectGetFormHelperImpl.KEY_FORM_NAME), is(equalTo("x")));
// errors
final JSONObject formErrors = formObject.getJSONObject(PostRedirectGetFormHelperImpl.KEY_ERRORS);
assertThat(formErrors.getString("hello"), is(equalTo("Error1")));
// form data
final JSONObject formData = formObject.getJSONObject(PostRedirectGetFormHelperImpl.KEY_FORM);
assertThat(formData.getString("hello"), is(equalTo("world")));
}
use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class PostRedirectGetWithCookiesFormHelperImplTest method shouldSerialiseFormInRedirectUrlParameter.
@Test
public void shouldSerialiseFormInRedirectUrlParameter() throws Exception {
request.setMethod("POST");
request.setParameterMap(ImmutableMap.<String, Object>of(":form", "x", "hello", "world"));
final SlingHttpServletResponse spiedResponse = spy(response);
final Form form = formHelper.getForm("x", request, response);
form.setError("hello", "Error1");
formHelper.renderForm(form, requestResource, request, spiedResponse);
verify(spiedResponse).addCookie(cookieCaptor.capture());
final Cookie formCookie = cookieCaptor.getValue();
final String formString = decode(formCookie.getValue());
final JSONObject formObject = toJSONObject(formString);
// form name
assertThat(formObject.getString(PostRedirectGetFormHelperImpl.KEY_FORM_NAME), is(equalTo("x")));
// errors
final JSONObject formErrors = formObject.getJSONObject(PostRedirectGetFormHelperImpl.KEY_ERRORS);
assertThat(formErrors.getString("hello"), is(equalTo("Error1")));
// form data
final JSONObject formData = formObject.getJSONObject(PostRedirectGetFormHelperImpl.KEY_FORM);
assertThat(formData.getString("hello"), is(equalTo("world")));
}
use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class HttpClientFactoryImplTest method testJsonGet.
@Test
public void testJsonGet() throws Exception {
impl.activate(config);
Request get = impl.get("/anonJson");
Executor exec = impl.getExecutor();
JSONObject jsonObject = exec.execute(get).handleResponse(new JsonObjectResponseHandler());
assertThat(jsonObject.has("foo"), is(true));
assertThat(jsonObject.getString("foo"), is("bar"));
}
use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class JsonEventLoggerTest method testConstructMessage.
@Test
public void testConstructMessage() throws JSONException {
Map<String, Object> emptyProps = new LinkedHashMap<String, Object>();
Event empty = new Event("my/empty/topic", emptyProps);
JSONObject jEmptyProps = new JSONObject(JsonEventLogger.constructMessage(empty));
assertEquals("basic event, empty props", "my/empty/topic", jEmptyProps.getString("event.topics"));
Map<String, Object> stringProps = new LinkedHashMap<String, Object>();
stringProps.put("slingevent:application", "376e48ac-b010-4905-8a35-f5413cf6a930");
Event stringEvent = new Event("my/simple/topic", stringProps);
JSONObject jStringProps = new JSONObject(JsonEventLogger.constructMessage(stringEvent));
assertEquals("simple event, string props", "376e48ac-b010-4905-8a35-f5413cf6a930", jStringProps.getString("slingevent:application"));
Map<String, Object> intProps = new LinkedHashMap<String, Object>();
intProps.put("event.job.retries", -1);
Event intEvent = new Event("my/simple/topic", intProps);
JSONObject jIntProps = new JSONObject(JsonEventLogger.constructMessage(intEvent));
assertEquals("simple event, int props", -1, jIntProps.getInt("event.job.retries"));
Map<String, Object> boolProps = new LinkedHashMap<String, Object>();
boolProps.put("event.isSimple", true);
Event boolEvent = new Event("my/simple/topic", boolProps);
JSONObject jBoolProps = new JSONObject(JsonEventLogger.constructMessage(boolEvent));
assertTrue("simple event, bool props", jBoolProps.getBoolean("event.isSimple"));
Map<String, Object> stringArrayProps = new LinkedHashMap<String, Object>();
stringArrayProps.put("resourceChangedAttributes", new String[] { "first", "second" });
Event stringArrayEvent = new Event("my/simple/topic", stringArrayProps);
JSONObject jStringArray = new JSONObject(JsonEventLogger.constructMessage(stringArrayEvent));
assertNotNull("complex event, string array not null", jStringArray.optJSONArray("resourceChangedAttributes"));
assertEquals("complex event, string array props", "first", jStringArray.getJSONArray("resourceChangedAttributes").getString(0));
assertEquals("complex event, string array props", "second", jStringArray.getJSONArray("resourceChangedAttributes").getString(1));
Map<String, Object> intArrayProps = new LinkedHashMap<String, Object>();
intArrayProps.put("numbers", new Integer[] { 0, 1, 2 });
Event intArrayEvent = new Event("my/simple/topic", intArrayProps);
JSONObject jIntArray = new JSONObject(JsonEventLogger.constructMessage(intArrayEvent));
assertNotNull("complex event, int array not null", jIntArray.optJSONArray("numbers"));
assertEquals("complex event, int array props", 0, jIntArray.getJSONArray("numbers").getInt(0));
assertEquals("complex event, int array props", 1, jIntArray.getJSONArray("numbers").getInt(1));
assertEquals("complex event, int array props", 2, jIntArray.getJSONArray("numbers").getInt(2));
Map<String, Object> mapProps = new LinkedHashMap<String, Object>();
Map<String, Object> headers = new LinkedHashMap<String, Object>();
headers.put("user-agent", "curl/7.25.0");
mapProps.put("headers", headers);
Event mapEvent = new Event("my/simple/topic", mapProps);
JSONObject jMapProps = new JSONObject(JsonEventLogger.constructMessage(mapEvent));
assertNotNull("complex event, map not null", jMapProps.optJSONObject("headers"));
assertEquals("complex event, map value in props", "curl/7.25.0", jMapProps.getJSONObject("headers").getString("user-agent"));
Map<String, Object> stringSetProps = new LinkedHashMap<String, Object>();
stringSetProps.put("resourceChangedAttributes", new LinkedHashSet<String>(Arrays.asList("first", "second")));
Event stringSetEvent = new Event("my/simple/topic", stringSetProps);
JSONObject jStringSet = new JSONObject(JsonEventLogger.constructMessage(stringSetEvent));
assertNotNull("complex event, string set not null", jStringSet.optJSONArray("resourceChangedAttributes"));
assertEquals("complex event, string set props", "first", jStringSet.getJSONArray("resourceChangedAttributes").getString(0));
assertEquals("complex event, string set props", "second", jStringSet.getJSONArray("resourceChangedAttributes").getString(1));
}
use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.
the class PackageHelperImpl method getErrorJSON.
/**
* {@inheritDoc}
*/
public String getErrorJSON(final String msg) {
final JSONObject json = new JSONObject();
try {
json.put(KEY_STATUS, "error");
json.put(KEY_MSG, msg);
return json.toString();
} catch (JSONException e) {
log.error("Error creating JSON Error response message: {}", e);
return JSON_EXCEPTION_MSG;
}
}
Aggregations