Search in sources :

Example 6 with Variant

use of jakarta.ws.rs.core.Variant in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method mediaTypesTest1.

/*
   * @testName: mediaTypesTest1
   * 
   * @assertion_ids: JAXRS:SPEC:33.7; JAXRS:SPEC:33.8; JAXRS:JAVADOC:268;
   * JAXRS:JAVADOC:264; JAXRS:JAVADOC:267; JAXRS:JAVADOC:263; JAXRS:JAVADOC:255;
   * JAXRS:JAVADOC:256; JAXRS:JAVADOC:257;
   * 
   * @test_Strategy: Create a VariantListBuilder instance using
   * Variant.VariantListBuilder.newInstance(); Add two MediaType to it; Verify
   * that two Variant in the returned List
   */
@Test
public void mediaTypesTest1() throws Fault {
    boolean pass = true;
    StringBuffer sb = new StringBuffer();
    List<String> encoding = new ArrayList<String>();
    List<String> lang = new ArrayList<String>();
    MediaType mt1 = new MediaType();
    MediaType mt2 = new MediaType("application", "atom+xml");
    List<MediaType> types = Arrays.asList(mt1, mt2);
    List<Variant> vts = Variant.VariantListBuilder.newInstance().mediaTypes(mt1, mt2).add().build();
    String status = verifyVariants(vts, types, encoding, lang, 2);
    if (status.endsWith("false")) {
        pass = false;
    }
    sb.append(status);
    assertTrue(pass, "At least one assertion faled: " + sb.toString());
    TestUtil.logTrace(sb.toString());
}
Also used : Variant(jakarta.ws.rs.core.Variant) ArrayList(java.util.ArrayList) MediaType(jakarta.ws.rs.core.MediaType) Test(org.junit.jupiter.api.Test)

Example 7 with Variant

use of jakarta.ws.rs.core.Variant in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method verifyVariants.

private static String verifyVariants(List<Variant> vts, List<MediaType> types, List<String> encoding, List<String> lang, int size_expected) {
    StringBuffer sb = new StringBuffer();
    boolean pass = true;
    List<MediaType> actual_types = new ArrayList<MediaType>();
    sb.append(newline + "========== Verifying Variants" + newline);
    int size_vt = vts.size();
    if (size_vt != size_expected) {
        sb.append(indent + "Test Failed: List<Variant> returned is not right.  It includes " + size_vt + " Variant Objects. Expecting " + size_expected + newline);
        pass = false;
        for (int j = 0; j < vts.size(); j++) {
            Variant vt = vts.get(j);
            sb.append(indent + "Variant " + j + ": MediaType: " + vt.getMediaType().toString() + "; " + "Langauge: " + vt.getLanguage() + "; Encoding: " + vt.getEncoding() + newline);
        }
    } else {
        sb.append(indent + "Correct number of Variant returned: " + size_expected + newline);
    }
    for (Variant vt : vts) {
        sb.append(indent + "===== Verifying Variant" + newline);
        MediaType mt = vt.getMediaType();
        if (!types.contains(mt)) {
            pass = false;
            sb.append(indent + "Unexpected MediaType found in variant: type=" + mt.getType() + " subtype = " + mt.getSubtype() + newline);
            sb.append(indent + "Expecting the following MediaType:" + newline);
            for (MediaType tmp : types) {
                sb.append(indent + indent + tmp.getType() + "/" + tmp.getSubtype() + newline);
            }
        } else {
            sb.append(indent + "Expected MediaType found Variant: " + mt.getType() + " subtype = " + mt.getSubtype() + newline);
            actual_types.add(mt);
        }
        if (encoding == null || encoding.isEmpty()) {
            if (vt.getEncoding() != null) {
                pass = false;
                sb.append(indent + "Unexpected encoding found: " + vt.getEncoding().toString());
            }
        } else {
            if (vt.getEncoding() == null) {
                pass = false;
                sb.append(indent + "No encoding found: " + vt.getEncoding() + newline);
                sb.append(indent + "Expecting the following encodings: " + newline);
                for (String tmp : encoding) {
                    sb.append(indent + indent + tmp + newline);
                }
            } else if (!encoding.contains(vt.getEncoding().toString().toLowerCase())) {
                sb.append(indent + "Unexpected encoding found: " + vt.getEncoding() + newline);
                sb.append(indent + "Expecting the following encodings: " + newline);
                for (String tmp : lang) {
                    sb.append(indent + indent + tmp + newline);
                }
                pass = false;
            } else {
                sb.append(indent + "Found expected encoding " + vt.getEncoding() + newline);
            }
            if (lang == null || lang.isEmpty()) {
                if (vt.getLanguage() != null) {
                    pass = false;
                    sb.append(indent + "Unexpected language found: " + vt.getLanguage().toString());
                }
            } else {
                if (vt.getLanguage() == null) {
                    pass = false;
                    sb.append(indent + "No language found: " + vt.getLanguage() + newline);
                    sb.append(indent + "Expecting the following languages: " + newline);
                    for (String tmp : lang) {
                        sb.append(indent + indent + tmp + newline);
                    }
                } else if (!lang.contains(langToString(vt.getLanguage()))) {
                    sb.append(indent + "Unexpected language found: " + vt.getLanguage() + newline);
                    sb.append(indent + "Expecting the following languages: " + newline);
                    for (String tmp : lang) {
                        sb.append(indent + indent + tmp + newline);
                    }
                    pass = false;
                } else {
                    sb.append(indent + "Found expected language " + vt.getLanguage() + newline);
                }
            }
        }
    }
    Set<MediaType> set = new LinkedHashSet<MediaType>();
    set.addAll(actual_types);
    List<MediaType> final_types = new ArrayList<MediaType>(set);
    if (types.size() != final_types.size()) {
        pass = false;
        sb.append("Some missing MediaType: expecting " + types.size() + ", only got " + final_types.size() + newline);
        for (int i = 0; i < final_types.size(); i++) {
            sb.append(final_types.get(i).toString() + newline);
        }
    }
    sb.append(pass);
    return sb.toString();
}
Also used : Variant(jakarta.ws.rs.core.Variant) LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) MediaType(jakarta.ws.rs.core.MediaType)

Example 8 with Variant

use of jakarta.ws.rs.core.Variant in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method mediaTypesTest2.

/*
   * @testName: mediaTypesTest2
   * 
   * @assertion_ids: JAXRS:SPEC:33.7; JAXRS:SPEC:33.8; JAXRS:SPEC:33.9;
   * JAXRS:JAVADOC:268; JAXRS:JAVADOC:264; JAXRS:JAVADOC:267; JAXRS:JAVADOC:263;
   * JAXRS:JAVADOC:255; JAXRS:JAVADOC:256; JAXRS:JAVADOC:257;
   * 
   * @test_Strategy: Create a VariantListBuilder instance using
   * Variant.VariantListBuilder.newInstance(); Add three MediaType to it; Verify
   * that two Variant in the returned List
   */
@Test
public void mediaTypesTest2() throws Fault {
    boolean pass = true;
    StringBuffer sb = new StringBuffer();
    List<String> encoding = new ArrayList<String>();
    List<String> lang = new ArrayList<String>();
    Map<String, String> params = new HashMap<String, String>();
    MediaType mt1 = new MediaType();
    MediaType mt2 = new MediaType("application", "atom+xml");
    MediaType mt3 = new MediaType("application", "x-www-form-urlencoded", params);
    List<MediaType> types = Arrays.asList(mt1, mt2, mt3);
    List<Variant> vts = Variant.VariantListBuilder.newInstance().mediaTypes(mt1, mt2, mt3).add().build();
    String status = verifyVariants(vts, types, encoding, lang, 3);
    if (status.endsWith("false")) {
        pass = false;
    }
    sb.append(status);
    assertTrue(pass, "At least one assertion faled: " + sb.toString());
    TestUtil.logTrace(sb.toString());
}
Also used : Variant(jakarta.ws.rs.core.Variant) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MediaType(jakarta.ws.rs.core.MediaType) Test(org.junit.jupiter.api.Test)

Example 9 with Variant

use of jakarta.ws.rs.core.Variant in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method constructorTest3.

/*
     * @testName: constructorTest3
     * 
     * @assertion_ids: JAXRS:JAVADOC:255; JAXRS:JAVADOC:256; JAXRS:JAVADOC:257;
     * JAXRS:JAVADOC:261; JAXRS:JAVADOC:262;
     * 
     * @test_Strategy: Create an MediaType instance using MediaType(String,
     * String)
     */
@Test
public void constructorTest3() throws Fault {
    String type = "application";
    String subtype = "x-www-form-urlencoded";
    Map<String, String> params = new HashMap<String, String>();
    String encoding = null;
    Locale lang = null;
    MediaType mt3 = new MediaType(type, subtype, params);
    Variant vt = new Variant(mt3, lang, encoding);
    verifyVariant(vt, type, subtype, params, new String[0], lang, encoding);
}
Also used : Locale(java.util.Locale) Variant(jakarta.ws.rs.core.Variant) HashMap(java.util.HashMap) MediaType(jakarta.ws.rs.core.MediaType) Test(org.junit.jupiter.api.Test)

Example 10 with Variant

use of jakarta.ws.rs.core.Variant in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method mediaTypesTest.

/*
     * @testName: mediaTypesTest
     * 
     * @assertion_ids: JAXRS:JAVADOC:260;
     * 
     * @test_Strategy: Call Variant.mediaTypes(MediaType ...) with three MediaType
     * objects, Verify that three Variant instances created properly
     */
@Test
public void mediaTypesTest() throws Fault {
    String type = "application";
    String subtype = "x-www-form-urlencoded";
    MediaType mt = new MediaType(type, subtype);
    type = "application";
    subtype = "atom+xml";
    MediaType mt1 = new MediaType(type, subtype);
    type = jakarta.ws.rs.core.MediaType.MEDIA_TYPE_WILDCARD;
    subtype = jakarta.ws.rs.core.MediaType.MEDIA_TYPE_WILDCARD;
    MediaType mt2 = new MediaType();
    List<String> langS = null;
    List<String> encodingS = null;
    List<MediaType> mts = Arrays.asList(mt, mt1, mt2);
    List<Variant> vts = Variant.mediaTypes(mt, mt1, mt2).add().build();
    verifyVariants(vts, mts, langS, encodingS);
}
Also used : Variant(jakarta.ws.rs.core.Variant) MediaType(jakarta.ws.rs.core.MediaType) Test(org.junit.jupiter.api.Test)

Aggregations

Variant (jakarta.ws.rs.core.Variant)29 Test (org.junit.jupiter.api.Test)25 MediaType (jakarta.ws.rs.core.MediaType)21 Locale (java.util.Locale)13 Response (jakarta.ws.rs.core.Response)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)3 GET (jakarta.ws.rs.GET)1 Path (jakarta.ws.rs.Path)1 ClientRequestContext (jakarta.ws.rs.client.ClientRequestContext)1 Invocation (jakarta.ws.rs.client.Invocation)1 Annotation (java.lang.annotation.Annotation)1 LinkedHashSet (java.util.LinkedHashSet)1