Search in sources :

Example 1 with TestServletInputStream

use of fixtures.TestServletInputStream in project cwms-radar-api by USACE.

the class CatalogControllerTest method bad_formats_return_501.

// get all the infrastructure in place first.
@Disabled
@ParameterizedTest
@ValueSource(strings = { "blurge,", "appliation/json+fred" })
public void bad_formats_return_501(String format) throws Exception {
    final String testBody = "test";
    CatalogController controller = spy(new CatalogController(new MetricRegistry()));
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    HashMap<String, Object> attributes = new HashMap<>();
    attributes.put(ContextUtil.maxRequestSizeKey, Integer.MAX_VALUE);
    when(request.getInputStream()).thenReturn(new TestServletInputStream(testBody));
    when(request.getAttribute("database")).thenReturn(this.conn);
    when(request.getRequestURI()).thenReturn("/catalog/TIMESERIES");
    Context context = ContextUtil.init(request, response, "*", new HashMap<String, String>(), HandlerType.GET, attributes);
    context.attribute("database", this.conn);
    assertNotNull(context.attribute("database"), "could not get the connection back as an attribute");
    when(request.getHeader(Header.ACCEPT)).thenReturn("BAD FORMAT");
    assertThrows(FormattingException.class, () -> {
        controller.getAll(context);
    });
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Context(io.javalin.http.Context) HashMap(java.util.HashMap) MetricRegistry(com.codahale.metrics.MetricRegistry) HttpServletResponse(javax.servlet.http.HttpServletResponse) TestHttpServletResponse(fixtures.TestHttpServletResponse) TestServletInputStream(fixtures.TestServletInputStream) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Disabled(org.junit.jupiter.api.Disabled)

Example 2 with TestServletInputStream

use of fixtures.TestServletInputStream in project cwms-radar-api by USACE.

the class CatalogControllerTest method catalog_returns_only_original_ids_by_default.

@Test
public void catalog_returns_only_original_ids_by_default() throws Exception {
    CatalogController controller = new CatalogController(new MetricRegistry());
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = new TestHttpServletResponse();
    HashMap<String, Object> attributes = new HashMap<>();
    attributes.put(ContextUtil.maxRequestSizeKey, Integer.MAX_VALUE);
    attributes.put(JsonMapperKt.JSON_MAPPER_KEY, new JavalinJackson());
    attributes.put("PolicyFactory", this.sanitizer);
    when(request.getInputStream()).thenReturn(new TestServletInputStream(""));
    when(request.getAttribute("database")).thenReturn(this.conn);
    when(request.getRequestURI()).thenReturn("/catalog/TIMESERIES");
    when(request.getHeader(Header.ACCEPT)).thenReturn("application/json;version=2");
    Context context = ContextUtil.init(request, response, "*", new HashMap<String, String>(), HandlerType.GET, attributes);
    context.attribute("database", this.conn);
    controller.getOne(context, CatalogableEndpoint.TIMESERIES.getValue());
    assertEquals(HttpCode.OK.getStatus(), response.getStatus(), "200 OK was not returned");
    assertNotNull(response.getOutputStream(), "Output stream wasn't created");
}
Also used : Context(io.javalin.http.Context) HashMap(java.util.HashMap) MetricRegistry(com.codahale.metrics.MetricRegistry) TestHttpServletResponse(fixtures.TestHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) TestHttpServletResponse(fixtures.TestHttpServletResponse) TestServletInputStream(fixtures.TestServletInputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) JavalinJackson(io.javalin.plugin.json.JavalinJackson) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with TestServletInputStream

use of fixtures.TestServletInputStream in project cwms-radar-api by USACE.

the class ClobControllerTest method bad_format_returns_501.

@Test
public void bad_format_returns_501() throws Exception {
    final String testBody = "";
    ClobController controller = spy(new ClobController(new MetricRegistry()));
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    HashMap<String, Object> attributes = new HashMap<>();
    attributes.put(ContextUtil.maxRequestSizeKey, Integer.MAX_VALUE);
    attributes.put(JsonMapperKt.JSON_MAPPER_KEY, new JavalinJackson());
    when(request.getInputStream()).thenReturn(new TestServletInputStream(testBody));
    Context context = ContextUtil.init(request, response, "*", new HashMap<String, String>(), HandlerType.GET, attributes);
    context.attribute("database", getTestConnection());
    when(request.getAttribute("database")).thenReturn(getTestConnection());
    assertNotNull(context.attribute("database"), "could not get the connection back as an attribute");
    when(request.getHeader(Header.ACCEPT)).thenReturn("BAD FORMAT");
    assertThrows(FormattingException.class, () -> {
        controller.getAll(context);
    });
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Context(io.javalin.http.Context) HashMap(java.util.HashMap) JavalinJackson(io.javalin.plugin.json.JavalinJackson) MetricRegistry(com.codahale.metrics.MetricRegistry) HttpServletResponse(javax.servlet.http.HttpServletResponse) ClobController(cwms.radar.api.ClobController) TestServletInputStream(fixtures.TestServletInputStream) Test(org.junit.jupiter.api.Test)

Example 4 with TestServletInputStream

use of fixtures.TestServletInputStream in project cwms-radar-api by USACE.

the class LocationControllerTest method test_basic_operations.

/**
 * Test of getOne method, of class LocationController.
 */
@Test
public void test_basic_operations() throws Exception {
    final String testBody = "";
    LocationController instance = new LocationController(new MetricRegistry());
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = new TestHttpServletResponse();
    HashMap<String, Object> attributes = new HashMap<>();
    attributes.put(ContextUtil.maxRequestSizeKey, Integer.MAX_VALUE);
    attributes.put(JsonMapperKt.JSON_MAPPER_KEY, new JavalinJackson());
    when(request.getInputStream()).thenReturn(new TestServletInputStream(testBody));
    final Context context = ContextUtil.init(request, response, "*", new HashMap<String, String>(), HandlerType.GET, attributes);
    context.attribute("database", getTestConnection());
    when(request.getAttribute("database")).thenReturn(getTestConnection());
    assertNotNull(context.attribute("database"), "could not get the connection back as an attribute");
    String Location_id = "SimpleNoAlias";
    instance.getOne(context, Location_id);
    assertEquals(200, context.status(), "incorrect status code returned");
}
Also used : Context(io.javalin.http.Context) HashMap(java.util.HashMap) MetricRegistry(com.codahale.metrics.MetricRegistry) TestHttpServletResponse(fixtures.TestHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) TestHttpServletResponse(fixtures.TestHttpServletResponse) TestServletInputStream(fixtures.TestServletInputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) JavalinJackson(io.javalin.plugin.json.JavalinJackson) Test(org.junit.jupiter.api.Test)

Example 5 with TestServletInputStream

use of fixtures.TestServletInputStream in project cwms-radar-api by USACE.

the class RatingsControllerTest method post_to_create_passed_to_deserializeXml.

// This is only supposed to test that when XML data is posted to create,
// that data is forward to the method deserializeFromXml
@Test
void post_to_create_passed_to_deserializeXml() throws Exception {
    final String testBody = "could be anything";
    RatingController controller = spy(new RatingController(new MetricRegistry()));
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    HashMap<String, Object> attributes = new HashMap<>();
    attributes.put(ContextUtil.maxRequestSizeKey, Integer.MAX_VALUE);
    attributes.put(JsonMapperKt.JSON_MAPPER_KEY, new JavalinJackson());
    when(request.getInputStream()).thenReturn(new TestServletInputStream(testBody));
    // Context context = new Context(request,response, attributes);
    Context context = ContextUtil.init(request, response, "*", new HashMap<String, String>(), HandlerType.POST, attributes);
    context.attribute("database", this.conn);
    when(request.getContentLength()).thenReturn(testBody.length());
    when(request.getAttribute("database")).thenReturn(this.conn);
    assertNotNull(context.attribute("database"), "could not get the connection back as an attribute");
    when(request.getHeader(Header.ACCEPT)).thenReturn(Formats.XMLV2);
    when(request.getContentType()).thenReturn(Formats.XMLV2);
    controller.create(context);
    // For this test, it's ok that the server throws a RatingException
    // Only want to check that the controller accessed our mock dao in the expected way
    // Curious that it is XML and not XMLv2
    verify(controller, times(1)).deserializeRatingSet(testBody, Formats.XML);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Context(io.javalin.http.Context) HashMap(java.util.HashMap) JavalinJackson(io.javalin.plugin.json.JavalinJackson) MetricRegistry(com.codahale.metrics.MetricRegistry) HttpServletResponse(javax.servlet.http.HttpServletResponse) RatingController(cwms.radar.api.RatingController) TestServletInputStream(fixtures.TestServletInputStream) JsonRatingUtilsTest(cwms.radar.data.dao.JsonRatingUtilsTest) Test(org.junit.jupiter.api.Test) DaoTest(cwms.radar.data.dao.DaoTest)

Aggregations

MetricRegistry (com.codahale.metrics.MetricRegistry)5 TestServletInputStream (fixtures.TestServletInputStream)5 Context (io.javalin.http.Context)5 HashMap (java.util.HashMap)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 JavalinJackson (io.javalin.plugin.json.JavalinJackson)4 Test (org.junit.jupiter.api.Test)4 TestHttpServletResponse (fixtures.TestHttpServletResponse)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ClobController (cwms.radar.api.ClobController)1 RatingController (cwms.radar.api.RatingController)1 DaoTest (cwms.radar.data.dao.DaoTest)1 JsonRatingUtilsTest (cwms.radar.data.dao.JsonRatingUtilsTest)1 Disabled (org.junit.jupiter.api.Disabled)1 ValueSource (org.junit.jupiter.params.provider.ValueSource)1