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);
});
}
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");
}
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);
});
}
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");
}
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);
}
Aggregations