blob: 2dffdb7fde1c31f6dc0143ab5e9740d61392a06e [file] [log] [blame]
/*
* Copyright (c) 2013, 2018 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.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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.GenericEntity;
import javax.ws.rs.core.GenericType;
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.Test;
import static org.junit.Assert.assertEquals;
import net.java.html.json.Model;
/**
* Reading and writing class generated by {@link Model} as
* {@link List}.
*
* @author Jaroslav Tulach
*/
public class ModelEntityOnListTest extends AbstractTypeTester {
@Path("empty")
public static class TestResource {
@POST
@Path("mybean")
public String myBean(List<MyBean> bean) {
if (bean.size() != 2) {
return "ERROR, length: " + bean.size();
}
if (!bean.get(0).getValue().equals("Hello")) {
return "ERROR, [0].value = " + bean.get(0).getValue();
}
if (!bean.get(1).getValue().equals("Ahoy")) {
return "ERROR, [1].value = " + bean.get(1).getValue();
}
return "PASSED";
}
@GET
@Path("getbean")
public Response getBean(@Context HttpHeaders headers) {
MyBean teb = new MyBean();
teb.setValue("hello");
List<MyBean> l = new ArrayList<MyBean>();
l.add(teb);
GenericEntity<List<MyBean>> ge = new GenericEntity<List<MyBean>>(l) {};
return Response.ok().type(MediaType.APPLICATION_JSON_TYPE).entity(ge).build();
}
}
public ModelEntityOnListTest() {
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");
GenericEntity<List<MyBean>> ge = new GenericEntity<List<MyBean>>(Arrays.asList(mb, ah)) {};
final Response response = target.request().post(Entity.entity(ge, MediaType.APPLICATION_JSON_TYPE));
assertEquals(200, response.getStatus());
assertEquals("PASSED", response.readEntity(String.class));
}
@Test
public void myBeanAndGet() {
WebTarget target = target("empty/getbean");
final Response response = target.request(MediaType.APPLICATION_JSON).get();
assertEquals(200, response.getStatus());
GenericType<List<MyBean>> ge = new GenericType<List<MyBean>>() {};
List<MyBean> teb = response.readEntity(ge);
assertEquals("one element in list: " + teb, 1, teb.size());
assertEquals("value", "hello", teb.get(0).getValue());
}
}