EntryPointResources { @DELETE @Path("/{id : .+}") @Timed public Response deleteEntrypoint(@Auth final User user, @PathParam("id") String id) { final Response response; EntryPointKeyVsctImpl entryPointKey = EntryPointKeyVsctImpl.fromID(id); Optional<EntryPoint> configuration = repository.getCurrentConfiguration(entryPointKey); if (configuration.isPresent()) { Optional<Boolean> removed = repository.removeEntrypoint(entryPointKey); if (removed.isPresent()) { if (removed.get()) { Set<String> entryPointsId = repository.getEntryPointsId(); response = ok().entity(entryPointsId).build(); deleteEntryPointSubscriber.onNext(new DeleteEntryPointEvent(UUID.randomUUID().toString(), entryPointKey, configuration.get().getHaproxy(), entryPointKey.getApplication(), entryPointKey.getPlatform())); } else { Set<String> entryPointsId = repository.getEntryPointsId(); LOGGER.warn("can't removed an entrypoint though its configuration has just been found. May be there are concurrency problem, admin or/and repository are overloaded."); response = status(NOT_FOUND).entity(entryPointsId).build(); } } else { Set<String> entryPointsId = repository.getEntryPointsId(); response = serverError().entity(entryPointsId).build(); } } else { Set<String> entryPointsId = repository.getEntryPointsId(); LOGGER.warn("can't find entrypoint {} from repository", entryPointKey); response = status(NOT_FOUND).entity(entryPointsId).build(); } return response; } EntryPointResources(EntryPointRepository repository, HaproxyResources haproxyResources,
                               Subscriber<AutoReloadConfigEvent> autoReloadConfigSubscriber,
                               Subscriber<AddEntryPointEvent> addEntryPointSubscriber,
                               Subscriber<UpdateEntryPointEvent> updateEntryPointSubscriber,
                               Subscriber<DeleteEntryPointEvent> deleteEntryPointSubscriber,
                               Subscriber<TryCommitPendingConfigurationEvent> tryCommitPendingConfigurationSubscriber,
                               Subscriber<TryCommitCurrentConfigurationEvent> tryCommitCurrentConfigurationSubscriber,
                               Subscriber<RegisterServerEvent> registerServerSubscriber,
                               Subscriber<CommitCompletedEvent> commitSuccessSubscriber,
                               Subscriber<CommitFailedEvent> commitFailureSubscriber); @GET @Timed Set<String> getEntryPoints(@Auth final User user); @GET @Path("/{id : .+}/autoreload") Boolean isAutoreloaded(@Auth final User user, @PathParam("id") String entryPointKey); @PATCH @Path("/{id : .+}/autoreload/swap") void swapAutoReload(@Auth final User user, @Suspended AsyncResponse asyncResponse, @PathParam("id") String key); @PUT @Path("/{id : .+}") @Timed void addEntryPoint(@Auth final User user, @Suspended AsyncResponse asyncResponse, @PathParam("id") String key, @Valid EntryPointMappingJson configuration); @PATCH @Path("/{id : .+}") @Timed void updateEntryPoint(@Auth final User user, @Suspended AsyncResponse asyncResponse, @PathParam("id") String id, @Valid UpdatedEntryPointMappingJson updatedConfiguration); @GET @Path("/{id : .+}/current") @Timed EntryPointMappingJson getCurrent(@Auth final User user, @PathParam("id") String id); @DELETE @Path("/{id : .+}") @Timed Response deleteEntrypoint(@Auth final User user, @PathParam("id") String id); @GET @Path("/{id : .+}/pending") @Timed EntryPointMappingJson getPending(@Auth final User user, @PathParam("id") String id); @GET @Path("/{id : .+}/committing") @Timed EntryPointMappingJson getCommitting(@Auth final User user, @PathParam("id") String id); @POST @Path("/{id : .+}/try-commit-current") @Produces(MediaType.TEXT_PLAIN) String tryCommitCurrent(@Auth final User user, @PathParam("id") String id); @POST @Path("/{id : .+}/try-commit-pending") @Produces(MediaType.TEXT_PLAIN) String tryCommitPending(@Auth final User user, @PathParam("id") String id); @POST @Path("/{id : .+}/backend/{backend}/register-server") @Produces(MediaType.TEXT_PLAIN) String registerServer( @Auth final User user, 
    							@PathParam("id") String id,
                                 @PathParam("backend") String backend,
                                 IncomingEntryPointBackendServerJsonRepresentation serverJson); @POST @Path("/{id : .+}/send-commit-success/{correlationId}") @Produces(MediaType.TEXT_PLAIN) String sendCommitSuccess(@Auth final User user, 
    									@PathParam("id") String id, @PathParam("correlationId") String correlationId); @POST @Path("/{id : .+}/send-commit-failure/{correlationId}") @Produces(MediaType.TEXT_PLAIN) String sendCommitFailure(@Auth final User user,
    									@PathParam("id") String id, @PathParam("correlationId") String correlationId);  }
@Test public void should_send_delete_entry_point_event_and_return_204_when_delete_an_entry_point() { when(entryPointRepository.removeEntrypoint(any(EntryPointKey.class))).thenReturn(Optional.of(Boolean.TRUE)); when(entryPointRepository.getCurrentConfiguration(any(EntryPointKey.class))).thenReturn(Optional.of(new EntryPoint("default-name", "hapadm", "hapVersion", 0, new HashSet<>(), new HashSet<>(), new HashMap<>()))); Response response = entryPointResources.deleteEntrypoint(USER_PROD, "MY_APP/MY_PLTF"); assertThat(response.getStatus()).isEqualTo(OK.getStatusCode()); verify(deleteEntryPointSubscriber).onNext(any(DeleteEntryPointEvent.class)); verify(entryPointRepository, times(1)).getEntryPointsId(); }
@Test public void should_not_post_delete_event_and_return_404_when_delete_an_non_existing_entry_point() { when(entryPointRepository.getCurrentConfiguration(any(EntryPointKey.class))).thenReturn(Optional.of(new EntryPoint("default-name", "hapadm", "hapVersion", 0, new HashSet<>(), new HashSet<>(), new HashMap<>()))); when(entryPointRepository.getCurrentConfiguration(any(EntryPointKey.class))).thenReturn(Optional.empty()); Response response = entryPointResources.deleteEntrypoint(USER_PROD, "MY_APP/MY_PLTF"); assertThat(response.getStatus()).isEqualTo(NOT_FOUND.getStatusCode()); verify(deleteEntryPointSubscriber, never()).onNext(any(DeleteEntryPointEvent.class)); verify(entryPointRepository, times(1)).getEntryPointsId(); }
@Test public void should_return_500_when_repository_cannot_remove_entry_point_delete_entry_point() { when(entryPointRepository.removeEntrypoint(any(EntryPointKey.class))).thenReturn(Optional.empty()); when(entryPointRepository.getCurrentConfiguration(any(EntryPointKey.class))).thenReturn(Optional.of(new EntryPoint("default-name", "hapadm", "hapVersion", 0, new HashSet<>(), new HashSet<>(), new HashMap<>()))); Response response = entryPointResources.deleteEntrypoint(USER_PROD, "MY_APP/MY_PLTF"); assertThat(response.getStatus()).isEqualTo(INTERNAL_SERVER_ERROR.getStatusCode()); verify(entryPointRepository, times(1)).getEntryPointsId(); }