// Copyright 2019 Google LLC
//
// 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
//
//     https://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 com.google.sps;

import com.google.sps.data.Product;

import java.io.File;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** */
@RunWith(JUnit4.class)
public final class ProductListExtractorTest {
  private ProductListExtractor productListExtractor;

  @Before
  public void setUp() {
    productListExtractor = new ProductListExtractor();
  }

  @Test
  public void CompleteDoc() throws Exception {
    List<Product> expectedProducts = new ArrayList<>();
    expectedProducts.add(Product.create("TWSBI Eco <b>Fountain Pen</b> - Clear - Extra-Fine", 
        "https://image-link-1.com", 
        "<span class=\"HRLxBb\">$30.99</span> from Goldspot", 
        "https://product-link-1.com", 
        "+$4.95 shipping"));
    
    File completeDocFile = new File("./src/main/webapp/fake-shopping-results/complete-doc.html");
    Document completeDoc = Jsoup.parse(completeDocFile, "UTF-8");

    List<Product> actualProducts = productListExtractor.extract(completeDoc);

    Assert.assertEquals(expectedProducts, actualProducts);
  }

  @Test
  public void noProductRatingProvided() throws Exception  {
    List<Product> expectedProducts = new ArrayList<>();
    expectedProducts.add(Product.create("TWSBI Eco <b>Fountain Pen</b> - Clear - Extra-Fine", 
        "https://image-link-1.com", 
        "<span class=\"HRLxBb\">$30.99</span> from Goldspot", 
        "https://product-link-1.com", 
        "+$4.95 shipping"));
    
    File completeDocFile = new File("./src/main/webapp/fake-shopping-results/product-rating-missing.html");
    Document completeDoc = Jsoup.parse(completeDocFile, "UTF-8");

    List<Product> actualProducts = productListExtractor.extract(completeDoc);

    Assert.assertEquals(expectedProducts, actualProducts);
  }
}
@@ -1170,19 +1170,27 @@
 Complete
-Doc
+ProductData
 () throw
@@ -1530,107 +1530,251 @@
     
-%0A    File completeDocFile = new File(%22./src/main/webapp/fake-shopping-results/complete-doc.html%22
+expectedProducts.add(Product.create(%22Faber-Castell Fountain Pen%22, %0A        %22https://image-link-2.com%22, %0A        %22%3Cspan class=%5C%22HRLxBb%5C%22%3E$20.00%3C/span%3E from Faber-Castell%22, %0A        %22https://product-link-2.com%22, %0A        %22Free shipping%22)
 );%0A
+    %0A
     
@@ -1786,49 +1786,116 @@
 ent 
-completeDoc = Jsoup.parse(completeDocFile
+mockResultsDoc = %0A        Jsoup.parse(new File(%22./src/main/webapp/mock-shopping-results/complete-data.html%22)
 , %22U
@@ -1963,32 +1963,35 @@
 tor.extract(
-complete
+mockResults
 Doc);%0A%0A    A
@@ -2449,30 +2449,67 @@
     
-File completeDocFile =
+Document mockResultsDoc = %0A        Jsoup.parse(%0A           
  new
@@ -2537,12 +2537,12 @@
 app/
-fake
+mock
 -sho
@@ -2588,64 +2588,401 @@
 ml%22)
+, %22UTF-8%22)
 ;%0A
+%0A
     
-Document completeDoc = Jsoup.parse(completeDocFile
+List%3CProduct%3E actualProducts = productListExtractor.extract(mockResultsDoc);%0A%0A    Assert.assertEquals(expectedProducts, actualProducts);%0A  %7D%0A%0A  @Test%0A  public void NoProductData() throws Exception  %7B%0A    List%3CProduct%3E expectedProducts = new ArrayList%3C%3E();%0A    %0A    Document mockResultsDoc = %0A        Jsoup.parse(new File(%22./src/main/webapp/mock-shopping-results/no-data.html%22)
 , %22U
@@ -3054,24 +3054,27 @@
 extract(
-complete
+mockResults
 Doc);%0A%0A 
@@ -3136,8 +3136,9 @@
 );%0A  %7D%0A%7D
+%0A

Update ProductListExtractor tests