package org.piphonom.arepa.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.springtestdbunit.DbUnitTestExecutionListener;
import com.github.springtestdbunit.annotation.DatabaseOperation;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.github.springtestdbunit.annotation.DatabaseTearDown;
import com.jayway.jsonpath.JsonPath;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.test.web.servlet.MockMvc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * Created by piphonom
 */
@ActiveProfiles("test")
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class})
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@DatabaseSetup(DeviceRestTest.DATASET)
@DatabaseTearDown(type = DatabaseOperation.DELETE_ALL, value = { DeviceRestTest.DATASET })
@DirtiesContext
@AutoConfigureMockMvc
public class DeviceRestTest {
    protected static final String DATASET = "classpath:dbunit/device.xml";

    @Autowired
    private MockMvc mockMvc;

    private final String expectedName = "deviceName";
    private final String pubId = "2f4f";

    @Test
    public void getName() throws Exception {
        MockHttpServletResponse response = this.mockMvc.perform(post("/customer/register/")
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andDo(print())
                .andReturn().getResponse();

        String content = new String(response.getContentAsByteArray(), java.nio.charset.StandardCharsets.UTF_8);
        assertNotNull(content);
        String name = JsonPath.read(content, "$.name");
        assertEquals(expectedName, name);
    }

    private static String asJsonString(final Object obj) {
        try {
            final ObjectMapper mapper = new ObjectMapper();
            final String jsonContent = mapper.writeValueAsString(obj);
            return jsonContent;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

@@ -34,60 +34,8 @@
 e;%0A%0A
-import com.fasterxml.jackson.databind.ObjectMapper;%0A
 impo
@@ -90,24 +90,24 @@
 onListener;%0A
+
 import com.g
@@ -1431,19 +1431,18 @@
 uilders.
-pos
+ge
 t;%0Aimpor
@@ -2463,34 +2463,34 @@
 orm(
-pos
+ge
 t(%22/
-customer/register/%22
+point/name/%22 + pubId
 )%0A  
@@ -2861,16 +2861,16 @@
 name%22);%0A
+
         
@@ -2912,335 +2912,7 @@
    %7D
-%0A%0A    private static String asJsonString(final Object obj) %7B%0A        try %7B%0A            final ObjectMapper mapper = new ObjectMapper();%0A            final String jsonContent = mapper.writeValueAsString(obj);%0A            return jsonContent;%0A        %7D catch (Exception e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D
 %0A%7D%0A

Fix REST test uri. Commit is still NOT working yet