Search in sources :

Example 46 with JSONObject

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")));
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) JSONObject(org.apache.sling.commons.json.JSONObject) Form(com.adobe.acs.commons.forms.Form) Test(org.junit.Test)

Example 47 with JSONObject

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")));
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) MockSlingHttpServletResponse(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse) Cookie(javax.servlet.http.Cookie) JSONObject(org.apache.sling.commons.json.JSONObject) Form(com.adobe.acs.commons.forms.Form) Test(org.junit.Test)

Example 48 with JSONObject

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"));
}
Also used : Executor(org.apache.http.client.fluent.Executor) JSONObject(org.apache.sling.commons.json.JSONObject) HttpRequest(org.mockserver.model.HttpRequest) Request(org.apache.http.client.fluent.Request) JsonObjectResponseHandler(com.adobe.acs.commons.http.JsonObjectResponseHandler) Test(org.junit.Test)

Example 49 with JSONObject

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));
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) Event(org.osgi.service.event.Event) JSONObject(org.apache.sling.commons.json.JSONObject) Test(org.junit.Test)

Example 50 with JSONObject

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;
    }
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) JSONException(org.apache.sling.commons.json.JSONException)

Aggregations

JSONObject (org.apache.sling.commons.json.JSONObject)74 JSONArray (org.apache.sling.commons.json.JSONArray)22 Test (org.junit.Test)20 JSONException (org.apache.sling.commons.json.JSONException)16 HashMap (java.util.HashMap)15 ValueMap (org.apache.sling.api.resource.ValueMap)9 Map (java.util.Map)8 ArrayList (java.util.ArrayList)6 ServletException (javax.servlet.ServletException)6 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)6 Resource (org.apache.sling.api.resource.Resource)6 RepositoryException (javax.jcr.RepositoryException)5 Calendar (java.util.Calendar)4 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)4 Config (com.adobe.acs.commons.workflow.bulk.execution.model.Config)3 IOException (java.io.IOException)3 LinkedHashMap (java.util.LinkedHashMap)3 Session (javax.jcr.Session)3 ModifiableValueMapDecorator (org.apache.sling.api.wrappers.ModifiableValueMapDecorator)3 Form (com.adobe.acs.commons.forms.Form)2