package com.enterprisesolutions.resources;

import com.enterprisesolutions.core.WebhookVerifier;
import com.enterprisesolutions.exceptions.InvalidWebhookException;
import com.enterprisesolutions.providers.InvalidWebhookExceptionMapper;
import io.dropwizard.testing.junit.ResourceTestRule;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

public class WebhookResourceTest {
    private static final WebhookVerifier verifier = mock(WebhookVerifier.class);

    @ClassRule
    public static final ResourceTestRule resources = ResourceTestRule.builder()
            .addResource(new WebhookResource(verifier))
            .addProvider(new InvalidWebhookExceptionMapper())
            .build();

    @Before
    public void setUp() {
        reset(verifier);
    }

    @Test
    public void shouldReturnOkIfSignatureValid() {
        Response response = resources.client().target("/webhooks").request()
                .header("Webhook-Key-Id", "key")
                .header("Webhook-Signature", "sig")
                .post(Entity.entity("body", APPLICATION_JSON_TYPE));

        assertThat(response.getStatus()).isEqualTo(204);

        verify(verifier).verify("body", "sig");
    }

    @Test
    public void shouldReturnErrorIfSignatureInvalid() {
        doThrow(new InvalidWebhookException("oh noes!")).when(verifier).verify("body", "sig");

        Response response = resources.client().target("/webhooks").request()
                .header("Webhook-Key-Id", "key")
                .header("Webhook-Signature", "sig")
                .post(Entity.entity("body", APPLICATION_JSON_TYPE));

        assertThat(response.getStatus()).isEqualTo(498);

        verify(verifier).verify("body", "sig");
    }
}
@@ -125,75 +125,8 @@
 ons.
-exceptions.InvalidWebhookException;%0Aimport com.enterprisesolutions.
 prov
@@ -160,16 +160,55 @@
 onMapper
+;%0Aimport com.google.common.io.Resources
 ;%0Aimport
@@ -258,33 +258,8 @@
 le;%0A
-import org.junit.Before;%0A
 impo
@@ -375,16 +375,69 @@
 ponse;%0A%0A
+import static com.google.common.base.Charsets.UTF_8;%0A
 import s
@@ -553,45 +553,8 @@
 hat;
-%0Aimport static org.mockito.Mockito.*;
 %0A%0Apu
@@ -605,22 +605,16 @@
  static 
-final 
 WebhookV
@@ -636,13 +636,12 @@
 r = 
-mock(
+new 
 Webh
@@ -655,14 +655,51 @@
 fier
-.class
+(%22ElfJ-3tF9I_zutNVK2lBABQrw-BgAhkZKIlvmbgk%22
 );%0A%0A
@@ -943,14 +943,12 @@
    @
-Before
+Test
 %0A   
@@ -965,109 +965,242 @@
 id s
-etUp() %7B%0A        reset(verifier)
+houldReturnOkIfSignatureValid() throws Exception %7B%0A        String signature = %224d48a688e8bd6c313e3eecc78fa55b3e4ae23c65e70cf35038010f47742fb670%22
 ;%0A    
-%7D%0A%0A
     
-@Test%0A    public void shouldReturnOkIfSignatureValid() %7B
+String body = Resources.toString(Resources.getResource(%22webhook.json%22), UTF_8);%0A
 %0A   
@@ -1359,37 +1359,41 @@
 ook-Signature%22, 
-%22
 sig
-%22
+nature
 )%0A              
@@ -1406,38 +1406,36 @@
 t(Entity.entity(
-%22
 body
-%22
 , APPLICATION_JS
@@ -1506,57 +1506,8 @@
 04);
-%0A%0A        verify(verifier).verify(%22body%22, %22sig%22);
 %0A   
@@ -1578,102 +1578,207 @@
 d() 
-%7B%0A        doThrow(new InvalidWebhookException(%22oh noes!%22)).when(verifier).verify(%22body%22, %22sig%22
+throws Exception %7B%0A        String signature = %224d48a688e8bd6c313e3eecc78fa55b3e4ae23c65e70cf35038010f47742fb671%22;%0A        String body = Resources.toString(Resources.getResource(%22webhook.json%22), UTF_8
 );%0A%0A
@@ -1948,21 +1948,25 @@
 ature%22, 
-%22
 sig
-%22
+nature
 )%0A      
@@ -1995,22 +1995,20 @@
 .entity(
-%22
 body
-%22
 , APPLIC
@@ -2087,57 +2087,8 @@
 98);
-%0A%0A        verify(verifier).verify(%22body%22, %22sig%22);
 %0A   

Remove mocking from WebhookResourceTest