package com.nhl.bootique.linkrest;

import static org.junit.Assert.assertEquals;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;

import org.junit.Test;

import com.google.inject.Module;
import com.nhl.bootique.jersey.JerseyModule;
import com.nhl.bootique.linkrest.unit.BQLinkRestTest;
import com.nhl.link.rest.DataResponse;
import com.nhl.link.rest.LinkRest;
import com.nhl.link.rest.annotation.LrAttribute;
import com.nhl.link.rest.annotation.LrId;
import com.nhl.link.rest.annotation.listener.SelectServerParamsApplied;
import com.nhl.link.rest.meta.LrEntityBuilder;
import com.nhl.link.rest.processor.ProcessingStage;
import com.nhl.link.rest.runtime.processor.select.SelectContext;

public class LinkRestModule_Pojo_IT extends BQLinkRestTest {

	@Override
	protected Module createExtrasModule() {
		return b -> {
			LinkRestModule.contributeExtraEntities(b).addBinding().toInstance(LrEntityBuilder.build(E1.class));
			JerseyModule.contributeResources(b).addBinding().to(R1.class);
		};
	}

	@Test
	public void testLRRequest() {
		Response response = target("/r1").request().get();
		assertEquals(Status.OK.getStatusCode(), response.getStatus());
		assertEquals("{\"data\":[{\"id\":1,\"name\":\"xyz\"}],\"total\":1}", response.readEntity(String.class));
	}

	@Path("/r1")
	public static class R1 {

		@Context
		private Configuration config;

		@GET
		public DataResponse<E1> get(@Context UriInfo uriInfo) {
			return LinkRest.select(E1.class, config).uri(uriInfo).listener(new PojoListener()).select();
		}
	}

	public static class E1 {

		private int id;
		private String name;

		@LrId
		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		@LrAttribute
		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
	}

	public static class PojoListener {

		@SelectServerParamsApplied
		public ProcessingStage<SelectContext<E1>, E1> altBackend(SelectContext<E1> context) {

			E1 e1 = new E1();
			e1.setId(1);
			e1.setName("xyz");
			context.getResponse().withObject(e1);

			// stop further processing
			return null;
		}
	}

}

@@ -75,16 +75,47 @@
 quals;%0A%0A
+import java.util.Collections;%0A%0A
 import j
@@ -733,55 +733,8 @@
 ed;%0A
-import com.nhl.link.rest.meta.LrEntityBuilder;%0A
 impo
@@ -981,111 +981,8 @@
 %3E %7B%0A
-%09%09%09LinkRestModule.contributeExtraEntities(b).addBinding().toInstance(LrEntityBuilder.build(E1.class));%0A
 %09%09%09J
@@ -2103,36 +2103,49 @@
 ext.
-getResponse().withObjec
+setObjects(Collections.singletonLis
 t(e1)
+)
 ;%0A%0A%09

Upgrade to LinkRest 1.24 #5