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.AvailabilityRestServiceTest.java

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

    // GET all items
    MockHttpServletRequest jsonRequest = createRequest(getServletContext(), 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(getServletContext(), 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.CategoryRestServiceTest.java

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

    // 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/categories.json")));
    JSONAssert.assertEquals(expectedObject, restObject, true);
}

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

@Test
@JUnitTemporaryDatabase/*from  w  ww  .j a  v  a2  s.  com*/
public void testGetServicesJson() throws Exception {
    String url = "/ifservices";

    // GET all users
    MockHttpServletRequest jsonRequest = createRequest(getServletContext(), 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.NodeRestServiceTest.java

@Test
@JUnitTemporaryDatabase/*from w ww  . j  a v  a  2  s  .  c  om*/
public void testNodeJson() throws Exception {
    createSnmpInterface();

    final MockHttpServletRequest req = createRequest(getServletContext(), GET, "/nodes");
    req.addHeader("Accept", "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"));
}

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

@Test
@JUnitTemporaryDatabase/*from   w ww.  j  a v a 2  s  .c  om*/
public void testAnotherNodeJson() throws Exception {
    createSnmpInterface();

    final MockHttpServletRequest req = createRequest(getServletContext(), GET, "/nodes");
    req.addHeader("Accept", MediaType.APPLICATION_JSON);
    req.addParameter("limit", "0");
    String json = sendRequest(req, 200);

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

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

@Test
@JUnitTemporaryDatabase/*from   w  ww .  ja  v  a 2 s . c o m*/
public void testIpInterfaceJson() throws Exception {
    createIpInterface();
    String url = "/nodes/1/ipinterfaces";

    final MockHttpServletRequest req = createRequest(getServletContext(), GET, url);
    req.addHeader("Accept", "application/json");
    req.addParameter("limit", "0");
    final String json = sendRequest(req, 200);
    assertNotNull(json);
    assertFalse(json.contains("The Owner"));
    JSONObject jo = new JSONObject(json);
    JSONArray ja = jo.getJSONArray("ipInterface");
    assertEquals(1, ja.length());
    jo = ja.getJSONObject(0);
    assertTrue(jo.isNull("ifIndex"));
    assertEquals("10.10.10.10", jo.getString("ipAddress"));
    assertEquals("1", jo.getString("nodeId"));
}

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

@Test
@JUnitTemporaryDatabase/* w ww  .  ja  va  2  s .co  m*/
public void testSnmpInterfaceJson() throws Exception {
    createSnmpInterface();
    String url = "/nodes/1/snmpinterfaces";

    final MockHttpServletRequest req = createRequest(getServletContext(), GET, url);
    req.addHeader("Accept", "application/json");
    req.addParameter("limit", "0");
    final String json = sendRequest(req, 200);
    assertNotNull(json);
    assertFalse(json.contains("The Owner"));

    JSONObject jo = new JSONObject(json);
    final JSONArray ja = jo.getJSONArray("snmpInterface");
    assertEquals(1, ja.length());
    jo = ja.getJSONObject(0);
    assertEquals(6, jo.getInt("ifIndex"));
    assertEquals(1, jo.getInt("ifOperStatus"));
    assertEquals("en1", jo.getString("ifDescr"));
}

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

@Test
@JUnitTemporaryDatabase/*from w  w  w  . j a  v  a  2 s.c o  m*/
public void testNotificationsJson() throws Exception {
    String url = "/notifications";

    // 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/notifications.json")));
    JSONAssert.assertEquals(expectedObject, restObject, true);
}

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

@Test
public void testGetLocationsJson() throws Exception {
    String url = "/remotelocations";

    // 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/remotelocations.json")));
    JSONAssert.assertEquals(expectedObject, restObject, true);
}

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

@Test
@JUnitTemporaryDatabase//from  w ww .j  a  v  a  2s  .c  o m
public void testResourcesJson() throws Exception {
    String url = "/resources";

    // 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/resources.json")));
    JSONAssert.assertEquals(expectedObject, restObject, true);
}