Example usage for org.springframework.mock.web MockHttpServletRequest addHeader

List of usage examples for org.springframework.mock.web MockHttpServletRequest addHeader

Introduction

In this page you can find the example usage for org.springframework.mock.web MockHttpServletRequest addHeader.

Prototype

public void addHeader(String name, Object value) 

Source Link

Document

Add an HTTP header entry for the given name.

Usage

From source file:org.opennms.web.rest.UserRestServiceTest.java

@Test
public void testUserJson() throws Exception {
    String url = "/users";
    createUser("foobar", "foobar@opennms.org");

    // GET all users
    MockHttpServletRequest jsonRequest = createRequest(getServletContext(), GET, url);
    jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
    String json = sendRequest(jsonRequest, 200);

    JSONObject restObject = new JSONObject(json);
    JSONObject expectedObject = new JSONObject(
            IOUtils.toString(new FileInputStream("src/test/resources/v1/users.json")));
    JSONAssert.assertEquals(expectedObject, restObject, true);
}

From source file:org.opennms.web.rest.v1.AcknowledgmentRestServiceIT.java

@Test
@JUnitTemporaryDatabase//w  w w .java 2 s .  co m
public void testGetAcksJson() throws Exception {
    String url = "/acks";

    // GET all items
    MockHttpServletRequest jsonRequest = createRequest(m_servletContext, GET, url);
    jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
    String json = sendRequest(jsonRequest, 200);

    JSONObject restObject = new JSONObject(json);
    JSONObject expectedObject = new JSONObject(
            IOUtils.toString(new FileInputStream("src/test/resources/v1/acks.json")));
    JSONAssert.assertEquals(expectedObject, restObject, true);
}

From source file:org.opennms.web.rest.v1.AlarmRestServiceIT.java

@Test
@JUnitTemporaryDatabase//from   ww  w. j a  v a2 s. co  m
public void testAlarmsJson() throws Exception {
    String url = "/alarms";

    // GET all items
    MockHttpServletRequest jsonRequest = createRequest(m_servletContext, GET, url, "admin",
            Arrays.asList(new String[] { Authentication.ROLE_ADMIN }));
    jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
    String json = sendRequest(jsonRequest, 200);

    JSONObject restObject = new JSONObject(json);
    JSONObject expectedObject = new JSONObject(
            IOUtils.toString(new FileInputStream("src/test/resources/v1/alarms.json")));
    JSONAssert.assertEquals(expectedObject, restObject, true);
}

From source file:org.opennms.web.rest.v1.AlarmStatsRestServiceIT.java

/**
 * TODO: Doesn't test firstAutomationTime, lastAutomationTime, reductionKey,
 * reductionKeyMemo, suppressedTime, suppressedUntil, clearKey, or stickyMemo
 * fields./* w  ww . j a  va2 s  .com*/
 */
@Test
@JUnitTemporaryDatabase
public void testGetAlarmStatsJson() throws Exception {
    createAlarm(OnmsSeverity.CLEARED, "admin");
    createAlarm(OnmsSeverity.MAJOR, "admin");
    createAlarm(OnmsSeverity.CRITICAL, "admin");
    createAlarm(OnmsSeverity.CRITICAL, null);
    createAlarm(OnmsSeverity.MINOR, null);
    createAlarm(OnmsSeverity.NORMAL, null);

    // GET all users
    MockHttpServletRequest jsonRequest = createRequest(m_servletContext, GET, "/stats/alarms");
    jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
    String json = sendRequest(jsonRequest, 200);

    JSONObject restObject = new JSONObject(json);
    JSONObject expectedObject = new JSONObject(
            IOUtils.toString(new FileInputStream("src/test/resources/v1/stats_alarms.json")));
    JSONAssert.assertEquals(expectedObject, restObject, true);
}

From source file:org.opennms.web.rest.v1.AvailabilityRestServiceIT.java

@Test
@JUnitTemporaryDatabase//  w  w  w  .jav a 2  s.c o  m
public void testGetAvailabilityJson() throws Exception {
    String url = "/availability";

    // GET all items
    MockHttpServletRequest jsonRequest = createRequest(m_servletContext, GET, url);
    jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
    String json = sendRequest(jsonRequest, 200);

    // TODO: The comment and last-updated fields are blank in the objects that are
    // fetched. Figure out how to get them to populate so that we can test serialization
    // of those values.
    //
    JSONObject restObject = new JSONObject(json);
    JSONObject expectedObject = new JSONObject(
            IOUtils.toString(new FileInputStream("src/test/resources/v1/availability.json")));
    JSONAssert.assertEquals(expectedObject, restObject, true);

    // GET node item
    jsonRequest = createRequest(m_servletContext, GET, url + "/nodes/" + m_populator.getNode1().getId());
    jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
    json = sendRequest(jsonRequest, 200);

    restObject = new JSONObject(json);
    expectedObject = new JSONObject(
            IOUtils.toString(new FileInputStream("src/test/resources/v1/availability_node.json")));
    JSONAssert.assertEquals(expectedObject, restObject, true);
}

From source file:org.opennms.web.rest.v1.CategoryRestServiceIT.java

@Test
@JUnitTemporaryDatabase/*  w  w  w .j a  va2s.  com*/
public void testCategoriesJson() throws Exception {
    String url = "/categories";

    // GET all users
    MockHttpServletRequest jsonRequest = createRequest(m_servletContext, GET, url);
    jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
    String json = sendRequest(jsonRequest, 200);

    JSONObject restObject = new JSONObject(json);
    JSONObject expectedObject = new JSONObject(
            IOUtils.toString(new FileInputStream("src/test/resources/v1/categories.json")));
    JSONAssert.assertEquals(expectedObject, restObject, true);
}

From source file:org.opennms.web.rest.v1.GroupRestServiceIT.java

@Test
public void testGroupsJson() throws Exception {
    String url = "/groups";

    // GET all groups
    MockHttpServletRequest jsonRequest = createRequest(m_servletContext, GET, url);
    jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
    String json = sendRequest(jsonRequest, 200);
    JSONObject restObject = new JSONObject(json);
    JSONObject expectedObject = new JSONObject(
            IOUtils.toString(new FileInputStream("src/test/resources/v1/groups.json")));
    JSONAssert.assertNotEquals(expectedObject, restObject, true);

    // Update the comment value
    sendPut("/groups/Admin", "comments=The administrators", 204);

    // Now they should be equal
    jsonRequest = createRequest(m_servletContext, GET, url);
    jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
    json = sendRequest(jsonRequest, 200);
    restObject = new JSONObject(json);
    expectedObject = new JSONObject(IOUtils.toString(new FileInputStream("src/test/resources/v1/groups.json")));
    JSONAssert.assertEquals(expectedObject, restObject, true);
}

From source file:org.opennms.web.rest.v1.IfServicesRestServiceIT.java

@Test
@JUnitTemporaryDatabase//from  w  w  w  .j  av a2  s.  c om
public void testGetServicesJson() throws Exception {
    String url = "/ifservices";

    // GET all users
    MockHttpServletRequest jsonRequest = createRequest(m_servletContext, GET, url);
    jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
    jsonRequest.setQueryString("orderBy=id");
    String json = sendRequest(jsonRequest, 200);

    JSONObject restObject = new JSONObject(json);
    JSONObject expectedObject = new JSONObject(
            IOUtils.toString(new FileInputStream("src/test/resources/v1/ifservices.json")));
    JSONAssert.assertEquals(expectedObject, restObject, true);
}

From source file:org.opennms.web.rest.v1.KscRestServiceIT.java

@Test
public void testKscJson() throws Exception {
    String url = "/ksc/0";

    // GET all users
    MockHttpServletRequest jsonRequest = createRequest(m_servletContext, GET, url);
    jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
    String json = sendRequest(jsonRequest, 200);

    JSONObject restObject = new JSONObject(json);
    JSONObject expectedObject = new JSONObject(
            IOUtils.toString(new FileInputStream("src/test/resources/v1/ksc.json")));
    JSONAssert.assertEquals(expectedObject, restObject, true);
}

From source file:org.opennms.web.rest.v1.NodeRestServiceIT.java

@Test
@JUnitTemporaryDatabase//w w w  .  j a  v  a2  s.  com
public void testNodeJson() throws Exception {
    createSnmpInterface();

    final MockHttpServletRequest req = createRequest(m_context, GET, "/nodes");
    req.addHeader("Accept", MediaType.APPLICATION_JSON);
    req.addParameter("limit", "0");
    String json = sendRequest(req, 200);
    JSONObject jo = new JSONObject(json);
    final JSONArray ja = jo.getJSONArray("node");
    assertEquals(1, ja.length());
    jo = ja.getJSONObject(0);
    assertEquals("A", jo.getString("type"));
    assertEquals("TestMachine0", jo.getString("label"));
}