/*
 * Copyright 2016-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.vorb.platon.web.api.controllers;

import de.vorb.platon.jooq.tables.records.CommentsRecord;
import de.vorb.platon.model.CommentStatus;
import de.vorb.platon.persistence.CommentRepository;

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.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import java.sql.Timestamp;
import java.time.Instant;

import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class CommentControllerIT {

    private static final String CREATION_DATE = "2017-10-06T19:45:23.751Z";
    private static final String LAST_MODIFICATION_DATE = "2017-10-06T19:48:51.179Z";

    private static final CommentsRecord SAMPLE_COMMENT = new CommentsRecord(
            4711L,
            25L,
            1336L,
            Timestamp.from(Instant.parse(CREATION_DATE)),
            Timestamp.from(Instant.parse(LAST_MODIFICATION_DATE)),
            CommentStatus.PUBLIC.toString(),
            "Sample text",
            "John Doe",
            "DBe/ZuZJBwFncB0tPNcXEQ==",
            "https://example.org"
    );

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private CommentRepository commentRepository;

    @Before
    public void setUp() throws Exception {
        when(commentRepository.findById(eq(SAMPLE_COMMENT.getId()))).thenReturn(SAMPLE_COMMENT);
    }

    @Test
    public void getCommentByIdReturnsSingleComment() throws Exception {
        mockMvc.perform(
                get("/api/comments/{id}", SAMPLE_COMMENT.getId())
                        .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.id").value(SAMPLE_COMMENT.getId()))
                .andExpect(jsonPath("$.parentId").value(SAMPLE_COMMENT.getParentId()))
                .andExpect(jsonPath("$.creationDate").value(CREATION_DATE))
                .andExpect(jsonPath("$.lastModificationDate").value(LAST_MODIFICATION_DATE))
                .andExpect(jsonPath("$.status").value(SAMPLE_COMMENT.getStatus()))
                .andExpect(jsonPath("$.text").value(SAMPLE_COMMENT.getText()))
                .andExpect(jsonPath("$.author").value(SAMPLE_COMMENT.getAuthor()))
                .andExpect(jsonPath("$.emailHash").value("0c17bf66e649070167701d2d3cd71711"))
                .andExpect(jsonPath("$.url").value(SAMPLE_COMMENT.getUrl()));
    }

}

@@ -1856,16 +1856,23 @@
  String 
+SAMPLE_
 CREATION
@@ -1939,16 +1939,23 @@
  String 
+SAMPLE_
 LAST_MOD
@@ -2076,16 +2076,17 @@
 sRecord(
+)
 %0A       
@@ -2094,14 +2094,21 @@
     
+.setId(
 4711L
-,
+)
 %0A   
@@ -2120,12 +2120,25 @@
     
+.setThreadId(
 25L
-,
+)
 %0A   
@@ -2150,14 +2150,27 @@
     
+.setParentId(
 1336L
-,
+)
 %0A   
@@ -2170,32 +2170,49 @@
 6L)%0A            
+.setCreationDate(
 Timestamp.from(I
@@ -2224,16 +2224,23 @@
 t.parse(
+SAMPLE_
 CREATION
@@ -2238,33 +2238,33 @@
 _CREATION_DATE))
-,
+)
 %0A            Tim
@@ -2260,16 +2260,41 @@
         
+.setLastModificationDate(
 Timestam
@@ -2314,16 +2314,23 @@
 t.parse(
+SAMPLE_
 LAST_MOD
@@ -2345,17 +2345,17 @@
 N_DATE))
+)
-,
 %0A       
@@ -2359,16 +2359,27 @@
         
+.setStatus(
 CommentS
@@ -2401,17 +2401,17 @@
 String()
-,
+)
 %0A       
@@ -2415,16 +2415,25 @@
         
+.setText(
 %22Sample 
@@ -2437,17 +2437,17 @@
 le text%22
-,
+)
 %0A       
@@ -2451,16 +2451,27 @@
         
+.setAuthor(
 %22John Do
@@ -2472,17 +2472,17 @@
 ohn Doe%22
-,
+)
 %0A       
@@ -2486,16 +2486,30 @@
         
+.setEmailHash(
 %22DBe/ZuZ
@@ -2526,17 +2526,17 @@
 NcXEQ==%22
-,
+)
 %0A       
@@ -2540,16 +2540,24 @@
         
+.setUrl(
 %22https:/
@@ -2569,21 +2569,16 @@
 ple.org%22
-%0A    
 );%0A%0A    
@@ -3341,16 +3341,23 @@
 ).value(
+SAMPLE_
 CREATION
@@ -3432,16 +3432,23 @@
 ).value(
+SAMPLE_
 LAST_MOD
@@ -3879,16 +3879,76 @@
 tUrl()))
+%0A                .andExpect(jsonPath(%22$.replies%22).isEmpty())
 ;%0A    %7D%0A

Improve CommentController integration test