package edu.prlab.tyler.iotgateway.gateway;

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.prlab.tyler.iotgateway.cloud.pojo.auth.SensitiveUser;
import edu.prlab.tyler.iotgateway.cloud.pojo.device.Device;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyChoice;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyContent;
import edu.prlab.tyler.iotgateway.cloud.pojo.privacy.PrivacyPolicyReport;
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.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = GatewayApplication.class)
public class GatewayHttpApiTest {

    @Autowired
    WebApplicationContext context;

    private MockMvc mvc;
    private ObjectMapper mapper;

    @Before
    public void setup() {
        this.mvc = MockMvcBuilders
                .webAppContextSetup(context)
                .build();
        this.mapper = new ObjectMapper();
    }

    @Test
    public void getPrivacyPolicyAndSetChoice() throws Exception{

        //取得裝置清單

        //拿取隱私政策
        MvcResult result = mvc.perform(MockMvcRequestBuilders
                .get("/privacy/" + "a1252c49-4188-4e6d-a32e-66604c664fb8")
                .accept(MediaType.APPLICATION_JSON_UTF8))
                .andDo(print())
                .andExpect(status().isOk())
                .andReturn();
        PrivacyPolicyReport report = mapper.readValue(result.getResponse().getContentAsString(), PrivacyPolicyReport.class);
        Assert.assertNotNull(report);

        //表達隱私偏好
        Device device = report.getDevice();
        PrivacyChoice privacyChoice = PrivacyChoice.builder()
                .privacyContent(PrivacyContent.builder()
                        .user(SensitiveUser.builder()
                                .username("test")
                                .password("test")
                                .build())
                        .device(device)
                        .policy(report.getPolicies().get(0))
                        .build())
                .isAccepted(true)
                .build();
        mapper.writeValueAsString(privacyChoice);
        result = mvc.perform(MockMvcRequestBuilders
                .post("/choice")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .accept(MediaType.APPLICATION_JSON_UTF8)
                .content(mapper.writeValueAsString(privacyChoice)))
                .andDo(print())
                .andExpect(status().isOk())
                .andReturn();
        Assert.assertNotNull(result);
    }
}
