BloomFilterHttpService { @Post("/{name}/multi-check-and-set") public JsonNode multiCheckAndSet(@Param String name, @RequestObject JsonNode req) throws FilterNotFoundException { final JsonNode values = checkNotNull("values", req.get("values")); checkParameter("values", values.isArray(), "expect Json array"); final BloomFilter filter = bloomFilterManager.ensureGetValidFilter(name); final ArrayNode response = MAPPER.createArrayNode(); for (final JsonNode value : values) { if (value.isTextual()) { response.add(BooleanNode.valueOf(!filter.set(value.textValue()))); } else { response.add(BooleanNode.FALSE); } } return response; } BloomFilterHttpService(BloomFilterManager<?, ? super ExpirableBloomFilterConfig> bloomFilterManager); @Put("/{name}") HttpResponse create(@Param String name,
                               @RequestObject JsonNode req); @Get("/{name}") JsonNode getFilterInfo(@Param String name); @Get("/list") JsonNode list(); @Post("/{name}/check") JsonNode check(@Param String name,
                          @RequestObject JsonNode req); @Post("/{name}/multi-check") JsonNode multiCheck(@Param String name,
                               @RequestObject JsonNode req); @Post("/{name}/check-and-set") JsonNode checkAndSet(@Param String name,
                                @RequestObject JsonNode req); @Post("/{name}/multi-check-and-set") JsonNode multiCheckAndSet(@Param String name,
                                     @RequestObject JsonNode req); @Delete("/{name}") HttpResponse remove(@Param String name);  }
@Test public void testMultiSetValueIsNull() { final ObjectNode param = mapper.createObjectNode(); assertThatThrownBy(() -> service.multiCheckAndSet(testingFilterName, param)) .isInstanceOf(BadParameterException.class) .hasMessageContaining("required parameter"); }
@Test public void testMultiSetValueIsNotArray() { final ObjectNode param = mapper.createObjectNode(); param.put("values", "12345"); assertThatThrownBy(() -> service.multiCheckAndSet(testingFilterName, param)) .isInstanceOf(BadParameterException.class) .hasMessageContaining("invalid parameter"); }
@Test public void testMultiSet() throws Exception { final List<String> testingValues = new ArrayList<>(); testingValues.add("testing-value1"); testingValues.add("testing-value2"); testingValues.add("testing-value3"); final GuavaBloomFilter testingFilter = factory.createFilter(new ExpirableBloomFilterConfig()); when(mockedManager.ensureGetValidFilter(testingFilterName)).thenReturn(testingFilter); final ArrayNode values = mapper.createArrayNode(); testingValues.forEach(values::add); final ObjectNode param = mapper.createObjectNode(); param.set("values", values); final JsonNode res = service.multiCheckAndSet(testingFilterName, param); assertThat(res.isArray()).isTrue(); for (final JsonNode mightContain : res) { assertThat(mightContain.isBoolean()).isTrue(); assertThat(mightContain.asBoolean()).isFalse(); } for (final String value : testingValues) { assertThat(testingFilter.mightContain(value)).isTrue(); } }
@Test public void testMultiCheckAndSet() throws Exception { final GuavaBloomFilter testingFilter = factory.createFilter(new ExpirableBloomFilterConfig()); when(mockedManager.ensureGetValidFilter(testingFilterName)).thenReturn(testingFilter); final ArrayNode values = mapper.createArrayNode(); values.add("testing-value1"); values.add("testing-value2"); values.add("testing-value2"); final ObjectNode param = mapper.createObjectNode(); param.set("values", values); final JsonNode res = service.multiCheck(testingFilterName, param); for (final JsonNode node : res) { assertThat(node.asBoolean()).isFalse(); } }