Search in sources :

Example 31 with JSONStringer

use of org.json.JSONStringer in project mobile-center-sdk-android by Microsoft.

the class AbstractLogTest method writeNullDeviceTest.

@Test
public void writeNullDeviceTest() throws JSONException {
    JSONStringer mockJsonStringer = mock(JSONStringer.class);
    when(mockJsonStringer.key(anyString())).thenReturn(mockJsonStringer);
    when(mockJsonStringer.value(anyString())).thenReturn(mockJsonStringer);
    AbstractLog mockLog = new MockLog();
    mockLog.setTimestamp(new Date());
    mockLog.write(mockJsonStringer);
    verify(mockJsonStringer, never()).key(AbstractLog.DEVICE);
}
Also used : JSONStringer(org.json.JSONStringer) Date(java.util.Date) Test(org.junit.Test)

Example 32 with JSONStringer

use of org.json.JSONStringer in project mobile-center-sdk-android by Microsoft.

the class DefaultLogSerializerTest method failToUsePrettyJson.

@Test
public void failToUsePrettyJson() throws Exception {
    /* Mock logs to verify interactions. */
    mockStatic(AppCenterLog.class);
    when(AppCenterLog.getLogLevel()).thenReturn(Log.VERBOSE);
    /* Mock stub JSONStringer. */
    JSONStringer stringer = mock(JSONStringer.class);
    whenNew(JSONStringer.class).withAnyArguments().thenReturn(stringer);
    when(stringer.key(anyString())).thenReturn(stringer);
    when(stringer.toString()).thenReturn("{}");
    /*
         * We are in test folder so the stub does not contain the private methods,
         * this test thus simulates what happens if we can't access the private methods
         * via reflection to use pretty json. We check it serializes gracefully.
         */
    assertEquals("{}", new DefaultLogSerializer().serializeContainer(mock(LogContainer.class)));
    /* And that it logs why the pretty json could not be used. */
    verifyStatic();
    AppCenterLog.error(eq(AppCenter.LOG_TAG), anyString(), any(NoSuchMethodError.class));
}
Also used : JSONStringer(org.json.JSONStringer) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 33 with JSONStringer

use of org.json.JSONStringer in project quickstart by wildfly.

the class RESTTest method testAddContact.

@Test
@InSequence(2)
public void testAddContact() throws Exception {
    HttpPost post = new HttpPost(contextPath.toString() + API_PATH);
    post.setHeader("Content-Type", "application/json");
    String newContactJSON = new JSONStringer().object().key("firstName").value(NEW_CONTACT_FIRSTNAME).key("lastName").value(NEW_CONTACT_LASTNAME).key("email").value(NEW_CONTACT_EMAIL).key("phoneNumber").value(NEW_CONTACT_PHONE).key("birthDate").value(NEW_CONTACT_BIRTHDATE).endObject().toString();
    post.setEntity(new StringEntity(newContactJSON));
    HttpResponse response = httpClient.execute(post);
    assertEquals(201, response.getStatusLine().getStatusCode());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) HttpResponse(org.apache.http.HttpResponse) JSONStringer(org.json.JSONStringer) Test(org.junit.Test) InSequence(org.jboss.arquillian.junit.InSequence)

Example 34 with JSONStringer

use of org.json.JSONStringer in project quickstart by wildfly.

the class RESTTest method testAddMember.

@Test
@InSequence(2)
public void testAddMember() throws Exception {
    HttpPost post = new HttpPost(contextPath.toString() + API_PATH);
    post.setHeader("Content-Type", "application/json");
    String newMemberJSON = new JSONStringer().object().key("name").value(NEW_MEMBER_NAME).key("email").value(NEW_MEMBER_EMAIL).key("phoneNumber").value(NEW_MEMBER_PHONE).endObject().toString();
    post.setEntity(new StringEntity(newMemberJSON));
    HttpResponse response = httpClient.execute(post);
    assertEquals(200, response.getStatusLine().getStatusCode());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) HttpResponse(org.apache.http.HttpResponse) JSONStringer(org.json.JSONStringer) Test(org.junit.Test) InSequence(org.jboss.arquillian.junit.InSequence)

Aggregations

JSONStringer (org.json.JSONStringer)34 Test (org.junit.Test)16 JSONObject (org.json.JSONObject)12 JSONException (org.json.JSONException)7 ArrayList (java.util.ArrayList)4 NonNull (android.support.annotation.NonNull)2 IOException (java.io.IOException)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 HttpResponse (org.apache.http.HttpResponse)2 HttpPost (org.apache.http.client.methods.HttpPost)2 StringEntity (org.apache.http.entity.StringEntity)2 InSequence (org.jboss.arquillian.junit.InSequence)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Log (com.microsoft.appcenter.ingestion.models.Log)1 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)1 Log (com.microsoft.azure.mobile.ingestion.models.Log)1 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedReader (java.io.BufferedReader)1