// Path: src/test/java/eu/euregjug/site/config/SecurityTestConfig.java
// @Configuration
// @Profile("test")
// public class SecurityTestConfig extends WebSecurityConfigurerAdapter {
// 
//     @Override
//     protected void configure(HttpSecurity http) throws Exception {
//         http.csrf().disable();
//     }
// }
// 
// Path: src/main/java/eu/euregjug/site/posts/PostEntity.java
// public enum Format {
// 
//     asciidoc, markdown
// }
// 
// Path: src/main/java/eu/euregjug/site/posts/PostEntity.java
// public enum Status {
// 
//     draft, published, hidden
// }


import eu.euregjug.site.config.SecurityTestConfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.euregjug.site.posts.PostEntity.Format;
import eu.euregjug.site.posts.PostEntity.Status;
import java.util.Arrays;
import java.util.Date;
import java.util.Locale;
import java.util.Optional;
import org.joor.Reflect;
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.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;

    @Test
    public void searchShouldWork() throws Exception {
        final PostEntity p1 = Reflect.on(
                new PostEntity(new Date(), "new-site-is-live", "New site is live", "Welcome to the new EuregJUG website. We have switched off the old static pages and replaced it with a little application based on Hibernate, Spring Data JPA, Spring Boot and Thymeleaf.")
        ).call("updateUpdatedAt").set("id", 23).get();

        when(this.postRepository.searchByKeyword("website")).thenReturn(Arrays.asList(p1));

        this.mvc
                .perform(
                        get("/api/posts/search")
                                .param("q", "website")
                                .principal(() -> "euregjug")
                )
                .andExpect(status().isOk())
                .andExpect(jsonPath("$", hasSize(1)))
                .andExpect(jsonPath("$[0].slug", equalTo("new-site-is-live")))
                .andDo(document("api/posts/search",
                        preprocessRequest(prettyPrint()),
                        preprocessResponse(prettyPrint())
                ));

        verify(this.postRepository).searchByKeyword("website");
        verifyNoMoreInteractions(this.postRepository);
    }

    @Test
    public void updateShouldShouldWork() throws Exception {
        final PostEntity updateEntity = new PostEntity(new Date(), "newslug", "newtitle", "newcontent");
// Path: src/test/java/eu/euregjug/site/config/SecurityTestConfig.java
// @Configuration
// @Profile("test")
// public class SecurityTestConfig extends WebSecurityConfigurerAdapter {
// 
//     @Override
//     protected void configure(HttpSecurity http) throws Exception {
//         http.csrf().disable();
//     }
// }
// 
// Path: src/main/java/eu/euregjug/site/posts/PostEntity.java
// public enum Format {
// 
//     asciidoc, markdown
// }
// 
// Path: src/main/java/eu/euregjug/site/posts/PostEntity.java
// public enum Status {
// 
//     draft, published, hidden
// }


// Path: src/test/java/eu/euregjug/site/posts/PostApiControllerTest.java
import eu.euregjug.site.config.SecurityTestConfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.euregjug.site.posts.PostEntity.Format;
import eu.euregjug.site.posts.PostEntity.Status;
import java.util.Arrays;
import java.util.Date;
import java.util.Locale;
import java.util.Optional;
import org.joor.Reflect;
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.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessResponse;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;


    @Test
    public void searchShouldWork() throws Exception {
        final PostEntity p1 = Reflect.on(
                new PostEntity(new Date(), "new-site-is-live", "New site is live", "Welcome to the new EuregJUG website. We have switched off the old static pages and replaced it with a little application based on Hibernate, Spring Data JPA, Spring Boot and Thymeleaf.")
        ).call("updateUpdatedAt").set("id", 23).get();

        when(this.postRepository.searchByKeyword("website")).thenReturn(Arrays.asList(p1));

        this.mvc
                .perform(
                        get("/api/posts/search")
                                .param("q", "website")
                                .principal(() -> "euregjug")
                )
                .andExpect(status().isOk())
                .andExpect(jsonPath("$", hasSize(1)))
                .andExpect(jsonPath("$[0].slug", equalTo("new-site-is-live")))
                .andDo(document("api/posts/search",
                        preprocessRequest(prettyPrint()),
                        preprocessResponse(prettyPrint())
                ));

        verify(this.postRepository).searchByKeyword("website");
        verifyNoMoreInteractions(this.postRepository);
    }

    @Test
    public void updateShouldShouldWork() throws Exception {
        final PostEntity updateEntity = new PostEntity(new Date(), "newslug", "newtitle", "newcontent");
        updateEntity.setFormat(Format.markdown);