package tk.puncha.integration.webapp;

import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import tk.puncha.Application;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class PetControllerTests extends ControllerTests {

  @Configuration
  @EnableAutoConfiguration()
  static class Dummy extends Application {
  }

  @Before
  public void before() {
    createWebClient();
  }

  @Test
  public void shouldShowPetIndexPage() throws Exception {
    HtmlPage ownerIndexPage = getPage("/pets");
    assertEquals(13.0, ownerIndexPage.getByXPath("count(//tbody/tr)").get(0));

    List ownerProperties = ownerIndexPage.getByXPath("//tbody/tr[1]/td/text()");
    assertEquals("Leo", ownerProperties.get(0).toString());
    assertEquals("cat", ownerProperties.get(1).toString());
    assertEquals("2010-09-07", ownerProperties.get(2).toString());
    assertEquals("George Franklin", ownerProperties.get(3).toString());

    List viewEditDeleteLinks = ownerIndexPage.getByXPath("//tbody/tr[1]/td[5]//a");
    assertEquals("/pets/1", ((HtmlAnchor) viewEditDeleteLinks.get(0)).getHrefAttribute());
    assertEquals("/pets/1/edit", ((HtmlAnchor) viewEditDeleteLinks.get(1)).getHrefAttribute());
    assertEquals("/pets/1/delete", ((HtmlAnchor) viewEditDeleteLinks.get(2)).getHrefAttribute());
  }

  @Test
  public void shouldShowPetDetailPage() throws Exception {
    HtmlPage page = getPage("/pets/7");
    String content = page.asText();

    // Pet
    assertTrue(content.contains("Samantha"));
    assertTrue(content.contains("cat"));
    assertTrue(content.contains("2012-09-04"));

    // Owner
    assertTrue(content.contains("Jean"));

    // Visits
    assertTrue(content.contains("2013-01-04 spayed"));
    assertTrue(content.contains("2013-01-01 rabies shot"));
  }

  // TODO: Implement this test
  @Test
  public void shouldEditPet() throws Exception {
  }
}

@@ -74,25 +74,22 @@
 it.html.
-HtmlAncho
+DomAtt
 r;%0Aimpor
@@ -851,109 +851,84 @@
 t(//
-tbody/tr)%22).get(0));%0A%0A    List ownerProperties = ownerIndexPage.getByXPath(%22//tbody/tr%5B1%5D/td/t
+div%5B@class='card'%5D)%22).get(0));%0A%0A    String content = ownerIndexPage.asT
 ext()
-%22)
 ;%0A  
@@ -939,258 +939,209 @@
 sert
-Equals(%22Leo%22, ownerProperties.get(0).toString());%0A    assertEquals(%22cat%22, ownerProperties.get(1).toString());%0A    assertEquals(%222010-09-07%22, ownerProperties.get(2).toString());%0A    assertEquals(%22George Franklin%22, ownerProperties.get(3).toString(
+True(content.contains(%22Leo%22));%0A    assertTrue(content.contains(%22Owned by: George Franklin%22));%0A    assertTrue(content.contains(%22Birthday: 2010-09-07%22));%0A    assertTrue(content.contains(%22Type: cat%22
 ));%0A%0A
+%0A
     
@@ -1200,28 +1200,36 @@
 (%22//
-tbody/tr%5B1%5D/td%5B5%5D//a
+div%5B@class='card'%5D%5B1%5D//@href
 %22);%0A
@@ -1251,19 +1251,21 @@
 s(%22/
-pet
+owner
 s/1%22, ((
 Html
@@ -1256,33 +1256,30 @@
 wners/1%22, ((
-HtmlAncho
+DomAtt
 r) viewEditD
@@ -1296,36 +1296,108 @@
 .get(0)).get
-HrefAttribut
+Value());%0A    assertEquals(%22/pets/1%22, ((DomAttr) viewEditDeleteLinks.get(1)).getValu
 e());%0A    as
@@ -1421,33 +1421,30 @@
 /1/edit%22, ((
-HtmlAncho
+DomAtt
 r) viewEditD
@@ -1462,27 +1462,19 @@
 get(
-1
+2
 )).get
-HrefAttribut
+Valu
 e())
@@ -1516,17 +1516,14 @@
 , ((
-HtmlAncho
+DomAtt
 r) v
@@ -1549,27 +1549,19 @@
 get(
-2
+3
 )).get
-HrefAttribut
+Valu
 e())

Fix Test: Pet Controller Tests are broken due to pets are shown in Html CARD View