BloomFilterHttpService { @Post("/{name}/check-and-set") public JsonNode checkAndSet(@Param String name, @RequestObject JsonNode req) throws FilterNotFoundException { final JsonNode testingValue = checkNotNull("value", req.get("value")); checkParameter("value", testingValue.isTextual(), "expect string type"); final BloomFilter filter = bloomFilterManager.ensureGetValidFilter(name); final boolean contain = !filter.set(testingValue.textValue()); return BooleanNode.valueOf(contain); } 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 testCheckAndSetValueIsNull() { final ObjectNode param = mapper.createObjectNode(); assertThatThrownBy(() -> service.checkAndSet(testingFilterName, param)) .isInstanceOf(BadParameterException.class) .hasMessageContaining("required parameter"); }
@Test public void testCheckAndSetValueIsNotString() { final ObjectNode param = mapper.createObjectNode(); param.put("value", 12345); assertThatThrownBy(() -> service.checkAndSet(testingFilterName, param)) .isInstanceOf(BadParameterException.class) .hasMessageContaining("invalid parameter"); }
@Test public void testCheckAndSet() throws Exception { final String testingValue = "testing-value"; final GuavaBloomFilter testingFilter = factory.createFilter(new ExpirableBloomFilterConfig()); when(mockedManager.ensureGetValidFilter(testingFilterName)).thenReturn(testingFilter); final ObjectNode param = mapper.createObjectNode(); param.put("value", testingValue); final JsonNode res = service.checkAndSet(testingFilterName, param); assertThat(res.isBoolean()).isTrue(); assertThat(res.asBoolean()).isFalse(); assertThat(testingFilter.mightContain(testingValue)).isTrue(); }