ModelEntityOnArrayTest.java

/*
 * Copyright (c) 2013, 2022 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

package org.glassfish.jersey.media.htmljson;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.test.TestProperties;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import net.java.html.json.Model;

/**
 * Reading and writing class generated by {@link Model} as
 * arrays.
 *
 * @author Jaroslav Tulach
 */
public class ModelEntityOnArrayTest extends AbstractTypeTester {

    @Path("empty")
    public static class TestResource {

        @POST
        @Path("mybean")
        public String myBean(MyBean[] bean) {
            if (bean.length != 2) {
                return "ERROR, length: " + bean.length;
            }
            if (!bean[0].getValue().equals("Hello")) {
                return "ERROR, [0].value = " + bean[0].getValue();
            }
            if (!bean[1].getValue().equals("Ahoy")) {
                return "ERROR, [1].value = " + bean[1].getValue();
            }
            return "PASSED";
        }

        @GET
        @Path("getbean")
        public Response getBean(@Context HttpHeaders headers) {
            MyBean teb = new MyBean();
            teb.setValue("hello");
            return Response.ok().type(MediaType.APPLICATION_JSON_TYPE).entity(new MyBean[] {teb}).build();
        }
    }

    public ModelEntityOnArrayTest() {
        enable(TestProperties.LOG_TRAFFIC);
    }

    @Test
    public void myBeanAndPut() {
        WebTarget target = target("empty/mybean");

        MyBean mb = new MyBean();
        mb.setValue("Hello");
        MyBean ah = new MyBean();
        ah.setValue("Ahoy");
        MyBean[] arr = new MyBean[] {mb, ah};

        final Response response = target.request().post(Entity.entity(arr, MediaType.APPLICATION_JSON_TYPE));

        assertEquals(200, response.getStatus());
        assertEquals("PASSED", response.readEntity(String.class));
    }

    @Test
    public void doReadWrite() throws Exception {
        MyBean mb = new MyBean();
        mb.setValue("Hello");
        MyBean ah = new MyBean();
        ah.setValue("Ahoy");
        MyBean[] arr = new MyBean[] {mb, ah};

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        os.write('[');
        os.write(arr[0].toString().getBytes("UTF-8"));
        os.write(',');
        os.write(arr[1].toString().getBytes("UTF-8"));
        os.write(']');
        os.close();

        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());

        final Class c = arr.getClass();
        Object ret = new HtmlJsonProvider().readFrom(c, null, null, MediaType.APPLICATION_JSON_TYPE, null, is);

        assertTrue(ret instanceof MyBean[], "It is array: " + ret);
        MyBean[] res = (MyBean[]) ret;
        assertEquals(2, res.length, "Two items: ");
        assertEquals(arr[0], res[0]);
        assertEquals(arr[1], res[1]);
    }

    @Test
    public void myBeanAndGet() {
        WebTarget target = target("empty/getbean");
        final Response response = target.request(MediaType.APPLICATION_JSON).get();
        assertEquals(200, response.getStatus());
        final MyBean[] teb = response.readEntity(MyBean[].class);

        assertEquals("hello", teb[0].getValue(), "value");
    }
}