package org.superdev.coddy.user.api;


import com.shazam.shazamcrest.MatcherAssert;
import com.shazam.shazamcrest.matcher.Matchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;
import org.springframework.http.HttpHeaders;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.superdev.coddy.Main;
import org.superdev.coddy.application.data.Response;
import org.superdev.coddy.application.utils.TestUtils;
import org.superdev.coddy.user.data.Credential;
import org.superdev.coddy.user.data.Token;
import org.superdev.coddy.user.elasticsearch.entity.UserEntity;
import org.superdev.coddy.user.elasticsearch.repository.UserRepository;

import java.io.IOException;
import java.util.Arrays;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
        classes = Main.class)
public class UserTest {

    private static final String USER_ENDPOINT = "/user";

    private TestRestTemplate restTemplate = new TestRestTemplate();

    @Autowired
    private UserRepository repository;

    @Before
    public void populateDataBase() throws IOException {
        UserEntity[] users = TestUtils.getObjecFromJson("user/user.database.json", UserEntity[].class);

        repository.save(Arrays.asList(users));
    }

    @After
    public void dropDatabase() {
        repository.deleteAll();
    }

    @Test
    public void testGetUserWithoutToken() {
        ResponseEntity<Response> response =
                restTemplate.getForEntity(TestUtils.getUrl(USER_ENDPOINT + "/napoleon"), Response.class);

        Response expected = new Response("HTTP 401 Unauthorized");

        Assert.assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
        MatcherAssert.assertThat(response.getBody(), Matchers.sameBeanAs(expected));
    }

    @Test
    public void testGetUserWithToken() throws IOException {
        Credential credential = new Credential("ciceron", "tutu".toCharArray());
        Token token = restTemplate.postForEntity(TestUtils.getUrl(USER_ENDPOINT + "/auth"), credential, Token.class).getBody();

        HttpHeaders headers = new HttpHeaders();
        headers.add(javax.ws.rs.core.HttpHeaders.AUTHORIZATION, token.getToken());
        headers.add(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, javax.ws.rs.core.MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<>("parameters", headers);

        ResponseEntity<UserEntity> response = restTemplate.exchange(TestUtils.getUrl(USER_ENDPOINT + "/ciceron"), HttpMethod.GET, entity, UserEntity.class);

        Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
        MatcherAssert.assertThat(response.getBody(), Matchers.sameBeanAs(TestUtils.getObjecFromJson("user/getUserWithTokenExpected.json", UserEntity.class)));
    }
}

@@ -2210,46 +2210,58 @@
     
-Credential cred
+HttpEntity%3CString%3E 
 enti
-al = new Credential
+ty = getHttpEntityWithToken
 (%22ci
@@ -2278,33 +2278,20 @@
 utu%22
-.toCharArray()
 );%0A
+%0A
         
 Toke
@@ -2290,19 +2290,43 @@
     
-Token token
+ResponseEntity%3CUserEntity%3E response
  = r
@@ -2341,21 +2341,16 @@
 ate.
-postForEntity
+exchange
 (Tes
@@ -2385,189 +2385,296 @@
 + %22/
-auth%22), credential, Token.class).getBody();%0A%0A        HttpHeaders headers = new HttpHeaders();%0A        headers.add(javax.ws.rs.core.HttpHeaders.AUTHORIZATION, token.getToken(
+ciceron%22), HttpMethod.GET, entity, UserEntity.class);%0A%0A        Assert.assertEquals(HttpStatus.OK, response.getStatusCode());%0A        MatcherAssert.assertThat(response.getBody(), Matchers.sameBeanAs(TestUtils.getObjecFromJson(%22user/getUserWithTokenExpected.json%22, UserEntity.class)
 ));%0A    
     
@@ -2673,111 +2673,103 @@
     
+%7D%0A%0A
     
-headers.add(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, javax.ws.rs.core.MediaType.APPLICATION_JSON)
+@Test%0A    public void testDeleteUserWithToken() %7B%0A        final String login = %22ciceron%22
 ;%0A  
@@ -2802,20 +2802,19 @@
 ntity = 
-new 
+get
 HttpEnti
@@ -2819,36 +2819,34 @@
 tity
-%3C%3E(%22parameters%22, headers
+WithToken(login, %22tutu%22
 );%0A
-%0A
     
@@ -2860,34 +2860,30 @@
 ponseEntity%3C
-UserEntity
+String
 %3E response =
@@ -2954,35 +2954,38 @@
 n%22), HttpMethod.
-G
+DEL
 ET
+E
 , entity, UserEn
@@ -2974,34 +2974,30 @@
 TE, entity, 
-UserEntity
+String
 .class);%0A%0A  
@@ -3025,34 +3025,42 @@
 uals(HttpStatus.
-OK
+NO_CONTENT
 , response.getSt
@@ -3068,32 +3068,33 @@
 tusCode());%0A
+%0A
         
 MatcherAsser
@@ -3085,156 +3085,758 @@
     
-MatcherAssert.assertThat(response.getBody(), Matchers.sameBeanAs(TestUtils.getObjecFromJson(%22user/getUserWithTokenExpected.json%22, UserEntity.class))
+// check if the user is effectively deleted.%0A        UserEntity user = repository.findByLogin(login);%0A        Assert.assertEquals(user, null);%0A    %7D%0A%0A    private HttpEntity%3CString%3E getHttpEntityWithToken(final String login, final String password) %7B%0A        Credential credential = new Credential(login, password.toCharArray());%0A        Token token = restTemplate.postForEntity(TestUtils.getUrl(USER_ENDPOINT + %22/auth%22), credential, Token.class).getBody();%0A        HttpHeaders headers = new HttpHeaders();%0A        headers.add(javax.ws.rs.core.HttpHeaders.AUTHORIZATION, token.getToken());%0A        headers.add(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, javax.ws.rs.core.MediaType.APPLICATION_JSON);%0A        return new HttpEntity%3C%3E(%22parameters%22, headers
 );%0A 

add new test to delete user