ParamValidationFilter implements Filter { @Override public void filterRequest(Request request, Response response) throws InternalErrorException, BadRequestException, ProcessingException { PoseidonRequest poseidonRequest = (PoseidonRequest) request; Map<String, Object> parsedParams = new HashMap<>(); if (params != null) { parsedParams.putAll(validateParams(poseidonRequest, params.getRequired(), true)); parsedParams.putAll(validateParams(poseidonRequest, params.getOptional(), false)); } poseidonRequest.setAttribute(RequestConstants.PARAMS, parsedParams); } ParamValidationFilter();  ParamValidationFilter(ParamsPOJO params, Configuration configuration); @Override void filterRequest(Request request, Response response); @Override void filterResponse(Request request, Response response);  }
@Test public void testPathParamGreedyMiddle() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO2 = mockPathParam("test2", 3); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO2 }; when(params.getRequired()).thenReturn(paramPOJOs); RequestContext.set(RequestConstants.URI, "/3/abcdef"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz/qwe/yui/ase/def"); PoseidonRequest request = new PoseidonRequest(servletRequest); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(1, parsedParams.size()); assertEquals("xyz/qwe/yui/ase", parsedParams.get("test2")); }
@Test public void testPathParamGreedyMiddleTail() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockPathParam("test", 5); ParamPOJO paramPOJO2 = mockPathParam("test2", 3); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO, paramPOJO2 }; when(params.getRequired()).thenReturn(paramPOJOs); RequestContext.set(RequestConstants.URI, "/3/abcdef/**"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz/qwe/def/ase/qjw"); PoseidonRequest request = new PoseidonRequest(servletRequest); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(2, parsedParams.size()); assertEquals("xyz/qwe", parsedParams.get("test2")); assertEquals("ase/qjw", parsedParams.get("test")); }
@Test public void testPathParamGreedyMiddleTailMultiple() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockPathParam("test", 5); ParamPOJO paramPOJO2 = mockPathParam("test2", 3); ParamPOJO paramPOJO3 = mockPathParam("test3", 8); ParamPOJO paramPOJO4 = mockPathParam("test4", 10); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO, paramPOJO2, paramPOJO3, paramPOJO4 }; when(params.getRequired()).thenReturn(paramPOJOs); RequestContext.set(RequestConstants.URI, "/3/abcdefgbh/ytr/{test3}/qwe/**"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz/qwe/def/ase/qjw/gbh/ytr/thy/qwe/123/567/212"); PoseidonRequest request = new PoseidonRequest(servletRequest); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(4, parsedParams.size()); assertEquals("xyz/qwe", parsedParams.get("test2")); assertEquals("ase/qjw", parsedParams.get("test")); assertEquals("thy", parsedParams.get("test3")); assertEquals("123/567/212", parsedParams.get("test4")); }
@Test public void testQueryParam() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockQueryParam("test", ParamPOJO.DataType.STRING); ParamPOJO paramPOJO2 = mockQueryParam("test2", ParamPOJO.DataType.BOOLEAN); ParamPOJO paramPOJO3 = mockQueryParam("test3", ParamPOJO.DataType.INTEGER); ParamPOJO paramPOJO4 = mockQueryParam("test4", ParamPOJO.DataType.NUMBER); ParamPOJO paramPOJO5 = mockQueryParam("test5", ParamPOJO.DataType.LONG); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO, paramPOJO2, paramPOJO3, paramPOJO4, paramPOJO5 }; when(params.getRequired()).thenReturn(paramPOJOs); Map<String, String[]> parameterMap = new HashMap<>(); parameterMap.put("test", new String[] { "works" }); parameterMap.put("test2", new String[] { "true" }); parameterMap.put("test3", new String[] { "2132" }); parameterMap.put("test4", new String[] { "32.532" }); parameterMap.put("test5", new String[] { "2147483648" }); RequestContext.set(RequestConstants.URI, "/3/abc/xyz"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz", parameterMap); PoseidonRequest request = new PoseidonRequest(servletRequest); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(5, parsedParams.size()); Object test = parsedParams.get("test"); assertTrue(test instanceof String); assertEquals("works", test); Object test2 = parsedParams.get("test2"); assertTrue(test2 instanceof Boolean); assertEquals(true, test2); Object test3 = parsedParams.get("test3"); assertTrue(test3 instanceof Integer); assertEquals(2132, test3); Object test4 = parsedParams.get("test4"); assertTrue(test4 instanceof Double); assertEquals(32.532, test4); Object test5 = parsedParams.get("test5"); assertTrue(test5 instanceof Long); assertEquals(2147483648L, test5); }
@Test public void testHeaderParam() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockHeaderParam("test", ParamPOJO.DataType.STRING); ParamPOJO paramPOJO2 = mockHeaderParam("test2", ParamPOJO.DataType.BOOLEAN); ParamPOJO paramPOJO3 = mockHeaderParam("test3", ParamPOJO.DataType.INTEGER); ParamPOJO paramPOJO4 = mockHeaderParam("test4", ParamPOJO.DataType.NUMBER); ParamPOJO paramPOJO5 = mockHeaderParam("test5", ParamPOJO.DataType.LONG); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO, paramPOJO2, paramPOJO3, paramPOJO4, paramPOJO5 }; when(params.getRequired()).thenReturn(paramPOJOs); Map<String, String> headers = new HashMap<>(); headers.put("test", "works"); headers.put("test2", "true"); headers.put("test3", "2132"); headers.put("test4", "32.532"); headers.put("test5", "2147483648"); RequestContext.set(RequestConstants.URI, "/3/abc/xyz"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz", new HashMap<>(), headers); PoseidonRequest request = new PoseidonRequest(servletRequest); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(5, parsedParams.size()); Object test = parsedParams.get("test"); assertTrue(test instanceof String); assertEquals("works", test); Object test2 = parsedParams.get("test2"); assertTrue(test2 instanceof Boolean); assertEquals(true, test2); Object test3 = parsedParams.get("test3"); assertTrue(test3 instanceof Integer); assertEquals(2132, test3); Object test4 = parsedParams.get("test4"); assertTrue(test4 instanceof Double); assertEquals(32.532, test4); Object test5 = parsedParams.get("test5"); assertTrue(test5 instanceof Long); assertEquals(2147483648L, test5); }
@Test public void testPathParamTypes() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockPathParam("test", 3, ParamPOJO.DataType.STRING); ParamPOJO paramPOJO2 = mockPathParam("test2", 4, ParamPOJO.DataType.BOOLEAN); ParamPOJO paramPOJO3 = mockPathParam("test3", 5, ParamPOJO.DataType.INTEGER); ParamPOJO paramPOJO4 = mockPathParam("test4", 6, ParamPOJO.DataType.NUMBER); ParamPOJO paramPOJO5 = mockPathParam("test5", 7, ParamPOJO.DataType.LONG); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO, paramPOJO2, paramPOJO3, paramPOJO4, paramPOJO5 }; when(params.getRequired()).thenReturn(paramPOJOs); RequestContext.set(RequestConstants.URI, "/3/abc/{test}/{test2}/{test3}/{test4}/{test5}"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/works/true/2132/32.532/2147483648"); PoseidonRequest request = new PoseidonRequest(servletRequest); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(5, parsedParams.size()); Object test = parsedParams.get("test"); assertTrue(test instanceof String); assertEquals("works", test); Object test2 = parsedParams.get("test2"); assertTrue(test2 instanceof Boolean); assertEquals(true, test2); Object test3 = parsedParams.get("test3"); assertTrue(test3 instanceof Integer); assertEquals(2132, test3); Object test4 = parsedParams.get("test4"); assertTrue(test4 instanceof Double); assertEquals(32.532, test4); Object test5 = parsedParams.get("test5"); assertTrue(test5 instanceof Long); assertEquals(2147483648L, test5); }
@Test public void testEnumParams() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockQueryParam("test", ParamPOJO.DataType.ENUM); mockJavaType(paramPOJO, "com.flipkart.poseidon.TestEnum"); ParamPOJO paramPOJO1 = mockHeaderParam("test1", ParamPOJO.DataType.ENUM); mockJavaType(paramPOJO1, "com.flipkart.poseidon.TestEnum"); ParamPOJO paramPOJO2 = mockPathParam("test2", 2, ParamPOJO.DataType.ENUM); mockJavaType(paramPOJO2, "com.flipkart.poseidon.TestEnum"); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO, paramPOJO1, paramPOJO2 }; when(params.getRequired()).thenReturn(paramPOJOs); load(params); Map<String, String[]> parameterMap = new HashMap<>(); parameterMap.put("test", new String[] { "XYZ" }); Map<String, String> headers = new HashMap<>(); headers.put("test1", "ABC"); RequestContext.set(RequestConstants.URI, "/3/{test2}/xyz"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/ABC/xyz", parameterMap, headers); PoseidonRequest request = new PoseidonRequest(servletRequest); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(3, parsedParams.size()); Object test = parsedParams.get("test"); assertTrue(test instanceof TestEnum); assertEquals(TestEnum.XYZ, test); Object testPath = parsedParams.get("test2"); assertTrue(testPath instanceof TestEnum); assertEquals(TestEnum.ABC, testPath); Object testHeader = parsedParams.get("test1"); assertTrue(testHeader instanceof TestEnum); assertEquals(TestEnum.ABC, testHeader); }
@Test(expected = BadRequestException.class) public void testQueryParamEnumWrong() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockQueryParam("test", ParamPOJO.DataType.ENUM); mockJavaType(paramPOJO, "com.flipkart.poseidon.TestEnum"); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO }; when(params.getRequired()).thenReturn(paramPOJOs); Map<String, String[]> parameterMap = new HashMap<>(); parameterMap.put("test", new String[] { "XYZ-A" }); RequestContext.set(RequestConstants.URI, "/3/abc/xyz"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz", parameterMap); PoseidonRequest request = new PoseidonRequest(servletRequest); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); fail(); }
@Test(expected = UnsupportedOperationException.class) public void testQueryParamEnumWrongClass() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockQueryParam("test", ParamPOJO.DataType.ENUM); mockJavaType(paramPOJO, "com.flipkart.poseidon.TestEnum123"); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO }; when(params.getRequired()).thenReturn(paramPOJOs); load(params); Map<String, String[]> parameterMap = new HashMap<>(); parameterMap.put("test", new String[] { "XYZ-A" }); RequestContext.set(RequestConstants.URI, "/3/abc/xyz"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz", parameterMap); PoseidonRequest request = new PoseidonRequest(servletRequest); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); fail(); }
@Test public void testBodyParam() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockBodyParam("test", "com.flipkart.poseidon.TestPOJO"); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO }; when(params.getRequired()).thenReturn(paramPOJOs); load(params); RequestContext.set(RequestConstants.URI, "/3/abc/xyz"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz"); PoseidonRequest request = new PoseidonRequest(servletRequest); request.setAttribute(RequestConstants.BODY, configuration.getObjectMapper().writeValueAsString(new TestPOJO("xyz", true))); request.setAttribute(RequestConstants.BODY_BYTES, configuration.getObjectMapper().writeValueAsBytes(new TestPOJO("abc", true))); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(1, parsedParams.size()); Object test = parsedParams.get("test"); assertTrue(test instanceof TestPOJO); TestPOJO testPOJO = (TestPOJO) test; assertEquals("xyz", testPOJO.getAbc()); assertEquals(true, testPOJO.isTest()); }
@Test public void testBodyParamList() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockBodyParam("test", new VariableModel("java.util.List", new VariableModel[] { new VariableModel("com.flipkart.poseidon.TestPOJO") })); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO }; when(params.getRequired()).thenReturn(paramPOJOs); load(params); RequestContext.set(RequestConstants.URI, "/3/abc/xyz"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz"); PoseidonRequest request = new PoseidonRequest(servletRequest); request.setAttribute(RequestConstants.BODY, configuration.getObjectMapper().writeValueAsString(Collections.singletonList(new TestPOJO("xyz", true)))); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(1, parsedParams.size()); Object test = parsedParams.get("test"); assertTrue(test instanceof List); Object testPOJOBlob = ((List) test).get(0); assertTrue(testPOJOBlob instanceof TestPOJO); TestPOJO testPOJO = (TestPOJO) testPOJOBlob; assertEquals("xyz", testPOJO.getAbc()); assertEquals(true, testPOJO.isTest()); }
@Test public void testBodyParamMap() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockBodyParam("test", new VariableModel("java.util.Map", new VariableModel[] { new VariableModel("java.lang.String"), new VariableModel("com.flipkart.poseidon.TestPOJO") })); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO }; when(params.getRequired()).thenReturn(paramPOJOs); load(params); RequestContext.set(RequestConstants.URI, "/3/abc/xyz"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz"); PoseidonRequest request = new PoseidonRequest(servletRequest); request.setAttribute(RequestConstants.BODY, configuration.getObjectMapper().writeValueAsString(Collections.singletonMap("key", new TestPOJO("xyz", true)))); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(1, parsedParams.size()); Object test = parsedParams.get("test"); assertTrue(test instanceof Map); Object testPOJOBlob = ((Map) test).get("key"); assertTrue(testPOJOBlob instanceof TestPOJO); TestPOJO testPOJO = (TestPOJO) testPOJOBlob; assertEquals("xyz", testPOJO.getAbc()); assertEquals(true, testPOJO.isTest()); }
@Test public void testBodyParamGenerics() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockBodyParam("test", new VariableModel("com.flipkart.poseidon.Wrapper", new VariableModel[] { new VariableModel("java.util.Map", new VariableModel[] { new VariableModel("java.lang.String"), new VariableModel("com.flipkart.poseidon.TestPOJO") }) })); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO }; when(params.getRequired()).thenReturn(paramPOJOs); load(params); RequestContext.set(RequestConstants.URI, "/3/abc/xyz"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz"); PoseidonRequest request = new PoseidonRequest(servletRequest); request.setAttribute(RequestConstants.BODY, configuration.getObjectMapper().writeValueAsString(new Wrapper<>(Collections.singletonMap("key", new TestPOJO("xyz", true))))); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(1, parsedParams.size()); Object test = parsedParams.get("test"); assertTrue(test instanceof Wrapper); Object wrapperMap = ((Wrapper) test).getObj(); assertTrue(wrapperMap instanceof Map); Object testPOJOBlob = ((Map) wrapperMap).get("key"); assertTrue(testPOJOBlob instanceof TestPOJO); TestPOJO testPOJO = (TestPOJO) testPOJOBlob; assertEquals("xyz", testPOJO.getAbc()); assertEquals(true, testPOJO.isTest()); }
@Test public void testBodyBytesParam() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockBodyParam("test", "com.flipkart.poseidon.TestPOJO"); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO }; when(params.getRequired()).thenReturn(paramPOJOs); load(params); RequestContext.set(RequestConstants.URI, "/3/abc/xyz"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz"); PoseidonRequest request = new PoseidonRequest(servletRequest); request.setAttribute(RequestConstants.BODY_BYTES, configuration.getObjectMapper().writeValueAsBytes(new TestPOJO("xyz", true))); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(1, parsedParams.size()); Object test = parsedParams.get("test"); assertTrue(test instanceof TestPOJO); TestPOJO testPOJO = (TestPOJO) test; assertEquals("xyz", testPOJO.getAbc()); assertEquals(true, testPOJO.isTest()); }
@Test public void testBodyBytesParamList() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockBodyParam("test", new VariableModel("java.util.List", new VariableModel[] { new VariableModel("com.flipkart.poseidon.TestPOJO") })); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO }; when(params.getRequired()).thenReturn(paramPOJOs); load(params); RequestContext.set(RequestConstants.URI, "/3/abc/xyz"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz"); PoseidonRequest request = new PoseidonRequest(servletRequest); request.setAttribute(RequestConstants.BODY_BYTES, configuration.getObjectMapper().writeValueAsBytes(Collections.singletonList(new TestPOJO("xyz", true)))); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(1, parsedParams.size()); Object test = parsedParams.get("test"); assertTrue(test instanceof List); Object testPOJOBlob = ((List) test).get(0); assertTrue(testPOJOBlob instanceof TestPOJO); TestPOJO testPOJO = (TestPOJO) testPOJOBlob; assertEquals("xyz", testPOJO.getAbc()); assertEquals(true, testPOJO.isTest()); }
@Test public void testBodyBytesParamMap() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockBodyParam("test", new VariableModel("java.util.Map", new VariableModel[] { new VariableModel("java.lang.String"), new VariableModel("com.flipkart.poseidon.TestPOJO") })); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO }; when(params.getRequired()).thenReturn(paramPOJOs); load(params); RequestContext.set(RequestConstants.URI, "/3/abc/xyz"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz"); PoseidonRequest request = new PoseidonRequest(servletRequest); request.setAttribute(RequestConstants.BODY_BYTES, configuration.getObjectMapper().writeValueAsBytes(Collections.singletonMap("key", new TestPOJO("xyz", true)))); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(1, parsedParams.size()); Object test = parsedParams.get("test"); assertTrue(test instanceof Map); Object testPOJOBlob = ((Map) test).get("key"); assertTrue(testPOJOBlob instanceof TestPOJO); TestPOJO testPOJO = (TestPOJO) testPOJOBlob; assertEquals("xyz", testPOJO.getAbc()); assertEquals(true, testPOJO.isTest()); }
@Test public void testBodyBytesParamGenerics() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockBodyParam("test", new VariableModel("com.flipkart.poseidon.Wrapper", new VariableModel[] { new VariableModel("java.util.Map", new VariableModel[] { new VariableModel("java.lang.String"), new VariableModel("com.flipkart.poseidon.TestPOJO") }) })); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO }; when(params.getRequired()).thenReturn(paramPOJOs); load(params); RequestContext.set(RequestConstants.URI, "/3/abc/xyz"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz"); PoseidonRequest request = new PoseidonRequest(servletRequest); request.setAttribute(RequestConstants.BODY_BYTES, configuration.getObjectMapper().writeValueAsBytes(new Wrapper<>(Collections.singletonMap("key", new TestPOJO("xyz", true))))); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(1, parsedParams.size()); Object test = parsedParams.get("test"); assertTrue(test instanceof Wrapper); Object wrapperMap = ((Wrapper) test).getObj(); assertTrue(wrapperMap instanceof Map); Object testPOJOBlob = ((Map) wrapperMap).get("key"); assertTrue(testPOJOBlob instanceof TestPOJO); TestPOJO testPOJO = (TestPOJO) testPOJOBlob; assertEquals("xyz", testPOJO.getAbc()); assertEquals(true, testPOJO.isTest()); }
@Test public void testPathParam() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockPathParam("test", 4); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO }; when(params.getRequired()).thenReturn(paramPOJOs); RequestContext.set(RequestConstants.URI, "/3/abc/xyz/{test}"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz/qwe"); PoseidonRequest request = new PoseidonRequest(servletRequest); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(1, parsedParams.size()); assertEquals("qwe", parsedParams.get("test")); }
@Test(expected = BadRequestException.class) public void testPathParamNotFound() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockPathParam("test", 5); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO }; when(params.getRequired()).thenReturn(paramPOJOs); RequestContext.set(RequestConstants.URI, "/3/abc/xyz/qwe"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz/qwe"); PoseidonRequest request = new PoseidonRequest(servletRequest); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); }
@Test public void testPathParamMiddle() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockPathParam("test", 3); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO }; when(params.getRequired()).thenReturn(paramPOJOs); RequestContext.set(RequestConstants.URI, "/3/abc/{test}/qwe"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz/qwe"); PoseidonRequest request = new PoseidonRequest(servletRequest); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(1, parsedParams.size()); assertEquals("xyz", parsedParams.get("test")); }
@Test public void testPathParamInfiniteTail() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockPathParam("test", 4); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO }; when(params.getRequired()).thenReturn(paramPOJOs); RequestContext.set(RequestConstants.URI, "/3/abc/xyz/**"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz/qwe/yui/ase"); PoseidonRequest request = new PoseidonRequest(servletRequest); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(1, parsedParams.size()); assertEquals("qwe/yui/ase", parsedParams.get("test")); }
@Test public void testPathParamInfiniteTailWithoutGreedyMarker() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockPathParam("test", 4); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO }; when(params.getRequired()).thenReturn(paramPOJOs); RequestContext.set(RequestConstants.URI, "/3/abc/xyz/{test}"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz/qwe/yui/ase"); PoseidonRequest request = new PoseidonRequest(servletRequest); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(1, parsedParams.size()); assertEquals("qwe", parsedParams.get("test")); }
@Test public void testPathParamInfiniteTailMiddle() throws Exception { ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockPathParam("test", 4); ParamPOJO paramPOJO2 = mockPathParam("test2", 3); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO, paramPOJO2 }; when(params.getRequired()).thenReturn(paramPOJOs); RequestContext.set(RequestConstants.URI, "/3/abc/{test2}/**"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz/qwe/yui/ase"); PoseidonRequest request = new PoseidonRequest(servletRequest); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(2, parsedParams.size()); assertEquals("qwe/yui/ase", parsedParams.get("test")); assertEquals("xyz", parsedParams.get("test2")); }
@Test public void testPathParamInfiniteTailMiddleWithoutGreedyMarker() throws Exception { RequestContext.set(RequestConstants.URI, "/123/{MIDDLE}/543/{TAIL}"); ParamsPOJO params = mock(ParamsPOJO.class); ParamPOJO paramPOJO = mockPathParam("test", 4); ParamPOJO paramPOJO2 = mockPathParam("test2", 3); ParamPOJO[] paramPOJOs = new ParamPOJO[] { paramPOJO, paramPOJO2 }; when(params.getRequired()).thenReturn(paramPOJOs); RequestContext.set(RequestConstants.URI, "/3/abc/{test2}/{test}"); HttpServletRequest servletRequest = mockHttpServletRequest("/3/abc/xyz/qwe/yui/ase"); PoseidonRequest request = new PoseidonRequest(servletRequest); new ParamValidationFilter(params, configuration).filterRequest(request, new PoseidonResponse()); Map<String, Object> parsedParams = request.getAttribute(RequestConstants.PARAMS); assertEquals(2, parsedParams.size()); assertEquals("qwe", parsedParams.get("test")); assertEquals("xyz", parsedParams.get("test2")); }