package com.programmingskillz.familytreexmlparser.web;

import static org.assertj.core.api.Assertions.assertThat;

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.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DocumentControllerIntegrationTest {

  private static final String URL = "/documents";

  private HttpHeaders httpHeaders;

  @Autowired
  private TestRestTemplate restTemplate;

  @Before
  public void init() {
    httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_XML);
  }

  @Test
  public void shouldReturnCreatedWhenValidRequest() {
    String xmlEntity = "<entries>" +
        "<entry>Adam</entry>" +
        "<entry parentName=\"Adam\">Stjepan</entry>" +
        "<entry parentName=\"Stjepan\">Luka</entry>" +
        "<entry parentName=\"Adam\">Leopold</entry>" +
        "</entries>";

    HttpEntity<String> httpEntity = new HttpEntity<>(xmlEntity, httpHeaders);

    ResponseEntity<String> response = restTemplate.postForEntity(URL, httpEntity, String.class);

    assertThat(response.getStatusCodeValue()).isEqualTo(201);
    assertThat(response.getBody()).contains("Document inserted successfully");
  }

  @Test
  public void shouldReturnBadRequestWhenNoRoot() {
    String xmlEntity = "<entries>" +
        "<entry parentName=\"Adam\">Stjepan</entry>" +
        "<entry parentName=\"Stjepan\">Luka</entry>" +
        "<entry parentName=\"Adam\">Leopold</entry>" +
        "</entries>";

    HttpEntity<String> httpEntity = new HttpEntity<>(xmlEntity, httpHeaders);

    ResponseEntity<String> response = restTemplate.postForEntity(URL, httpEntity, String.class);

    assertThat(response.getStatusCodeValue()).isEqualTo(400);
    assertThat(response.getBody()).contains("Root entry is missing");
  }

  @Test
  public void shouldReturnBadRequestWhenMoreThanOneRoot() {
    String xmlEntity = "<entries>" +
        "<entry>Adam</entry>" +
        "<entry>Noa</entry>" +
        "<entry parentName=\"Adam\">Stjepan</entry>" +
        "<entry parentName=\"Stjepan\">Luka</entry>" +
        "<entry parentName=\"Adam\">Leopold</entry>" +
        "</entries>";

    HttpEntity<String> httpEntity = new HttpEntity<>(xmlEntity, httpHeaders);

    ResponseEntity<String> response = restTemplate.postForEntity(URL, httpEntity, String.class);

    assertThat(response.getStatusCodeValue()).isEqualTo(400);
    assertThat(response.getBody()).contains("Only one root entry is allowed");
  }
}
@@ -112,33 +112,8 @@
 t;%0A%0A
-import org.junit.Before;%0A
 impo
@@ -354,16 +354,76 @@
 mplate;%0A
+import org.springframework.core.ParameterizedTypeReference;%0A
 import o
@@ -503,16 +503,60 @@
 eaders;%0A
+import org.springframework.http.HttpMethod;%0A
 import o
@@ -756,16 +756,39 @@
 unner;%0A%0A
+import java.util.Map;%0A%0A
 @ActiveP
@@ -1013,44 +1013,8 @@
 %22;%0A%0A
-  private HttpHeaders httpHeaders;%0A%0A
   @A
@@ -1068,142 +1068,8 @@
 e;%0A%0A
-  @Before%0A  public void init() %7B%0A    httpHeaders = new HttpHeaders();%0A    httpHeaders.setContentType(MediaType.APPLICATION_XML);%0A  %7D%0A%0A
   @T
@@ -1391,109 +1391,43 @@
     
-Http
+Response
 Entity%3C
+Map%3C
 String
-%3E httpEntity = new HttpEntity%3C%3E(xmlEntity, httpHeaders);%0A%0A    ResponseEntity%3C
+, 
 String%3E
+%3E
  res
@@ -1438,64 +1438,29 @@
 e = 
-restTemplate.postForEntity(URL, httpEntity, String.class
+getResponse(xmlEntity
 );%0A%0A
@@ -1554,26 +1554,42 @@
 etBody()
-).contains
+.get(%22message%22)).isEqualTo
 (%22Docume
@@ -1913,109 +1913,43 @@
     
-Http
+Response
 Entity%3C
+Map%3C
 String
-%3E httpEntity = new HttpEntity%3C%3E(xmlEntity, httpHeaders);%0A%0A    ResponseEntity%3C
+, 
 String%3E
+%3E
  res
@@ -1960,64 +1960,29 @@
 e = 
-restTemplate.postForEntity(URL, httpEntity, String.class
+getResponse(xmlEntity
 );%0A%0A
@@ -2076,26 +2076,42 @@
 etBody()
-).contains
+.get(%22message%22)).isEqualTo
 (%22Root e
@@ -2498,109 +2498,43 @@
     
-Http
+Response
 Entity%3C
+Map%3C
 String
-%3E httpEntity = new HttpEntity%3C%3E(xmlEntity, httpHeaders);%0A%0A    ResponseEntity%3C
+, 
 String%3E
+%3E
  res
@@ -2545,64 +2545,29 @@
 e = 
-restTemplate.postForEntity(URL, httpEntity, String.class
+getResponse(xmlEntity
 );%0A%0A
@@ -2665,18 +2665,34 @@
 dy()
-).contains
+.get(%22message%22)).isEqualTo
 (%22On
@@ -2726,10 +2726,442 @@
 d%22);%0A  %7D
+%0A%0A  private ResponseEntity%3CMap%3CString, String%3E%3E getResponse(String requestEntity) %7B%0A    HttpHeaders httpHeaders = new HttpHeaders();%0A    httpHeaders.setContentType(MediaType.APPLICATION_XML);%0A%0A    HttpEntity%3CString%3E httpEntity = new HttpEntity%3C%3E(requestEntity, httpHeaders);%0A%0A    return restTemplate.exchange(%0A        URL,%0A        HttpMethod.POST,%0A        httpEntity,%0A        ParameterizedTypeReference.forType(Map.class)%0A    );%0A  %7D
 %0A%7D

Refactor Controller Integration test