package io.github.vyo.hello_jersey.rest;

import io.github.vyo.hello_jersey.entity.Greeting;
import io.github.vyo.hello_jersey.repository.GreetingStore;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import javax.ws.rs.core.Application;

import static org.junit.Assert.assertEquals;

public class HelloJerseyJerseyTest extends JerseyTest {

    @Override
    protected Application configure() {
        GreetingStore greetingStore = Mockito.mock(GreetingStore.class);
        Mockito.when(greetingStore.findGreeting(Mockito.anyString())).thenAnswer(new Answer<Greeting>() {
            @Override
            public Greeting answer(InvocationOnMock invocation)
                    throws Throwable {
                String name = (String) invocation.getArguments()[0];
                return new Greeting("Hello, I am a mock response. (" + name + ")");
            }

        });

        ResourceConfig resourceConfig = new ResourceConfig(HelloJersey.class);
        return resourceConfig;
    }

    @Test
    public void testHelloObject() {
        final String hello = target("rest/hellojersey/hello").request().get(Greeting.class).getMessage();
        assertEquals("Hello Jersey!", hello);
    }

    @Test
    public void testHelloJSON() {
        final String hello = target("rest/hellojersey/hello").request().get().readEntity(String.class);
        assertEquals("{\"message\":\"Hello Jersey!\"}", hello);
    }

    @Test
    public void testHelloEchoObject() {

        String echoString = "I am an echo!";
        String echoResult = new StringBuilder(echoString).reverse().toString();

        final String hello = target("rest/hellojersey/echo/" + echoString).request().get(Greeting.class).getMessage();
        assertEquals(echoResult, hello);
    }

    @Test
    public void testHelloEchoJSON() {

        String echoString = "I am an echo!";
        String echoResultJSON = "{\"message\":\"" + new StringBuilder(echoString).reverse().toString() + "\"}";

        final String helloJSON = target("rest/hellojersey/echo/" + echoString).request().get().readEntity(String
                .class);
        assertEquals(echoResultJSON, helloJSON);
    }
}
@@ -35,16 +35,80 @@
 .rest;%0A%0A
+import com.google.gson.Gson;%0Aimport com.google.gson.JsonParser;%0A
 import i
@@ -210,16 +210,75 @@
 gStore;%0A
+import org.glassfish.hk2.utilities.binding.AbstractBinder;%0A
 import o
@@ -419,92 +419,8 @@
 ito;
-%0Aimport org.mockito.invocation.InvocationOnMock;%0Aimport org.mockito.stubbing.Answer;
 %0A%0Aim
@@ -608,24 +608,103 @@
 nfigure() %7B%0A
+        ResourceConfig resourceConfig = new ResourceConfig(HelloJersey.class);%0A
         Gree
@@ -849,157 +849,281 @@
 wer(
-new Answer%3CGreeting%3E() %7B%0A            @Override%0A            public Greeting answer(InvocationOnMock invocation)%0A                    throws Throwable %7B
+invocation -%3E %7B%0A            String name = (String) invocation.getArguments()%5B0%5D;%0A%0A            if (name.equals(%22hawaii%22)) %7B%0A                return new Greeting(%22Aloha!%22);%0A            %7D else %7B%0A                return new Greeting(%22Hello, I am a mock response.%22);%0A            %7D
 %0A   
@@ -1123,32 +1123,36 @@
   %7D%0A        
+%7D);%0A
         
 String name 
@@ -1143,69 +1143,125 @@
     
-String name = (String) invocation.getArguments()%5B0%5D
+Mockito.when(greetingStore.defaultGreeting()).thenAnswer(invocation -%3E new Greeting(%22Hello mock Jersey!%22))
 ;%0A
+%0A
         
     
@@ -1260,81 +1260,264 @@
     
-        return new Greeting(%22Hello, I am a mock response. (%22 + name + %22)%22
+// context injection will not work, thus specify injection behaviour explicitly%0A        resourceConfig.register(new AbstractBinder() %7B%0A            @Override%0A            protected void configure() %7B%0A                bind(greetingStore).to(GreetingStore.class
 );%0A 
@@ -1525,25 +1525,24 @@
           %7D%0A
-%0A
         %7D);%0A
@@ -1544,88 +1544,8 @@
  %7D);
-%0A%0A        ResourceConfig resourceConfig = new ResourceConfig(HelloJersey.class);
 %0A   
@@ -1755,24 +1755,29 @@
 uals(%22Hello 
+mock 
 Jersey!%22, he
@@ -1949,62 +1949,289 @@
     
-assertEquals(%22%7B%5C%22message%5C%22:%5C
+//overkill at this point, but a simple string comparison does not suffice for JSON - contents may be in any%0A        // order%0A        JsonParser parser = new JsonParser();%0A        assertEquals(parser.parse(new Gson().toJson(new Greeting(
 %22Hello 
+mock 
 Jersey!
-%5C%22%7D%22, 
+%22))), parser.parse(
 hello)
+)
 ;%0A  
@@ -2702,34 +2702,10 @@
 sult
-JSON = %22%7B%5C%22message%5C%22:%5C%22%22 +
+ =
  new
@@ -2755,16 +2755,8 @@
 ng()
- + %22%5C%22%7D%22
 ;%0A%0A 
@@ -2904,37 +2904,272 @@
     
-assertEquals(echoResultJSON, 
+//overkill at this point, but a simple string comparison does not suffice for JSON - contents may be in any%0A        // order%0A        JsonParser parser = new JsonParser();%0A        assertEquals(parser.parse(new Gson().toJson(new Greeting(echoResult))), parser.parse(
 hell
@@ -3174,16 +3174,17 @@
 lloJSON)
+)
 ;%0A    %7D%0A

Update jersey test with injection and mocking