Search in sources :

Example 1 with ApiResponse

use of net.ripe.rpki.validator3.api.ApiResponse in project rpki-validator-3 by RIPE-NCC.

the class BgpPreviewController method list.

@GetMapping(path = "/")
public ResponseEntity<ApiResponse<Stream<BgpPreview>>> list(@RequestParam(name = "startFrom", defaultValue = "0") int startFrom, @RequestParam(name = "pageSize", defaultValue = "20") int pageSize, @RequestParam(name = "search", defaultValue = "", required = false) String searchString, @RequestParam(name = "sortBy", defaultValue = "prefix") String sortBy, @RequestParam(name = "sortDirection", defaultValue = "asc") String sortDirection) {
    final SearchTerm searchTerm = StringUtils.isNotBlank(searchString) ? new SearchTerm(searchString) : null;
    final Sorting sorting = Sorting.parse(sortBy, sortDirection);
    final Paging paging = Paging.of(startFrom, pageSize);
    BgpPreviewService.BgpPreviewResult bgpPreviewResult = bgpPreviewService.find(searchTerm, sorting, paging);
    return ResponseEntity.ok(ApiResponse.<Stream<BgpPreview>>builder().data(bgpPreviewResult.getData().map(entry -> BgpPreview.of(entry.getOrigin().toString(), entry.getPrefix().toString(), entry.getValidity().name()))).metadata(Metadata.of(bgpPreviewResult.getTotalCount())).build());
}
Also used : Paging(net.ripe.rpki.validator3.api.Paging) Stream(java.util.stream.Stream) SearchTerm(net.ripe.rpki.validator3.api.SearchTerm) Sorting(net.ripe.rpki.validator3.api.Sorting) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with ApiResponse

use of net.ripe.rpki.validator3.api.ApiResponse in project rpki-validator-3 by RIPE-NCC.

the class RoaPrefixAssertionsController method add.

@PostMapping(consumes = { Api.API_MIME_TYPE, "application/json" })
public ResponseEntity<ApiResponse<RoaPrefixAssertionResource>> add(@RequestBody @Valid ApiCommand<AddRoaPrefixAssertion> command) {
    final long id = roaPrefixAssertionsService.execute(command.getData());
    final RoaPrefixAssertion ignoreFilter = roaPrefixAssertions.get(id);
    final Link selfRel = linkTo(methodOn(RoaPrefixAssertionsController.class).get(id)).withSelfRel();
    return ResponseEntity.created(URI.create(selfRel.getHref())).body(ApiResponse.data(toResource(ignoreFilter)));
}
Also used : RoaPrefixAssertion(net.ripe.rpki.validator3.domain.RoaPrefixAssertion) Link(org.springframework.hateoas.Link) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 3 with ApiResponse

use of net.ripe.rpki.validator3.api.ApiResponse in project rpki-validator-3 by RIPE-NCC.

the class RoaPrefixAssertionsController method list.

@GetMapping
public ResponseEntity<ApiResponse<Stream<RoaPrefixAssertionResource>>> list(@RequestParam(name = "startFrom", defaultValue = "0") int startFrom, @RequestParam(name = "pageSize", defaultValue = "20") int pageSize, @RequestParam(name = "search", defaultValue = "", required = false) String searchString, @RequestParam(name = "sortBy", defaultValue = "prefix") String sortBy, @RequestParam(name = "sortDirection", defaultValue = "asc") String sortDirection) {
    final SearchTerm searchTerm = StringUtils.isNotBlank(searchString) ? new SearchTerm(searchString) : null;
    final Sorting sorting = Sorting.parse(sortBy, sortDirection);
    final Paging paging = Paging.of(startFrom, pageSize);
    final List<RoaPrefixAssertion> matching = roaPrefixAssertions.find(searchTerm, sorting, paging).collect(Collectors.toList());
    int totalSize = (int) roaPrefixAssertions.count(searchTerm);
    final Links links = Paging.links(startFrom, pageSize, totalSize, (sf, ps) -> methodOn(RoaPrefixAssertionsController.class).list(sf, ps, searchString, sortBy, sortDirection));
    return ResponseEntity.ok(ApiResponse.<Stream<RoaPrefixAssertionResource>>builder().links(links).metadata(Metadata.of(totalSize)).data(matching.stream().map(this::toResource)).build());
}
Also used : RoaPrefixAssertion(net.ripe.rpki.validator3.domain.RoaPrefixAssertion) Paging(net.ripe.rpki.validator3.api.Paging) Links(org.springframework.hateoas.Links) SearchTerm(net.ripe.rpki.validator3.api.SearchTerm) Sorting(net.ripe.rpki.validator3.api.Sorting) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 4 with ApiResponse

use of net.ripe.rpki.validator3.api.ApiResponse in project rpki-validator-3 by RIPE-NCC.

the class TrustAnchorControllerTest method should_fail_on_invalid_request.

@Test
public void should_fail_on_invalid_request() throws Exception {
    ResultActions result = mvc.perform(post("/api/trust-anchors").accept(Api.API_MIME_TYPE).contentType(Api.API_MIME_TYPE).content(objectMapper.writeValueAsString(ApiCommand.of(AddTrustAnchor.builder().type(TrustAnchor.TYPE).name(TEST_CA_NAME).locations(Arrays.asList("invalid-location")).subjectPublicKeyInfo("public key info too short").build()))));
    result.andExpect(status().isBadRequest()).andExpect(content().contentType(Api.API_MIME_TYPE));
    ApiResponse<TrustAnchorResource> response = addTrustAnchorResponse(result);
    assertThat(response.getErrors()).isNotEmpty();
}
Also used : ResultActions(org.springframework.test.web.servlet.ResultActions) Test(org.junit.Test) IntegrationTest(net.ripe.rpki.validator3.IntegrationTest)

Example 5 with ApiResponse

use of net.ripe.rpki.validator3.api.ApiResponse in project rpki-validator-3 by RIPE-NCC.

the class TrustAnchorControllerTest method should_add_trust_anchor.

@Test
public void should_add_trust_anchor() throws Exception {
    ResultActions result = mvc.perform(post("/api/trust-anchors").accept(Api.API_MIME_TYPE).contentType(Api.API_MIME_TYPE).content(objectMapper.writeValueAsString(ApiCommand.of(AddTrustAnchor.builder().type(TrustAnchor.TYPE).name(TEST_CA_NAME).locations(Arrays.asList("rsync://example.com/rpki")).subjectPublicKeyInfo("jdfakljkldf;adsfjkdsfkl;nasdjfnsldajfklsd;ajfk;ljdsakjfkla;sdhfkjdshfkljadsl;kjfdklfjdaksl;jdfkl;jafkldjsfkl;adjsfkl;adjsf;lkjkl;dj;adskjfdljadjbkfbkjblafkjdfbasfjlka").build()))));
    result.andExpect(status().isCreated()).andExpect(content().contentType(Api.API_MIME_TYPE));
    ApiResponse<TrustAnchorResource> response = addTrustAnchorResponse(result);
    assertThat(response.getData()).isNotNull();
    TrustAnchorResource resource = response.getData();
    Link selfRel = resource.getLinks().getLink("self");
    mvc.perform(get(selfRel.getHref()).accept(Api.API_MIME_TYPE)).andExpect(status().isOk()).andExpect(content().contentType(Api.API_MIME_TYPE)).andExpect(jsonPath("$.data.name").value(TEST_CA_NAME));
}
Also used : ResultActions(org.springframework.test.web.servlet.ResultActions) Link(org.springframework.hateoas.Link) Test(org.junit.Test) IntegrationTest(net.ripe.rpki.validator3.IntegrationTest)

Aggregations

GetMapping (org.springframework.web.bind.annotation.GetMapping)9 Paging (net.ripe.rpki.validator3.api.Paging)7 SearchTerm (net.ripe.rpki.validator3.api.SearchTerm)7 Sorting (net.ripe.rpki.validator3.api.Sorting)7 Links (org.springframework.hateoas.Links)7 Link (org.springframework.hateoas.Link)5 Stream (java.util.stream.Stream)4 TrustAnchor (net.ripe.rpki.validator3.domain.TrustAnchor)4 PostMapping (org.springframework.web.bind.annotation.PostMapping)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Slf4j (lombok.extern.slf4j.Slf4j)3 Api (net.ripe.rpki.validator3.api.Api)3 ApiResponse (net.ripe.rpki.validator3.api.ApiResponse)3 URI (java.net.URI)2 Locale (java.util.Locale)2 Valid (javax.validation.Valid)2 IntegrationTest (net.ripe.rpki.validator3.IntegrationTest)2 ApiCommand (net.ripe.rpki.validator3.api.ApiCommand)2 ApiError (net.ripe.rpki.validator3.api.ApiError)2