
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.github.proxyprint.kitchen.WebAppConfig;
import io.github.proxyprint.kitchen.models.User;
import io.github.proxyprint.kitchen.models.repositories.UserDAO;
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.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(WebAppConfig.class)
@WebIntegrationTest
public class ConsumerTests {

    @Autowired
    WebApplicationContext wac;
    @Autowired
    private UserDAO users;
    @Autowired
    private Gson GSON;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    //testar se o servidor regista utilizador
    @Test
    public void registerUser() throws Exception {
        MvcResult mvcResult = this.mockMvc.perform(post("/consumer/register")
                .param("username", "testusername")
                .param("password", "testpassword")
                .param("name", "testname")
                .param("email", "testemail@mail.pt")
                .param("latitude", "testlat")
                .param("longitude", "testlong")).andReturn();

        String response = mvcResult.getResponse().getContentAsString();

        JsonObject jsonObject = (new JsonParser()).parse(response).getAsJsonObject();

        boolean status = jsonObject.get("success").getAsBoolean();

        assert (status);
    }

    //testar se o servidor deteta utilizador já registado
    @Test
    public void errorRegisterUser() throws Exception {
        MvcResult mvcResult = this.mockMvc.perform(post("/consumer/register")
                .param("username", "testusername")
                .param("password", "testpassword")
                .param("name", "testname")
                .param("email", "testemail@mail.pt")
                .param("latitude", "testlat")
                .param("longitude", "testlong")).andReturn();

        String response = mvcResult.getResponse().getContentAsString();
        System.out.println(response);
        JsonObject jsonObject = (new JsonParser()).parse(response).getAsJsonObject();

        boolean status = !jsonObject.get("success").getAsBoolean();

        assert (status);

        User user = this.users.findByUsername("testusername");
        this.users.delete(user);
    }
}

@@ -2086,32 +2086,546 @@
 status);%0A    %7D%0A%0A
+    //testar login%0A    @Test%0A    public void login() throws Exception %7B%0A        MvcResult mvcResult = this.mockMvc.perform(post(%22/login%22)%0A                .param(%22username%22, %22testusername%22)%0A                .param(%22password%22, %22testpassword%22)).andReturn();%0A%0A        String response = mvcResult.getResponse().getContentAsString();%0A        JsonObject jsonObject = (new JsonParser()).parse(response).getAsJsonObject();%0A%0A        boolean status = jsonObject.get(%22success%22).getAsBoolean();%0A%0A        assert (status);%0A    %7D%0A%0A
     //testar se 
@@ -3192,46 +3192,8 @@
 ();%0A
-        System.out.println(response);%0A
     

test login