package com.google.sps.workspace;

import static org.junit.Assert.assertEquals;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class WorkspaceTest {
  private final LocalServiceTestHelper helper =
      new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());

  static final String STUDENT = "STUDENT";
  static final String TA = "TA";

  @Before
  public void setUp() {
    helper.setUp();
  }

  @After
  public void tearDown() {
    helper.tearDown();
  }

  @Test
  public void WorkspaceNewEntity() throws EntityNotFoundException {
    Workspace w = new Workspace(STUDENT, TA);

    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    Entity e = ds.get(KeyFactory.stringToKey(w.getWorkspaceID()));

    assertEquals(STUDENT, e.getProperty("studentUID"));
    assertEquals(TA, e.getProperty("taUID"));
  }

  @Test(expected = NullPointerException.class)
  public void WorkspaceNullStudent() throws EntityNotFoundException {
    new Workspace(null, TA);
  }

  @Test(expected = NullPointerException.class)
  public void WorkspaceNullTA() throws EntityNotFoundException {
    new Workspace(STUDENT, null);
  }

  @Test(expected = NullPointerException.class)
  public void WorkspaceNullID() throws EntityNotFoundException {
    new Workspace(null);
  }

  @Test(expected = IllegalArgumentException.class)
  public void WorkspaceNonExistantID() throws EntityNotFoundException {
    new Workspace("NotValid");
  }

  @Test
  public void WorkspaceID() throws EntityNotFoundException {
    String id = new Workspace(STUDENT, TA).getWorkspaceID();

    Workspace w = new Workspace(id);
    assertEquals(STUDENT, w.getStudentUID());
    assertEquals(TA, w.getTaUID());
  }

  @Test
  public void getStudentID() {
    Workspace w = new Workspace(STUDENT, TA);
    assertEquals(STUDENT, w.getStudentUID());
  }

  @Test
  public void getTaID() {
    Workspace w = new Workspace(STUDENT, TA);
    assertEquals(TA, w.getTaUID());
  }

  @Test
  public void getWorkspaceID() throws EntityNotFoundException {
    Workspace w1 = new Workspace(STUDENT, TA);
    Workspace w2 = new Workspace(w1.getWorkspaceID());
    assertEquals(w1.getWorkspaceID(), w2.getWorkspaceID());
  }
}

@@ -1096,38 +1096,24 @@
 ty() throws 
-EntityNotFound
 Exception %7B%0A
@@ -1494,38 +1494,24 @@
 nt() throws 
-EntityNotFound
 Exception %7B%0A
@@ -1626,38 +1626,24 @@
 TA() throws 
-EntityNotFound
 Exception %7B%0A
@@ -1763,38 +1763,24 @@
 ID() throws 
-EntityNotFound
 Exception %7B%0A
@@ -1902,38 +1902,24 @@
 ID() throws 
-EntityNotFound
 Exception %7B%0A
@@ -1993,38 +1993,24 @@
 ID() throws 
-EntityNotFound
 Exception %7B%0A
@@ -2498,30 +2498,16 @@
  throws 
-EntityNotFound
 Exceptio

Throw generic exception in test.