Example usage for junit.framework Assert assertEquals

List of usage examples for junit.framework Assert assertEquals

Introduction

In this page you can find the example usage for junit.framework Assert assertEquals.

Prototype

static public void assertEquals(int expected, int actual) 

Source Link

Document

Asserts that two ints are equal.

Usage

From source file:fr.msch.wissl.server.TestUsers.java

@Test
public void test() throws IOException, JSONException {
    HttpClient client = new HttpClient();

    // check the users and sessions created by TServer
    GetMethod get = new GetMethod(TServer.URL + "users");
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);//from   w ww  . ja  va2  s.co  m
    Assert.assertEquals(200, get.getStatusCode());
    JSONObject obj = new JSONObject(get.getResponseBodyAsString());
    JSONArray users = obj.getJSONArray("users");
    Assert.assertEquals(2, users.length());
    for (int i = 0; i < 2; i++) {
        JSONObject u = users.getJSONObject(i);
        String username = u.getString("username");
        if (username.equals(this.user_username)) {
            Assert.assertEquals(this.user_userId, u.getInt("id"));
            Assert.assertEquals(2, u.getInt("auth"));
            Assert.assertEquals(0, u.getInt("downloaded"));
        } else if (username.equals(this.admin_username)) {
            Assert.assertEquals(this.admin_userId, u.getInt("id"));
            Assert.assertEquals(1, u.getInt("auth"));
            Assert.assertEquals(0, u.getInt("downloaded"));
        } else {
            Assert.fail("Unexpected user:" + username);
        }
    }

    JSONArray sessions = obj.getJSONArray("sessions");
    Assert.assertEquals(2, sessions.length());
    for (int i = 0; i < 2; i++) {
        JSONObject s = sessions.getJSONObject(i);
        String username = s.getString("username");
        if (username.equals(this.user_username)) {
            Assert.assertEquals(this.user_userId, s.getInt("user_id"));
            Assert.assertTrue(s.getInt("last_activity") >= 0); // might be 0
            Assert.assertTrue(s.getInt("created_at") >= 0);
            Assert.assertFalse(s.has("origins")); // admin only
            Assert.assertFalse(s.has("last_played_song"));
        }
    }

    // unknown user: 404
    get = new GetMethod(TServer.URL + "user/100");
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    Assert.assertEquals(404, get.getStatusCode());

    // check admin user
    get = new GetMethod(TServer.URL + "user/" + this.admin_userId);
    get.addRequestHeader("sessionId", this.admin_sessionId);
    client.executeMethod(get);
    Assert.assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    JSONObject u = obj.getJSONObject("user");
    Assert.assertEquals(this.admin_userId, u.getInt("id"));
    Assert.assertEquals(this.admin_username, u.getString("username"));
    Assert.assertEquals(1, u.getInt("auth"));
    Assert.assertEquals(0, u.getInt("downloaded"));
    sessions = obj.getJSONArray("sessions");
    Assert.assertEquals(1, sessions.length());
    JSONObject s = sessions.getJSONObject(0);

    Assert.assertEquals(this.admin_userId, s.getInt("user_id"));
    Assert.assertTrue(s.getInt("last_activity") >= 0); // might be 0
    Assert.assertTrue(s.getInt("created_at") > 0);
    Assert.assertTrue(s.has("origins")); // admin only
    Assert.assertFalse(s.has("last_played_song"));
    Assert.assertTrue(obj.getJSONArray("playlists").length() == 0);

    // create a new user with user account: error 401
    PostMethod post = new PostMethod(TServer.URL + "user/add");
    post.addRequestHeader("sessionId", this.user_sessionId);
    post.addParameter("username", "new-user");
    post.addParameter("password", "new-pw");
    client.executeMethod(post);
    Assert.assertEquals(401, post.getStatusCode());

    // create new user with empty username: err 400
    post = new PostMethod(TServer.URL + "user/add");
    post.addRequestHeader("sessionId", this.admin_sessionId);
    post.addParameter("username", "");
    post.addParameter("password", "new-pw");
    post.addParameter("auth", "1");
    client.executeMethod(post);
    Assert.assertEquals(400, post.getStatusCode());

    // create new user with short password: err 400
    post = new PostMethod(TServer.URL + "user/add");
    post.addRequestHeader("sessionId", this.admin_sessionId);
    post.addParameter("username", "new-user");
    post.addParameter("password", "pw");
    post.addParameter("auth", "1");
    client.executeMethod(post);
    Assert.assertEquals(400, post.getStatusCode());

    // create new user with invalid auth: err 400
    post = new PostMethod(TServer.URL + "user/add");
    post.addRequestHeader("sessionId", this.admin_sessionId);
    post.addParameter("username", "new-user");
    post.addParameter("password", "pw");
    post.addParameter("auth", "3");
    client.executeMethod(post);
    Assert.assertEquals(400, post.getStatusCode());

    int new_userId = -1;
    String new_sessionId = null;
    String new_username = "new-user";
    String new_password = "new-pw";

    // create new user
    post = new PostMethod(TServer.URL + "user/add");
    post.addRequestHeader("sessionId", this.admin_sessionId);
    post.addParameter("username", new_username);
    post.addParameter("password", new_password);
    post.addParameter("auth", "2");
    client.executeMethod(post);
    Assert.assertEquals(200, post.getStatusCode());
    u = new JSONObject(post.getResponseBodyAsString()).getJSONObject("user");
    new_userId = u.getInt("id");
    Assert.assertEquals(new_username, u.getString("username"));
    Assert.assertEquals(2, u.getInt("auth"));

    // check new user 
    get = new GetMethod(TServer.URL + "user/" + new_userId);
    get.addRequestHeader("sessionId", this.user_sessionId);
    client.executeMethod(get);
    Assert.assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    Assert.assertTrue(obj.getJSONArray("playlists").length() == 0);
    Assert.assertEquals(0, obj.getJSONArray("sessions").length());

    // login new user
    obj = new JSONObject(this.login(new_username, new_password));
    Assert.assertEquals(new_userId, obj.getInt("userId"));
    new_sessionId = obj.getString("sessionId");

    // check if logged in with /users
    get = new GetMethod(URL + "users");
    get.addRequestHeader("sessionId", new_sessionId);
    client.executeMethod(get);
    Assert.assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    users = obj.getJSONArray("users");
    Assert.assertEquals(3, users.length());
    for (int i = 0; i < 3; i++) {
        u = users.getJSONObject(i);
        String username = u.getString("username");
        if (username.equals(new_username)) {
            Assert.assertEquals(new_userId, u.getInt("id"));
        } else {
            Assert.assertTrue(username.equals(user_username) || username.equals(admin_username));
        }
    }
    sessions = obj.getJSONArray("sessions");
    Assert.assertEquals(3, sessions.length());
    for (int i = 0; i < 3; i++) {
        s = sessions.getJSONObject(i);
        String username = s.getString("username");
        if (username.equals(new_username)) {
            Assert.assertEquals(new_userId, s.getInt("user_id"));
            Assert.assertTrue(s.getInt("last_activity") >= 0); // might be 0
            Assert.assertTrue(s.getInt("created_at") > 0);
            Assert.assertFalse(s.has("origins")); // admin only
            Assert.assertFalse(s.has("last_played_song"));
        } else {
            Assert.assertTrue(username.equals(user_username) || username.equals(admin_username));
        }
    }

    // try to change password without the old password: 401
    post = new PostMethod(URL + "user/password");
    post.addRequestHeader("sessionId", new_sessionId);
    post.addParameter("old_password", "password");
    post.addParameter("new_password", "password2");
    client.executeMethod(post);
    Assert.assertEquals(401, post.getStatusCode());

    // change password for new user
    post = new PostMethod(URL + "user/password");
    post.addRequestHeader("sessionId", new_sessionId);
    post.addParameter("old_password", new_password);
    new_password = "something else";
    post.addParameter("new_password", new_password);
    client.executeMethod(post);
    Assert.assertEquals(204, post.getStatusCode());

    // logout and relogin with new password
    post = new PostMethod(URL + "logout");
    post.addRequestHeader("sessionId", new_sessionId);
    client.executeMethod(post);
    Assert.assertEquals(204, post.getStatusCode());

    obj = new JSONObject(this.login(new_username, new_password));
    Assert.assertEquals(new_userId, obj.getInt("userId"));
    new_sessionId = obj.getString("sessionId");

    // try to delete an user as user: 401
    post = new PostMethod(URL + "user/remove");
    post.addRequestHeader("sessionId", user_sessionId);
    post.addParameter("user_ids[]", "" + new_userId);
    client.executeMethod(post);
    Assert.assertEquals(401, post.getStatusCode());

    // try to delete no user: 400
    post = new PostMethod(URL + "user/remove");
    post.addRequestHeader("sessionId", admin_sessionId);
    client.executeMethod(post);
    Assert.assertEquals(400, post.getStatusCode());

    // try to delete admin with admin: 400
    post = new PostMethod(URL + "user/remove");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("user_ids[]", "" + admin_userId);
    client.executeMethod(post);
    Assert.assertEquals(400, post.getStatusCode());

    // delete both regular users
    post = new PostMethod(URL + "user/remove");
    post.addRequestHeader("sessionId", admin_sessionId);
    post.addParameter("user_ids[]", "" + user_userId);
    post.addParameter("user_ids[]", "" + new_userId);
    client.executeMethod(post);
    Assert.assertEquals(204, post.getStatusCode());

    // check that old sessions do not work anymore
    get = new GetMethod(URL + "users");
    get.addRequestHeader("sessionId", user_sessionId);
    client.executeMethod(get);
    Assert.assertEquals(401, get.getStatusCode());

    // check that there is only 1 user
    get = new GetMethod(URL + "users");
    get.addRequestHeader("sessionId", admin_sessionId);
    client.executeMethod(get);
    Assert.assertEquals(200, get.getStatusCode());
    obj = new JSONObject(get.getResponseBodyAsString());
    Assert.assertEquals(obj.getJSONArray("users").length(), 1);
    Assert.assertEquals(obj.getJSONArray("sessions").length(), 1);

}

From source file:org.jasig.ssp.util.importer.job.twodottwo.StageSuccessTest.java

@Test
public void testStageSuccess() throws Exception {

    //Test file should have 1 step which should write successfully
    JobExecution jobExecution = jobLauncherTestUtils.launchJob();
    Collection<StepExecution> stepExecutions = jobExecution.getStepExecutions();
    for (StepExecution stepExecution : stepExecutions) {
        Assert.assertEquals(stepExecution.getWriteCount(), 1);
    }/*  w  w w .java  2 s. co m*/
    BatchStatus exitStatus = jobExecution.getStatus();

    Assert.assertEquals(BatchStatus.COMPLETED, exitStatus);

}

From source file:org.ocpsoft.rewrite.servlet.config.ParameterConfigurationTest.java

@Test
public void testTestPathParameterNotMatchingRegexes() throws Exception {
    HttpAction<HttpGet> action = get("/lincoln3/order/z42");
    Assert.assertEquals(404, action.getResponse().getStatusLine().getStatusCode());
}

From source file:com.acc.facades.suggestion.DefaultSimpleSuggestionFacadeIntegrationTest.java

@Test
public void testReferencesForPurchasedInCategory() {
    final UserModel user = userService.getUserForUID("deJol");
    userService.setCurrentUser(user);/*from  w w w  . j a v  a  2  s .  c o  m*/
    List<ProductData> result = simpleSuggestionFacade.getReferencesForPurchasedInCategory("cameras",
            Collections.EMPTY_LIST, false, null);
    Assert.assertEquals(4, result.size());
    result = simpleSuggestionFacade.getReferencesForPurchasedInCategory("cameras", Collections.EMPTY_LIST,
            false, NumberUtils.INTEGER_ONE);
    Assert.assertEquals(1, result.size());
    result = simpleSuggestionFacade.getReferencesForPurchasedInCategory("cameras",
            Arrays.asList(ProductReferenceTypeEnum.SIMILAR), false, null);
    Assert.assertEquals(1, result.size());
    result = simpleSuggestionFacade.getReferencesForPurchasedInCategory("cameras",
            Arrays.asList(ProductReferenceTypeEnum.ACCESSORIES), false, null);
    Assert.assertEquals(2, result.size());
    result = simpleSuggestionFacade.getReferencesForPurchasedInCategory("cameras",
            Arrays.asList(ProductReferenceTypeEnum.ACCESSORIES), true, null);
    Assert.assertEquals(1, result.size());
    final ProductData product = result.get(0);
    Assert.assertEquals("adapterDC", product.getCode());
    Assert.assertEquals("adapter", product.getName());
}

From source file:org.openxdata.server.service.impl.MobileMenuTextServiceTest.java

@Test
public void saveMobileMenuText_shouldSaveMobileMenuTextList() throws Exception {
    final String textName = "Exit";

    List<MobileMenuText> textList = utilityService.getMobileMenuText("en");
    Assert.assertEquals(1, textList.size());
    Assert.assertNull(getMobileMenuText(textName, textList));

    MobileMenuText mobileMenuText = new MobileMenuText();
    mobileMenuText.setMenuText(textName);
    mobileMenuText.setLocaleKey("en");
    mobileMenuText.setMenuId((short) 2);
    mobileMenuText.setCreator(userService.getUsers().get(0));
    mobileMenuText.setDateCreated(new Date());

    textList = new ArrayList<MobileMenuText>();
    textList.add(mobileMenuText);// ww  w .  j  a  v  a 2s. c  om

    utilityService.saveMobileMenuText(textList);

    textList = utilityService.getMobileMenuText("en");
    Assert.assertEquals(2, textList.size());
    Assert.assertNotNull(getMobileMenuText(textName, textList));
}

From source file:org.openmrs.module.webservices.rest.web.v1_0.controller.PersonNameControllerTest.java

@Test
public void shouldGetAPersonName() throws Exception {
    Object result = controller.retrieve(personUuid, nameUuid, request);
    Assert.assertNotNull(result);//from   w ww  .  j a v a 2 s  .  c  o  m
    Assert.assertEquals("Super", PropertyUtils.getProperty(result, "givenName"));
    Assert.assertEquals("User", PropertyUtils.getProperty(result, "familyName"));
    Assert.assertNull(PropertyUtils.getProperty(result, "auditInfo"));
    Assert.assertNotNull(PropertyUtils.getProperty(result, "uuid"));
}

From source file:com.mirth.connect.model.converters.tests.NCPDPSerializerTest.java

@Test
public void test51ResponseToXml() throws Exception {
    String input = FileUtils.readFileToString(new File("tests/test-ncpdp-51-response-input.txt"));
    String output = FileUtils.readFileToString(new File("tests/test-ncpdp-51-response-output.xml"));
    NCPDPSerializer serializer = new NCPDPSerializer(defaultProperties);
    Assert.assertEquals(output, TestUtil.prettyPrintXml(serializer.toXML(input)));
}

From source file:org.ocpsoft.rewrite.servlet.config.HttpConfigurationOrder2Test.java

@Test
public void testOrder2() throws Exception {
    HttpAction<HttpGet> action = get("/login");
    Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
    Assert.assertEquals("test page", action.getResponseContent());
}

From source file:org.atemsource.atem.impl.pojo.PrimitiveCollectionAssociationAttributeTest.java

@Test
public void testTargetType() {
    EntityType entityType = entityTypeRepository.getEntityType(EntityA.class);
    CollectionAttribute attribute = (CollectionAttribute) entityType.getAttribute("stringList");
    Assert.assertNotNull(attribute);/*from w  w w  .ja  v a2  s. com*/
    Assert.assertEquals(String.class, attribute.getTargetType().getJavaType());
}

From source file:org.openqa.selendroid.server.HandlerRegisteredTest.java

@Test
public void postClickHandlerRegistered() throws Exception {
    String url = "http://localhost:" + port + "/wd/hub/session/12345/element/815/click";
    HttpResponse response = executeRequest(url, HttpMethod.POST);
    SelendroidAssert.assertResponseIsOk(response);
    Assert.assertEquals("{\"status\":0,\"value\":\"sessionId#12345 elementId#815\"}",
            IOUtils.toString(response.getEntity().getContent()));
}