<file_sep>import org.junit.Test;
import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;
import org.mockito.*;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class RentACatTest {

	/**
	 * The test fixture for this JUnit test. Test fixture: a fixed state of a set of
	 * objects used as a baseline for running tests. The test fixture is initialized
	 * using the @Before setUp method which runs before every test case. The test
	 * fixture is removed using the @After tearDown method which runs after each
	 * test case.
	 */

	RentACat r; // Object to test
	Cat c1; // First mock cat object
	Cat c2; // Second mock cat object
	Cat c3; // Third mock cat object

	@Before
	public void setUp() throws Exception {
		// Turn on automatic bug injection in the Cat class, to emulate a buggy Cat.
		// Your unit tests should work regardless of these bugs if you mock all Cats.
		Cat._bugInjectionOn = true;

		// INITIALIZE THE TEST FIXTURE
		// 1. Create a new RentACat object and assign to r
		r = RentACat.createInstance(); 

		// 2. Create a mock Cat with ID 1 and name "Jennyanydots", assign to c1
		// TODO: Fill in
		c1 = Mockito.mock(Cat.class);
		Mockito.when(c1.getId()).thenReturn(1);
		Mockito.when(c1.getName()).thenReturn("Jennyanydots");
		
		// 3. Create a mock Cat with ID 2 and name "Old Deuteronomy", assign to c2
		// TODO: Fill in
		c2 = Mockito.mock(Cat.class);
		Mockito.when(c2.getId()).thenReturn(2);
		Mockito.when(c2.getName()).thenReturn("Old Deuteronomy");
		

		// 4. Create a mock Cat with ID 3 and name "Mistoffelees", assign to c3
		// TODO: Fill in
		c3 = Mockito.mock(Cat.class);
		Mockito.when(c3.getId()).thenReturn(3);
		Mockito.when(c3.getName()).thenReturn("Mistoffelees");		
		
		// Hint: You will have to stub the mocked Cats to make them behave as if the ID
		// is 1 and name is "Jennyanydots", etc.
	}

	@After
	public void tearDown() throws Exception {
		// Not necessary strictly speaking since the references will be overwritten in
		// the next setUp call anyway and Java has automatic garbage collection.
		r = null;
		c1 = null;
		c2 = null;
		c3 = null;
	}

	/**
	 * Test case for Cat getCat(int id).
	 * Preconditions: r has no cats.
	 * Execution steps: Call getCat(2).
	 * Postconditions: Return value is null.
	 */

	@Test
	public void testGetCatNullNumCats0() {
		assertNull(r.getCat(2));
	}

	/**
	 * Test case for Cat getCat(int id).
	 * Preconditions: c1, c2, and c3 are added to r using addCat(Cat c).
	 * Execution steps: Call getCat(2).
	 * Postconditions: Return value is not null.
	 *                 Returned cat has an ID of 2.
	 */
	
	@Test
	public void testGetCatNumCats3() {
		r.addCat(c1);
		r.addCat(c2);
		r.addCat(c3);
		
		assert(r.getCat(2).getId() == 2) : "getcat shoud be 2";
	}

	/**
	 * Test case for boolean catAvailable(int id).
	 * Preconditions: r has no cats.
	 * Execution steps: Call catAvailable(2).
	 * Postconditions: Return value is false.
	 */

	@Test
	public void testCatAvailableFalseNumCats0() {
		assert(r.catAvailable(2)==false) : "No cats but catAvailable(2) returns true";
	}

	/**
	 * Test case for boolean catAvailable(int id).
	 * Preconditions: c1, c2, and c3 are added to r using addCat(Cat c).
	 *                c3 is rented.
	 *                c1 and c2 are not rented.
	 * Execution steps: Call catAvailable(2).
	 * Postconditions: Return value is true.
	 */

	@Test
	public void testCatAvailableTrueNumCats3() { 
		r.addCat(c1);
		r.addCat(c2);
		r.addCat(c3);
		r.rentCat(3);
		assert(r.catAvailable(2) == true) : "3 cats and cat 2 is not rented but catAvailable(2) returns false";
	}
 
	/**
	 * Test case for boolean catAvailable(int id).
	 * Preconditions: c1, c2, and c3 are added to r using addCat(Cat c).
	 *                c2 is rented.
	 *                c1 and c3 are not rented.
	 * Execution steps: Call catAvailable(2).
	 * Postconditions: Return value is false.
	 */
	
	@Test
	public void testCatAvailableFalseNumCats3() {
		r.addCat(c1);
		r.addCat(c2);
		r.addCat(c3);
		r.rentCat(2);;
		assert(r.catAvailable(2) == false) : "3 cats and cat 2 is rented but catAvailable(2) returns true";
	}

	/**
	 * Test case for boolean catExists(int id).
	 * Preconditions: r has no cats.
	 * Execution steps: Call catExists(2).
	 * Postconditions: Return value is false.
	 */

	@Test
	public void testCatExistsFalseNumCats0() {
		assert(r.catExists(2)==false) : "No cats but catExists(2) returns true";
	}

	/**
	 * Test case for boolean catExists(int id).
	 * Preconditions: c1, c2, and c3 are added to r using addCat(Cat c).
	 * Execution steps: Call catExists(2).
	 * Postconditions: Return value is true.
	 */
	
	@Test
	public void testCatExistsTrueNumCats3() {
		r.addCat(c1);
		r.addCat(c2);
		r.addCat(c3);
		assert(r.catExists(2)==true) : "3 cats but catExists(2) returns false";
	}

	/**
	 * Test case for String listCats().
	 * Preconditions: r has no cats.
	 * Execution steps: Call listCats().
	 * Postconditions: Return value is "".
	 */

	@Test
	public void testListCatsNumCats0() {
		assert(r.listCats().equals("")) : "No cats but listCats() returns non-empty string expected:<[]> but was:<[empty]>";
	}

	/**
	 * Test case for String listCats().
	 * Preconditions: c1, c2, and c3 are added to r using addCat(Cat c).
	 * Execution steps: Call listCats().
	 * Postconditions: Return value is "ID 1. Jennyanydots\nID 2. Old
	 *                 Deuteronomy\nID 3. Mistoffelees\n".
	 */
	
	@Test
	public void testListCatsNumCats3() {
		r.addCat(c1);
		r.addCat(c2);
		r.addCat(c3);
		String ret = "ID 1. Jennyanydots\nID 2. Old Deuteronomy\nID 3. Mistoffelees\n";
		assert(r.listCats().equals(ret)) : "3 cats and listCats() does not return the expected string expected:<ID 1. Jennyanydots[\r\n" + 
				"ID 2. Old Deuteronomy\r\n" + 
				"ID 3. Mistoffelees\r\n" + 
				"]> but was:<ID 1. Jennyanydots[ ID 2. Old Deuteronomy   ID 3. Mistoffelees      ]>";
		
	}

	/**
	 * Test case for boolean rentCat(int id).
	 * Preconditions: r has no cats.
	 * Execution steps: Call rentCat(2).
	 * Postconditions: Return value is false.
	 */

	@Test
	public void testRentCatFailureNumCats0() {
		assert(r.rentCat(2) == false) : "No cats but rentCat(2) returns true";
	}

	/** 
	 * Test case for boolean rentCat(int id).
	 * Preconditions: c1, c2, and c3 are added to r using addCat(Cat c).
	 *                c2 is rented.
	 * Execution steps: Call rentCat(2).
	 * Postconditions: Return value is false.
	 *                 c1.rentCat(), c2.rentCat(), c3.rentCat() are never called.
	 *                 
	 * Hint: See Example/NoogieTest.java in testBadgerPlayCalled method to see an
	 * example of behavior verification.
	 */
	
	@Test
	public void testRentCatFailureNumCats3() {
		r.addCat(c1);
		r.addCat(c2);
		r.addCat(c3);
		r.rentCat(2);
		
		
		assert(r.rentCat(2)==false) : "3 cats and cat 2 is rented but rentCat(2) returns true";
		Mockito.verify(c1,Mockito.times(0)).rentCat();
		Mockito.verify(c2,Mockito.times(0)).rentCat();
		Mockito.verify(c3,Mockito.times(0)).rentCat();
	}

	/**
	 * Test case for boolean returnCat(int id).
	 * Preconditions: r has no cats.
	 * Execution steps: Call returnCat(2).
	 * Postconditions: Return value is false.
	 */

	@Test
	public void testReturnCatFailureNumCats0() { 
		assert(r.returnCat(2)==false) : "No cats but returnCat(2) returns true";
	}

	/**
	 * Test case for boolean returnCat(int id).
	 * Preconditions: c1, c2, and c3 are added to r using addCat(Cat c).
	 *                c2 is rented.
	 * Execution steps: Call returnCat(2).
	 * Postconditions: Return value is true.
	 *                 c2.returnCat() is called exactly once.
	 *                 c1.returnCat() and c3.returnCat are never called.
	 *                 
	 * Hint: See Example/NoogieTest.java in testBadgerPlayCalled method to see an
	 * example of behavior verification.
	 */
	
	@Test
	public void testReturnCatNumCats3() {
		r.addCat(c1);
		r.addCat(c2);
		r.addCat(c3);
		c2.rentCat();;
		
		
		assert(r.returnCat(2)==true);
		Mockito.verify(c1,Mockito.times(0)).returnCat();
		Mockito.verify(c2,Mockito.times(1)).returnCat();
		Mockito.verify(c3,Mockito.times(0)).returnCat();
	}
}
<file_sep>// Generated by Selenium IDE
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsNot.not;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Alert;
import org.openqa.selenium.Keys;
import java.util.*;
import java.net.MalformedURLException;
import java.net.URL;
public class RedditCatsTest {
  private WebDriver driver;
  private Map<String, Object> vars;
  JavascriptExecutor js;
  @Before
  public void setUp() {
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--headless");
    options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
    driver = new ChromeDriver(options);
    js = (JavascriptExecutor) driver;
    vars = new HashMap<String, Object>();
  }
  @After
  public void tearDown() {
    driver.quit();
  }
  @Test
  public void fUNTITLE() {
    driver.get("https://www.reddit.com/r/cats/");
    driver.manage().window().setSize(new Dimension(966, 990));
    driver.findElement(By.xpath("//div[@id=\'SHORTCUT_FOCUSABLE_DIV\']/div[2]/div/div/div/div[2]/div/div/div/div/div/h1")).click();
    assertThat(driver.getTitle(), is("Cats"));
  }
  @Test
  public void fUNJOINBUTTONEXISTS() {
    driver.get("https://www.reddit.com/r/cats/");
    driver.manage().window().setSize(new Dimension(969, 990));
    driver.findElement(By.cssSelector(".\\_3I4Wpl_rl6oTm02aWPZayD")).click();
    assertThat(driver.findElement(By.xpath("//div[@id=\'SHORTCUT_FOCUSABLE_DIV\']/div[2]/div/div/div/div[2]/div/div/div/div/div[2]/button")).getText(), is("JOIN"));
  }
  @Test
  public void fUNRULE3() {
    driver.get("https://www.reddit.com/r/cats/");
    driver.manage().window().setSize(new Dimension(977, 990));
    driver.findElement(By.cssSelector(".\\_3I4Wpl_rl6oTm02aWPZayD")).click();
    //  The rules box is located in the bottom right corner
    assertThat(driver.findElement(By.xpath("//div[@id=\'SHORTCUT_FOCUSABLE_DIV\']/div[2]/div/div/div/div[2]/div[3]/div[2]/div/div[5]/div/div[2]/div[3]/div/div[2]/div")).getText(), is("No NSFW, animal abuse, or cruelty"));
  }
  @Test
  public void fUNRULES11ITEMS() {
    driver.get("https://www.reddit.com/r/cats/");
    driver.manage().window().setSize(new Dimension(981, 990));
    driver.findElement(By.cssSelector(".\\_3I4Wpl_rl6oTm02aWPZayD")).click();
    {
      WebElement element = driver.findElement(By.cssSelector(".\\_2RkQc9Gtsq3cPQNZLYv4zc"));
      Actions builder = new Actions(driver);
      builder.moveToElement(element).perform();
    }
    // 11th element present
    {
      List<WebElement> elements = driver.findElements(By.xpath("//div[@id=\'SHORTCUT_FOCUSABLE_DIV\']/div[2]/div/div/div/div[2]/div[3]/div[2]/div/div[5]/div/div[2]/div[11]/div/div/div"));
      assert(elements.size() > 0);
    }
    // 12th element not present
    {
      List<WebElement> elements = driver.findElements(By.xpath("//div[@id=\'SHORTCUT_FOCUSABLE_DIV\']/div[2]/div/div/div/div[2]/div[3]/div[2]/div/div[5]/div/div[2]/div[12]/div/div/div"));
      assert(elements.size() == 0);
    }
  }
  @Test
  public void fUNSIGNUPLINK() {
    driver.get("https://www.reddit.com/r/cats/");
    driver.manage().window().setSize(new Dimension(972, 990));
    driver.findElement(By.cssSelector(".\\_3I4Wpl_rl6oTm02aWPZayD")).click();
    // variable for sign up button
    {
      WebElement element = driver.findElement(By.xpath("//a[contains(@href, \'https://www.reddit.com/register/?dest=https%3A%2F%2Fwww.reddit.com%2Fr%2Fcats%2F\')]"));
      String attribute = element.getAttribute("href");
      vars.put("signUp", attribute);
    }
    // link should be the same
    assertEquals(vars.get("signUp").toString(), "https://www.reddit.com/register/?dest=https%3A%2F%2Fwww.reddit.com%2Fr%2Fcats%2F");
  }

  @Test
  public void fUNSEARCHSMELLYCAT() {
    driver.get("https://www.reddit.com/r/cats/");
    driver.manage().window().setSize(new Dimension(974, 990));
    driver.findElement(By.cssSelector(".\\_3I4Wpl_rl6oTm02aWPZayD")).click();
    {
      WebElement element = driver.findElement(By.cssSelector(".\\_2RkQc9Gtsq3cPQNZLYv4zc"));
      Actions builder = new Actions(driver);
      builder.moveToElement(element).perform();
    }
    driver.findElement(By.id("header-search-bar")).click();
    driver.findElement(By.id("header-search-bar")).sendKeys("smelly cat");
    driver.findElement(By.id("header-search-bar")).sendKeys(Keys.ENTER);
    // list of results will display the search term "smelly cat" at the top above "Search results"
    assertThat(driver.findElement(By.cssSelector(".\\_3j9XjJayuKq7dJ8huVnCuS")).getText(), is("smelly cat"));
  }
  
}
