BloomFilterHttpService { @Put("/{name}") public HttpResponse create(@Param String name, @RequestObject JsonNode req) { final JsonNode expectedInsertions = req.get("expectedInsertions"); final JsonNode fpp = req.get("fpp"); final JsonNode validPeriodAfterCreate = req.get("validPeriod") == null ? req.get("validPeriodAfterCreate") : req.get("validPeriod"); final JsonNode validPeriodAfterAccess = req.get("validPeriodAfterAccess"); final JsonNode overwrite = req.get("overwrite"); final ExpirableBloomFilterConfig config = new ExpirableBloomFilterConfig(); if (expectedInsertions != null) { config.setExpectedInsertions(expectedInsertions.intValue()); } if (fpp != null) { config.setFpp(fpp.doubleValue()); } if (validPeriodAfterCreate != null) { config.setValidPeriodAfterCreate(Duration.ofSeconds(validPeriodAfterCreate.intValue())); } if (validPeriodAfterAccess != null) { config.setValidPeriodAfterAccess(Duration.ofSeconds(validPeriodAfterAccess.intValue())); } final CreateFilterResult<?> createResult; if (overwrite != null && overwrite.isBoolean() && overwrite.asBoolean()) { createResult = bloomFilterManager.createFilter(name, config, true); } else { createResult = bloomFilterManager.createFilter(name, config); } return HttpResponse.of( createResult.isCreated() ? HttpStatus.CREATED : HttpStatus.OK, MediaType.JSON_UTF_8, MAPPER.valueToTree(createResult.getFilter()).toString()); } 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 testForceCreateFilter() throws Exception { final int validPeriodAfterCreate = 1000; final int validPeriodAfterAccess = 100; final int expectedInsertions = 1000000; final double fpp = 0.0001; final ObjectNode request = mapper.createObjectNode(); request.put("validPeriodAfterCreate", validPeriodAfterCreate); request.put("validPeriodAfterAccess", validPeriodAfterAccess); request.put("fpp", fpp); request.put("expectedInsertions", expectedInsertions); request.put("overwrite", true); final ExpirableBloomFilterConfig expectConfig = new ExpirableBloomFilterConfig(expectedInsertions, fpp); expectConfig.setValidPeriodAfterCreate(Duration.ofSeconds(validPeriodAfterCreate)); expectConfig.setValidPeriodAfterAccess(Duration.ofSeconds(validPeriodAfterAccess)); final GuavaBloomFilter expectedFilter = factory.createFilter(expectConfig); final CreateFilterResult<GuavaBloomFilter> result = new CreateFilterResult<>(expectedFilter, true); when(mockedManager.createFilter(testingFilterName, expectConfig, true)).thenReturn(result); final AggregatedHttpResponse response = service.create(testingFilterName, request).aggregate().get(); assertThat(response.status().code()).isEqualTo(HttpStatus.CREATED.code()); final JsonNode responseInJson = mapper.readTree(response.content(StandardCharsets.UTF_8)); final GuavaBloomFilter filter = new ObjectMapper() .readerFor(GuavaBloomFilter.class) .readValue(responseInJson); assertThat(filter).isNotNull().isEqualTo(expectedFilter); }
@Test public void testCreateAlreadyExistsFilter() throws Exception { final ObjectNode request = mapper.createObjectNode(); final ExpirableBloomFilterConfig expectConfig = new ExpirableBloomFilterConfig(); final GuavaBloomFilter expectedFilter = factory.createFilter(expectConfig); final CreateFilterResult<GuavaBloomFilter> result = new CreateFilterResult<>(expectedFilter, false); when(mockedManager.createFilter(testingFilterName, expectConfig)).thenReturn(result); final AggregatedHttpResponse response = service.create(testingFilterName, request).aggregate().get(); assertThat(response.status().code()).isEqualTo(HttpStatus.OK.code()); final JsonNode responseInJson = mapper.readTree(response.content(StandardCharsets.UTF_8)); final GuavaBloomFilter filter = new ObjectMapper() .readerFor(GuavaBloomFilter.class) .readValue(responseInJson); assertThat(filter).isNotNull().isEqualTo(expectedFilter); }