use of com.tvd12.ezyhttp.server.core.manager.ComponentManager in project ezyhttp by youngmonkeys.
the class BlockingServletTest method getRequestDeserializeValueException.
@Test
public void getRequestDeserializeValueException() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
BlockingServlet sut = new BlockingServlet();
sut.init();
String requestURI = "/get";
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
when(request.getRequestURI()).thenReturn(requestURI);
when(request.getServerPort()).thenReturn(PORT);
when(request.getParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("param")));
when(request.getParameter("param")).thenReturn("ParameterValue");
when(request.getHeaderNames()).thenReturn(Collections.enumeration(Collections.singletonList("header")));
when(request.getHeader("header")).thenReturn("HeaderValue");
when(request.getCookies()).thenReturn(new Cookie[] { new Cookie("cookie", "CookieValue") });
RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
GetRequestDeserializeValueExceptionHandler requestHandler = new GetRequestDeserializeValueExceptionHandler();
requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, false), requestHandler);
HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
ServletOutputStream outputStream = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(outputStream);
// when
sut.service(request, response);
// then
verify(request, times(1)).getMethod();
verify(request, times(1)).getRequestURI();
verify(response, times(1)).getContentType();
verify(response, times(1)).getOutputStream();
verify(response, times(1)).setStatus(StatusCodes.BAD_REQUEST);
componentManager.destroy();
}
use of com.tvd12.ezyhttp.server.core.manager.ComponentManager in project ezyhttp by youngmonkeys.
the class BlockingServletTest method doGetManagementTest.
@Test
public void doGetManagementTest() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
componentManager.setManagementPort(MANAGEMENT_POR);
BlockingServlet sut = new BlockingServlet();
sut.init();
String requestURI = "/get";
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
when(request.getRequestURI()).thenReturn(requestURI);
when(request.getServerPort()).thenReturn(MANAGEMENT_POR);
when(request.getParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("param")));
when(request.getParameter("param")).thenReturn("ParameterValue");
when(request.getParameterValues("param")).thenReturn(new String[] { "ParameterValue" });
when(request.getHeaderNames()).thenReturn(Collections.enumeration(Collections.singletonList("header")));
when(request.getHeader("header")).thenReturn("HeaderValue");
when(request.getCookies()).thenReturn(new Cookie[] { new Cookie("cookie", "CookieValue") });
RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
GetRequestHandler requestHandler = new GetRequestHandler();
requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, true), requestHandler);
HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
ServletOutputStream outputStream = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(outputStream);
// when
sut.service(request, response);
// then
verify(request, times(1)).getMethod();
verify(request, times(1)).getRequestURI();
verify(request, times(1)).getServerPort();
verify(response, times(1)).getContentType();
verify(response, times(1)).getOutputStream();
verify(response, times(1)).setStatus(StatusCodes.OK);
DataConverters dataConverters = componentManager.getDataConverters();
BodySerializer bodySerializer = dataConverters.getBodySerializer(ContentTypes.APPLICATION_JSON);
ExResponse responseData = new ExResponse("Hello ParameterValue, HeaderValue, CookieValue");
verify(outputStream, times(1)).write(bodySerializer.serialize(responseData));
componentManager.destroy();
}
use of com.tvd12.ezyhttp.server.core.manager.ComponentManager in project ezyhttp by youngmonkeys.
the class BlockingServletTest method doGetFromManagement.
@Test
public void doGetFromManagement() throws Exception {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setServerPort(PORT);
componentManager.setManagementPort(MANAGEMENT_POR);
componentManager.getRequestHandlerManager().addHandler(new RequestURI(HttpMethod.GET, "/get", false), mock(RequestHandler.class));
BlockingServlet sut = new BlockingServlet();
sut.init();
String requestURI = "/get";
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
when(request.getRequestURI()).thenReturn(requestURI);
when(request.getServerPort()).thenReturn(MANAGEMENT_POR);
when(request.getParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("param")));
when(request.getParameter("param")).thenReturn("ParameterValue");
when(request.getParameterValues("param")).thenReturn(new String[] { "ParameterValue" });
when(request.getHeaderNames()).thenReturn(Collections.enumeration(Collections.singletonList("header")));
when(request.getHeader("header")).thenReturn("HeaderValue");
when(request.getCookies()).thenReturn(new Cookie[] { new Cookie("cookie", "CookieValue") });
HttpServletResponse response = mock(HttpServletResponse.class);
// when
sut.service(request, response);
// then
verify(request, times(1)).getMethod();
verify(request, times(1)).getRequestURI();
verify(response, times(1)).setStatus(StatusCodes.OK);
componentManager.destroy();
}
use of com.tvd12.ezyhttp.server.core.manager.ComponentManager in project ezyhttp by youngmonkeys.
the class ApplicationContextBuilderTest method test.
@Test
public void test() {
// given
EzyBeanContext internalBeanContext = EzyBeanContext.builder().addSingleton(new InternalSingleton1()).build();
Properties properties = new Properties();
properties.put("b", 2);
ApplicationContext applicationContext = new ApplicationContextBuilder().scan("com.tvd12.ezyhttp.server.core.test.component", "com.tvd12.ezyhttp.server.core.test.config", "com.tvd12.ezyhttp.server.core.test.controller").scan(Arrays.asList("com.tvd12.ezyhttp.server.core.test.reflect", "com.tvd12.ezyhttp.server.core.test.request", "com.tvd12.ezyhttp.server.core.test.resources", "com.tvd12.ezyhttp.server.core.test.service")).addComponentClasses(Collections.singletonList(SourceService.class)).addPropertiesSources(Collections.singletonList("application3.yaml")).addPropertiesSource("application-enable.yaml").addProperty("a", "1").addProperties(properties).addProperties(Collections.singletonMap("c", "3")).beanContext(internalBeanContext).addSingleton(new InternalSingleton2()).addSingleton(Collections.singletonMap("internalSingleton3", new InternalSingleton3())).addSingleton(mock(AbsentMessageResolver.class)).addComponentClass(InternalSingleton1.class).addPropertiesSources().build();
EzyBeanContext beanContext = applicationContext.getBeanContext();
// when
int actualOneProp = beanContext.getProperty("one", int.class);
boolean managementEnable = beanContext.getProperty("management.enable", boolean.class);
Boolean resourceEnable = beanContext.getProperty("resources.enable", boolean.class);
Boolean resourceUploadEnable = beanContext.getProperty("resources.upload.enable", boolean.class);
UserService userService = beanContext.getSingleton(UserService.class);
UserService0 userService0 = beanContext.getSingleton(UserService0.class);
ViewContextBuilder viewContextBuilder = beanContext.getSingleton(ViewContextBuilder.class);
ResourceResolver resourceResolver = beanContext.getSingleton(ResourceResolver.class);
ResourceDownloadManager resourceDownloadManager = beanContext.getSingleton(ResourceDownloadManager.class);
Set<String> packagesToScan = beanContext.getPackagesToScan();
EventService eventService = beanContext.getSingleton(EventService.class);
SourceService sourceService = beanContext.getSingleton(SourceService.class);
String helloValue = beanContext.getProperty("hello", String.class);
InternalSingleton1 internalSingleton1 = beanContext.getBeanCast(InternalSingleton1.class);
InternalSingleton2 internalSingleton2 = beanContext.getBeanCast(InternalSingleton2.class);
InternalSingleton3 internalSingleton3 = beanContext.getBeanCast(InternalSingleton3.class);
// then
Asserts.assertEquals(1, actualOneProp);
Asserts.assertTrue(managementEnable);
Asserts.assertTrue(resourceEnable);
Asserts.assertTrue(resourceUploadEnable);
Asserts.assertNotNull(userService);
Asserts.assertNotNull(userService0);
Asserts.assertNotNull(viewContextBuilder);
Asserts.assertNotNull(resourceDownloadManager);
Asserts.assertEquals(4, resourceResolver.getResources().size());
Asserts.assertNotNull(eventService);
Asserts.assertNotNull(sourceService);
Asserts.assertNotNull(helloValue);
Asserts.assertEquals("1", beanContext.getProperty("a", String.class));
Asserts.assertEquals(2, beanContext.getProperty("b", int.class));
Asserts.assertEquals("3", beanContext.getProperty("c", String.class));
Asserts.assertEquals("3", applicationContext.getProperty("c", String.class));
Asserts.assertEquals(packagesToScan, Sets.newHashSet("com.tvd12.ezyhttp.server", "com.tvd12.ezyhttp.server.core.test", "com.tvd12.ezyhttp.server.core.test.component", "com.tvd12.ezyhttp.server.core.test.config", "com.tvd12.ezyhttp.server.core.test.controller", "com.tvd12.ezyhttp.server.core.test.event", "com.tvd12.ezyhttp.server.core.test.api", "com.tvd12.ezyhttp.server.core.test.reflect", "com.tvd12.ezyhttp.server.core.test.request", "com.tvd12.ezyhttp.server.core.test.resources", "com.tvd12.ezyhttp.server.core.test.service"));
Asserts.assertNotNull(internalSingleton1);
Asserts.assertNotNull(internalSingleton2);
Asserts.assertNotNull(internalSingleton3);
ComponentManager componentManager = ComponentManager.getInstance();
Asserts.assertEquals(componentManager.getAsyncDefaultTimeout(), 10000);
System.out.println(applicationContext);
applicationContext.destroy();
}
use of com.tvd12.ezyhttp.server.core.manager.ComponentManager in project ezyhttp by youngmonkeys.
the class ComponentManagerTest method test.
@Test
public void test() {
// given
ComponentManager componentManager = ComponentManager.getInstance();
componentManager.setExposeManagementURIs(true);
// when
// then
Asserts.assertTrue(componentManager.isExposeManagementURIs());
componentManager.destroy();
}
Aggregations