Search in sources :

Example 1 with Conditions

use of com.liferay.apio.architect.test.util.json.Conditions in project com-liferay-apio-architect by liferay.

the class HALTestUtil method isAJsonObjectWithTheLinks.

/**
 * Returns a {@code Matcher} that checks if the field is a JSON object
 * containing the links of a {@code RootElement} that matches the provided
 * ID.
 *
 * @param  id the ID of the {@code RootElement}
 * @return the matcher
 */
public static Matcher<JsonElement> isAJsonObjectWithTheLinks(String id) {
    Conditions.Builder builder = new Conditions.Builder();
    Conditions linkConditions = builder.where("binary1", isALinkTo("localhost/b/model/" + id + "/binary1")).where("binary2", isALinkTo("localhost/b/model/" + id + "/binary2")).where("embedded2", isALinkTo("localhost/p/first-inner-model/second")).where("link1", isALinkTo("www.liferay.com")).where("link2", isALinkTo("community.liferay.com")).where("linked1", isALinkTo("localhost/p/first-inner-model/third")).where("linked2", isALinkTo("localhost/p/first-inner-model/fourth")).where("relatedCollection1", isALinkTo("localhost/p/model/" + id + "/models")).where("relatedCollection2", isALinkTo("localhost/p/model/" + id + "/models")).where("self", isALinkTo("localhost/p/model/" + id)).build();
    return is(aJsonObjectWith(linkConditions));
}
Also used : Conditions(com.liferay.apio.architect.test.util.json.Conditions)

Example 2 with Conditions

use of com.liferay.apio.architect.test.util.json.Conditions in project com-liferay-apio-architect by liferay.

the class HALTestUtil method isAJsonObjectWithTheFirstEmbedded.

/**
 * Returns a {@code Matcher} that checks if the field is a JSON object of
 * the first embedded model.
 *
 * @param  id
 * @return the matcher
 */
public static Matcher<JsonElement> isAJsonObjectWithTheFirstEmbedded(String id) {
    Conditions.Builder builder = new Conditions.Builder();
    Conditions firstEmbeddedLinkConditions = builder.where("binary", isALinkTo("localhost/b/first-inner-model/first/binary")).where("link", isALinkTo("www.liferay.com")).where("linked", isALinkTo("localhost/p/second-inner-model/second")).where("relatedCollection", isALinkTo("localhost/p/first-inner-model/first/models")).where("self", isALinkTo("localhost/p/first-inner-model/first")).build();
    Matcher<JsonElement> isAJsonObjectWithTheSecondEmbedded = is(aJsonObjectWhere("embedded", isAJsonObjectWithTheSecondEmbedded()));
    Conditions firstEmbeddedConditions = builder.where("_embedded", isAJsonObjectWithTheSecondEmbedded).where("_links", is(aJsonObjectWith(firstEmbeddedLinkConditions))).where("boolean", is(aJsonBoolean(true))).where("booleanList", isAJsonArrayContaining(true, false)).where("localizedString", is(aJsonString(equalTo("Translated")))).where("number", is(aJsonInt(equalTo(42)))).where("numberList", isAJsonArrayContaining(1, 2)).where("string", is(aJsonString(equalTo("A string")))).where("stringList", isAJsonArrayContaining("a", "b")).build();
    Conditions conditions = builder.where("embedded1", is(aJsonObjectWith(firstEmbeddedConditions))).where("nested1", isAJsonObjectWithTheFirstNested()).where("nested2", isAJsonObjectWithTheSecondNested(id)).build();
    return is(aJsonObjectWith(conditions));
}
Also used : JsonElement(com.google.gson.JsonElement) Conditions(com.liferay.apio.architect.test.util.json.Conditions)

Example 3 with Conditions

use of com.liferay.apio.architect.test.util.json.Conditions in project com-liferay-apio-architect by liferay.

the class HALTestUtil method isAJsonObjectWithTheSecondEmbedded.

/**
 * Returns a {@code Matcher} that checks if the field is a JSON object of
 * the second embedded model.
 *
 * @return the matcher
 */
public static Matcher<JsonElement> isAJsonObjectWithTheSecondEmbedded() {
    Conditions.Builder builder = new Conditions.Builder();
    Conditions secondEmbeddedLinkConditions = builder.where("binary", isALinkTo("localhost/b/second-inner-model/first/binary")).where("embedded", isALinkTo("localhost/p/third-inner-model/first")).where("link", isALinkTo("community.liferay.com")).where("linked", isALinkTo("localhost/p/third-inner-model/second")).where("relatedCollection", isALinkTo("localhost/p/second-inner-model/first/models")).where("self", isALinkTo("localhost/p/second-inner-model/first")).build();
    Conditions secondEmbeddedConditions = builder.where("_links", is(aJsonObjectWith(secondEmbeddedLinkConditions))).where("boolean", is(aJsonBoolean(false))).where("booleanList", isAJsonArrayContaining(true)).where("number", is(aJsonInt(equalTo(2017)))).where("numberList", isAJsonArrayContaining(1)).where("string", is(aJsonString(equalTo("A string")))).where("stringList", isAJsonArrayContaining("a")).build();
    return is(aJsonObjectWith(secondEmbeddedConditions));
}
Also used : Conditions(com.liferay.apio.architect.test.util.json.Conditions)

Example 4 with Conditions

use of com.liferay.apio.architect.test.util.json.Conditions in project com-liferay-apio-architect by liferay.

the class JSONLDErrorMessageMapperTest method testJSONLDErrorMessageMapper.

@Test
public void testJSONLDErrorMessageMapper() {
    APIError apiError = new APIError(new IllegalArgumentException(), "A title", "A description", "A type", 404);
    HttpHeaders httpHeaders = Mockito.mock(HttpHeaders.class);
    String error = ErrorWriter.writeError(_errorMessageMapper, apiError, httpHeaders);
    Conditions.Builder builder = new Conditions.Builder();
    Conditions conditions = builder.where("@type", is(aJsonString(equalTo("A type")))).where("description", is(aJsonString(equalTo("A description")))).where("statusCode", is(aJsonInt(equalTo(404)))).where("title", is(aJsonString(equalTo("A title")))).build();
    assertThat(error, is(aJsonObjectStringWith(conditions)));
}
Also used : HttpHeaders(javax.ws.rs.core.HttpHeaders) JsonMatchers.aJsonString(com.liferay.apio.architect.test.util.json.JsonMatchers.aJsonString) APIError(com.liferay.apio.architect.error.APIError) Conditions(com.liferay.apio.architect.test.util.json.Conditions) Test(org.junit.Test)

Example 5 with Conditions

use of com.liferay.apio.architect.test.util.json.Conditions in project com-liferay-apio-architect by liferay.

the class JSONLDTestUtil method isAJsonObjectWithTheFirstEmbedded.

/**
 * Returns a {@code Matcher} that checks if the field is a JSON object of
 * the first embedded model.
 *
 * @param  member whether the {@code FirstEmbeddedModel} is added as a
 *         collection member
 * @return the matcher
 */
public static Matcher<JsonElement> isAJsonObjectWithTheFirstEmbedded(boolean member) {
    Builder builder = new Builder();
    List<Matcher<? super JsonElement>> theContext = Arrays.asList(aJsonObjectWhere("linked", IS_A_TYPE_ID_JSON_OBJECT), aJsonObjectWhere("relatedCollection", IS_A_TYPE_ID_JSON_OBJECT));
    Builder firstEmbeddedBuilder = builder.where("@context", is(aJsonArrayThat(contains(theContext)))).where("@id", isALinkTo("localhost/p/first-inner-model/first")).where("@type", containsTheTypes("Type")).where("binary", isALinkTo("localhost/b/first-inner-model/first/binary")).where("boolean", is(aJsonBoolean(true))).where("booleanList", isAJsonArrayContaining(true, false)).where("embedded", isAJsonObjectWithTheSecondEmbedded()).where("link", isALinkTo("www.liferay.com")).where("linked", isALinkTo("localhost/p/second-inner-model/second")).where("localizedString", is(aJsonString(equalTo("Translated")))).where("number", is(aJsonInt(equalTo(42)))).where("numberList", isAJsonArrayContaining(1, 2)).where("relatedCollection", isALinkTo("localhost/p/first-inner-model/first/models")).where("string", is(aJsonString(equalTo("A string")))).where("stringList", isAJsonArrayContaining("a", "b"));
    if (member) {
        return aJsonObjectWith(firstEmbeddedBuilder.build());
    }
    Matcher<? super JsonElement> anOperation = builder.where("@id", is(aJsonString(equalTo("_:delete-operation")))).where("@type", is(aJsonString(equalTo("Operation")))).where("method", is(aJsonString(equalTo("DELETE")))).build();
    Conditions conditions = firstEmbeddedBuilder.where("operation", is(aJsonArrayThat(contains(anOperation)))).build();
    return aJsonObjectWith(conditions);
}
Also used : Matcher(org.hamcrest.Matcher) JsonElement(com.google.gson.JsonElement) Builder(com.liferay.apio.architect.test.util.json.Conditions.Builder) Conditions(com.liferay.apio.architect.test.util.json.Conditions)

Aggregations

Conditions (com.liferay.apio.architect.test.util.json.Conditions)30 Builder (com.liferay.apio.architect.test.util.json.Conditions.Builder)13 Test (org.junit.Test)13 JsonMatchers.aJsonString (com.liferay.apio.architect.test.util.json.JsonMatchers.aJsonString)8 HttpHeaders (javax.ws.rs.core.HttpHeaders)8 JsonElement (com.google.gson.JsonElement)7 JsonObject (com.google.gson.JsonObject)5 Matcher (org.hamcrest.Matcher)5 APIError (com.liferay.apio.architect.error.APIError)3 Description (org.hamcrest.Description)3 StringDescription (org.hamcrest.StringDescription)3 ErrorMessageMapper (com.liferay.apio.architect.message.json.ErrorMessageMapper)1 JSONObjectBuilder (com.liferay.apio.architect.message.json.JSONObjectBuilder)1 ArrayList (java.util.ArrayList)1