// 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 javatests;

import java.util.*;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import data.JsonConverter;
import data.Vendor;

/** Test the functionality of the JsonConverter class. */
@RunWith(JUnit4.class)
public final class JsonConverterTest {
  @Test
  public void testCreateFileMethod() {
    // Test that the method can create a file with content.
    JsonConverter converter = new JsonConverter();
    File file = converter.createFile();

    String expectedFileContent = "{\"Vehicle\":{\"Car\":\"Blue Tacoma\"}}";
    String fileContent = "";

    Scanner input;
    try {
      input = new Scanner(file);
    } catch(FileNotFoundException e) {
      input = null;
    }

    while(input.hasNextLine()) {
      fileContent += input.nextLine();
    }

    // Test that the file isn't empty and has the right content.
    assertFalse("createFile() didn't add any content to the file when is should've",
      fileContent.equals(""));
    assertTrue("createFile() didn't add the right content to the file.", 
      fileContent.equals(expectedFileContent));
  }

  @Test
  public void testUpdateFileMethod() throws IOException {
    // For the protoype, updateFile() calls createFile() and returns true.
    JsonConverter converter = new JsonConverter();
    Vendor vendor = new Vendor();

    boolean expectedResponse = true;
    boolean actualResponse = converter.updateFile(vendor);

    // Test that the actual and expected responses are the same.
    assertTrue("updateFile() didn't return true when it should have.", 
      expectedResponse == actualResponse);
  }
}

@@ -1084,86 +1084,593 @@
 t %7B%0A
-  @Test%0A  public void testCreateFileMethod() %7B%0A    // Test that the method can
+%0A  /** Test that createFile() successfully creates a new file. */  %0A  @Test%0A  public void testCreateFileMethodCreatesNewFile() %7B%0A    String vendorID = %22vend_1%22;%0A    String legVendID = %22legVend_27%22;%0A    int nextGenVendorID = 17;%0A    Vendor vendor = new Vendor(vendorID, legVendID, nextGenVendorID);%0A%0A    JsonConverter converter = new JsonConverter();%0A    File file = converter.createFile(vendor.getVendorID());%0A%0A    Scanner input;%0A    try %7B%0A      input = new Scanner(file);%0A    %7D catch(FileNotFoundException e) %7B%0A      input = null;%0A    %7D%0A%0A    assertFalse(%22createFile() didn't correctly
  cre
@@ -1679,26 +1679,355 @@
 e a 
+new 
 file
+.%22,%0A      input == null);%0A  %7D%0A%0A  /** Test that createFile()
  w
+r
 it
-h content.
+es the expected content to a file. */%0A  @Test%0A  public void testCreateFileMethodWritesExpectedContent() %7B%0A    String vendorID = %22vend_1%22;%0A    String legVendID = %22legVend_27%22;%0A    int nextGenVendorID = 17;%0A    Vendor vendor = new Vendor(vendorID, legVendID, nextGenVendorID);%0A
 %0A   
@@ -2111,16 +2111,36 @@
 ateFile(
+vendor.getVendorID()
 );%0A%0A    
@@ -2452,189 +2452,8 @@
  %7D%0A%0A
-    // Test that the file isn't empty and has the right content.%0A    assertFalse(%22createFile() didn't add any content to the file when is should've%22,%0A      fileContent.equals(%22%22));%0A
     
@@ -2575,16 +2575,75 @@
 );%0A  %7D%0A%0A
+  /** Test that updateFile() return true for prototype. */%0A
   @Test%0A
@@ -2708,78 +2708,37 @@
     
-// For the protoype, updateFile() calls createFile() and returns true.
+Vendor vendor = new Vendor();
 %0A   
@@ -2788,42 +2788,8 @@
 r();
-%0A    Vendor vendor = new Vendor();
 %0A%0A  
@@ -2887,73 +2887,8 @@
 );%0A%0A
-    // Test that the actual and expected responses are the same.%0A
     

update JsonConverterTest to match the new createFile() parameters