package unal.architecture.test.integration;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import org.junit.*;
import unal.architecture.entity.User;

import javax.naming.NamingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

import static org.junit.Assert.*;

public class UserRESTIT {
    private static final String URI = "http://localhost:8080/SoftwareArchitectureUNAL/users";
    private static Client client;

    @BeforeClass
    public static void beforeClass() throws NamingException {
        client = ClientBuilder.newClient().register(JacksonJsonProvider.class);
    }

    @AfterClass
    public static void afterClass() throws NamingException {
        client.close();
    }

    @Before
    public void before() {
    }

    @After
    public void after() {
    }

    @Test
    public void findAdminFromList() {
        List<User> users;

        users = client.target(URI)
                .request(MediaType.APPLICATION_JSON)
                .get(new GenericType<List<User>>() {
                });
        assertNotNull(users);
        assertTrue(!users.isEmpty());

        boolean found = false;
        for (User u : users) {
            if (u.getUsername().equals("admin")) {
                found = true;
                break;
            }
        }
        assertTrue(found);
    }

    @Test
    public void findAdmin() {
        User user;

        user = client.target(URI)
                .path("admin")
                .request(MediaType.APPLICATION_JSON)
                .get(User.class);
        assertNotNull(user);
    }

    @Test
    public void crudUser() {
        Response response;
        User user;

        //Create
        user = new User();
        user.setName("Test User");
        user.setUsername("testuser");

        user = client.target(URI)
                .request(MediaType.APPLICATION_JSON)
                .post(Entity.json(user), User.class);
        assertNotNull(user);

        //Read
        user = client.target(URI)
                .path(user.getUsername())
                .request(MediaType.APPLICATION_JSON)
                .get(User.class);
        assertNotNull(user);
        assertEquals("Test User", user.getName());
        assertEquals("testuser", user.getUsername());

        //Update
        user.setEmail("testuser@architecure.unal");
        user = client.target(URI)
                .path(user.getUsername())
                .request(MediaType.APPLICATION_JSON)
                .put(Entity.json(user), User.class);
        assertNotNull(user);
        assertEquals("testuser@architecure.unal", user.getEmail());

        //Delete
        response = client.target(URI)
                .path(user.getUsername())
                .request(MediaType.APPLICATION_JSON)
                .delete();
        assertEquals(204, response.getStatus());

        user = client.target(URI)
                .path(user.getUsername())
                .request(MediaType.APPLICATION_JSON)
                .get(User.class);
        assertNull(user);
    }
}
@@ -156,16 +156,62 @@
 ty.User;
+%0Aimport unal.architecture.entity.UserPassword;
 %0A%0Aimport
@@ -1858,16 +1858,18 @@
         
+/*
 Response
@@ -1889,32 +1889,67 @@
       User user;
+%0A        UserPassword userPassword;
 %0A%0A        //Crea
@@ -2051,17 +2051,16 @@
 user%22);%0A
-%0A
         
@@ -2055,32 +2055,305 @@
 %22);%0A        user
+.setSalesman(false);%0A        user.setWorker(false);%0A        user.setAdmin(false);%0A        user.setEmail(%22amrondonp@carapp.com%22);%0A%0A        userPassword = new UserPassword();%0A        userPassword.setPassword(%22pass%22);%0A        userPassword.setUser(user);%0A%0A%0A        userPassword
  = client.target
@@ -2441,39 +2441,55 @@
 Entity.json(user
-), User
+Password), UserPassword
 .class);%0A       
@@ -2503,24 +2503,32 @@
 NotNull(user
+Password
 );%0A%0A        
@@ -3592,20 +3592,22 @@
 tNull(user);
+*/
 %0A    %7D%0A%7D

Test fixed but needs revision