PollTests.java :  » Client » daca » com » google » android » daca » test » Android Open Source

Android Open Source » Client » daca 
daca » com » google » android » daca » test » PollTests.java
package com.google.android.daca.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.logging.LogManager;

import android.test.InstrumentationTestCase;
import android.util.Log;

import com.google.android.daca.service.DoodleRESTService;
import com.google.android.daca.types.CreatePollResponse;
import com.google.android.daca.types.Initiator;
import com.google.android.daca.types.Option;
import com.google.android.daca.types.Participant;
import com.google.android.daca.types.Poll;
import com.google.android.daca.types.States;

public class PollTests extends InstrumentationTestCase {
  DoodleRESTService service;
  //this is a poll with 5 options
  private static final String VALID_TEXT_POLL_ID = "zrsqwgby9mddpc8x";
  private static final String VALID_TEXT_POLL_ADMINKEY = "wx89nbi9";
  private static final String DELETED_TEXT_POLL_ID = "vmhaccq43sm6eskg";
  /**
   * All polls created by these test are stored in this list and must be deleted in {@link#tearDown()}
   * If a test creates and then deletes a poll it must not store the response in this list
   */
  private List<CreatePollResponse> createdPolls = new ArrayList<CreatePollResponse>();
  @Override
  protected void setUp() throws Exception {
    // TODO Auto-generated method stub
    super.setUp();
    service = new DoodleRESTService();
  }
  /**
   * Tests participation in TEXT poll. The name of the participant is Pavel + random int
   * The expected result is valid participant identifier
   * @throws Throwable
   */
  public void testParticipation() throws Throwable {
    Participant participant = new Participant();
    Random rnd = new Random();
    participant.setPreferences(Arrays.asList(new Integer[]{
        rnd.nextInt(1),
        rnd.nextInt(1),
        rnd.nextInt(1),
        rnd.nextInt(1),
        rnd.nextInt(1)}));
    participant.setName("Pavel" + rnd.nextInt());
    String id = service.participate(participant, VALID_TEXT_POLL_ID);
    System.out.println(id);
    assertNotNull(id);
  }

  public void testParticipationEmptyName() {
    Participant participant = new Participant();
    participant.setPreferences(Arrays.asList(new Integer[]{1,0,1,0,1}));
    participant.setName("");
    try {
      String id = service.participate(participant, VALID_TEXT_POLL_ID);
      //this line must not be reached
      fail();
    } catch (Exception ex) {
      assertEquals("Name is empty.", ex.getMessage());
    }
  }

  /**
   * Tests retrieving a valid TEXT poll from doodle site. The expected result is a Poll object with
   * 5 options
   * @throws Throwable
   */
  public void testGetValidPoll() throws Throwable {
    Poll result = service.getPollByID(VALID_TEXT_POLL_ID);
    assertTrue(result.getOptions() != null && result.getOptions().size() == 5);
    assertEquals("test 2", result.getTitle());
    assertEquals(States.OPEN, result.getState());    
  }
  
  public void testGetDeletedPoll() throws Throwable 
  {
    try 
    {
      service.getPollByID(DELETED_TEXT_POLL_ID);
      //this line must not be reached
      fail("Getting deleted poll was successful, although it shouldn't be. ERROR");
    } 
    catch (Exception ex) 
    {
      assertEquals("the poll has been deleted", ex.getMessage());
    }
  }

  public void testCreatePoll() throws Throwable {
    Poll poll = createPoll();
    CreatePollResponse response = service.createPoll(poll);
    assertNotNull(response);
    Log.d("testCreatePoll", "PollID = " + response.getId());
    assertNotNull(response.getId());
    Log.d("testCreatePoll", "AdminKey = " + response.getAdminKey());
    assertNotNull(response.getAdminKey());
    //to be deleted later
    createdPolls.add(response);
  }
  
  public void testCloseAndReopenPoll() throws Throwable {
    Poll poll = service.getPollByID(VALID_TEXT_POLL_ID);
    assertTrue(poll.getOptions() != null && poll.getOptions().size() == 5);
    assertEquals("test 2", poll.getTitle());
    poll.setState(States.CLOSED);
    service.editPoll(poll, VALID_TEXT_POLL_ID, VALID_TEXT_POLL_ADMINKEY);
    poll = service.getPollByID(VALID_TEXT_POLL_ID);
    assertEquals(States.CLOSED, poll.getState());
    poll.setState(States.OPEN);
    service.editPoll(poll, VALID_TEXT_POLL_ID, VALID_TEXT_POLL_ADMINKEY);
    poll = service.getPollByID(VALID_TEXT_POLL_ID);
    assertEquals(States.OPEN, poll.getState());
  }

//  public void testClosePollWithInvalidKey() throws Throwable {
//    Poll poll = service.getPollByID(VALID_TEXT_POLL_ID);
//    assertEquals(States.OPEN, poll.getState());  
//    poll.setState(States.CLOSED);
//    try {
//      service.editPoll(poll, VALID_TEXT_POLL_ID, INVALID_TEXT_POLL_ADMINKEY);
//      //this line must not be reached
//      fail("Closing the poll with wrong admin key is possible, although it shouldn't be");
//    } catch (Exception ex) {
//      assertEquals("the keys do not match", ex.getMessage());
//    }
//    poll = service.getPollByID(VALID_TEXT_POLL_ID);
//    assertEquals(States.OPEN, poll.getState());  
//  }
  
  public void testDeletePoll() throws Throwable {
    Poll poll = createPoll();
    CreatePollResponse response = service.createPoll(poll);
    assertNotNull(response);
    Log.d("testDeletePoll", "PollID = " + response.getId());
    assertNotNull(response.getId());
    Log.d("testDeletePoll", "AdminKey = " + response.getAdminKey());
    assertNotNull(response.getAdminKey());
    boolean deleted = service.deletePollByID(response.getId(), response.getAdminKey());
    assertTrue("Poll has been deleted.", deleted);
  }
  
  @Override
  protected void tearDown() throws Exception {
    super.tearDown();
    for (CreatePollResponse poll : createdPolls) {
      service.deletePollByID(poll.getId(), poll.getAdminKey());
    }
    LogManager.getLogManager().reset();
  }
  
  private Poll createPoll() {
    Poll poll = new Poll();
    Initiator initiator = new Initiator();
    initiator.setName("Pavel");
    poll.setInitiator(initiator);
    List<Option> options = new ArrayList<Option>();
    Option option = new Option();
    option.setValue("Levski");
    options.add(option);
    option = new Option();
    option.setValue("CSKA");
    options.add(option);
    option = new Option();
    option.setValue("Lokomotiv");
    options.add(option);
    poll.setOptions(options);
    poll.setTitle("Best team");
    poll.setDescription("Choose your favourite team");
    
    return poll;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.