@Test public void exportResources() throws IOException { final DataManager dataManager = mock(DataManager.class); final Environment env = newEnvironment("env"); final String envId = env.getId().get(); final Integration integration1 = new Integration.Builder() .id("integration1") .putContinuousDeliveryState(envId, new ContinuousDeliveryEnvironment.Builder().build()) .build(); final Integration integration2 = new Integration.Builder() .id("integration2") .putContinuousDeliveryState(envId, new ContinuousDeliveryEnvironment.Builder().build()) .putContinuousDeliveryState("different-env", new ContinuousDeliveryEnvironment.Builder().build()) .build(); @SuppressWarnings({"unchecked", "rawtypes"}) final Class<Function<ListResult<Integration>, ListResult<Integration>>[]> filterVarArgsType = (Class) Function[].class; final ArgumentCaptor<Function<ListResult<Integration>, ListResult<Integration>>[]> filter = ArgumentCaptor.forClass(filterVarArgsType); when(dataManager.fetchByPropertyValue(Environment.class, "name", "env")).thenReturn(Optional.of(env)); when(dataManager.fetchAll(eq(Integration.class), filter.capture())).thenReturn(ListResult.of(integration1, integration2)); final StreamingOutput someStream = mock(StreamingOutput.class); final IntegrationSupportHandler integrationSupportHandler = mock(IntegrationSupportHandler.class); when(integrationSupportHandler.export(Arrays.asList("integration1", "integration2"))).thenReturn(someStream); final EnvironmentHandler environmentHandler = new EnvironmentHandler(dataManager); final PublicApiHandler handler = new PublicApiHandler(dataManager, null, null, null, null, environmentHandler, integrationSupportHandler, null); try (Response response = handler.exportResources("env", false, false)) { assertThat(response).isNotNull(); assertThat(response.getStatusInfo().toEnum()).isEqualTo(Status.OK); assertThat(response.getEntity()).isSameAs(someStream); } final Integration integration3 = new Integration.Builder() .id("integration3") .putContinuousDeliveryState("different-env", new ContinuousDeliveryEnvironment.Builder().build()) .build(); final List<Function<ListResult<Integration>, ListResult<Integration>>[]> filters = filter.getAllValues(); assertThat(filters).hasSize(1).as("Varargs capture should give us single Function").hasOnlyElementsOfType(Function.class); @SuppressWarnings("unchecked") final Function<ListResult<Integration>, ListResult<Integration>> filterFunction = (Function<ListResult<Integration>, ListResult<Integration>>) (Object) filters .get(0); assertThat(filterFunction.apply(ListResult.of(integration1, integration2, integration3))).isEqualTo(ListResult.of(integration1, integration2)); try (Response response = handler.exportResources("env", false, true)) { assertThat(response).isNotNull(); assertThat(response.getStatusInfo().toEnum()).isEqualTo(Status.OK); assertThat(response.getEntity()).isSameAs(someStream); } verify(dataManager, times(2)).update(withLastExportedTimestamp(integration1, envId)); verify(dataManager, times(2)).update(withLastExportedTimestamp(integration2, envId)); }
@GET @Path("integrations/{env}/export.zip") @Produces(MediaType.APPLICATION_OCTET_STREAM) @SuppressWarnings("JdkObsolete") public Response exportResources(@NotNull @PathParam("env") @Parameter(required = true) String environment, @QueryParam("all") @Parameter boolean exportAll, @QueryParam("ignoreTimestamp") @Parameter boolean ignoreTimestamp) throws IOException { EnvironmentHandler.validateEnvironment("environment", environment); final Environment env = environmentHandler.getEnvironment(environment); final ListResult<Integration> integrations; final String envId = env.getId().get(); if (exportAll) { integrations = dataMgr.fetchAll(Integration.class); @SuppressWarnings("JdkObsolete") Date taggedAt = new Date(); integrations.getItems().forEach(i -> { final HashMap<String, ContinuousDeliveryEnvironment> state = new HashMap<>(i.getContinuousDeliveryState()); EnvironmentHandler.createOrUpdateTag(state, envId, taggedAt); dataMgr.update(i.builder().continuousDeliveryState(state).build()); }); LOG.debug("Exporting ALL ({}) integrations for environment {} at {} ...", integrations.getItems().size(), environment, taggedAt); } else { integrations = dataMgr.fetchAll(Integration.class, listResult -> listResult.getItems().stream().filter(i -> { boolean result = ignoreTimestamp; final Map<String, ContinuousDeliveryEnvironment> map = i.getContinuousDeliveryState(); final ContinuousDeliveryEnvironment cdEnv = map.get(envId); if (!ignoreTimestamp && cdEnv != null) { final Date taggedAt = cdEnv.getLastTaggedAt(); final Date exportedAt = cdEnv.getLastExportedAt().orElse(null); result = exportedAt == null || exportedAt.before(taggedAt); } return result; }).collect(ListResult.collector()).build()); LOG.debug("Exporting ({}) integrations for environment {} ...", integrations.getItems().size(), environment); } final List<String> ids = integrations.getItems().stream() .map(integration -> integration.getId().get()) .collect(Collectors.toList()); if (ids.isEmpty()) { return Response.status(Response.Status.NO_CONTENT.getStatusCode(), "No integrations to export").build(); } @SuppressWarnings("JdkObsolete") final Date exportedAt = new Date(); final StreamingOutput output = handler.export(ids); environmentHandler.updateCDEnvironments(integrations.getItems(), envId, exportedAt, b -> b.lastExportedAt(exportedAt)); LOG.debug("Exported ({}) integrations for environment {}", ids.size(), environment); return Response.ok(output).build(); }
PublicApiHandler { @GET @Path("integrations/{env}/export.zip") @Produces(MediaType.APPLICATION_OCTET_STREAM) @SuppressWarnings("JdkObsolete") public Response exportResources(@NotNull @PathParam("env") @Parameter(required = true) String environment, @QueryParam("all") @Parameter boolean exportAll, @QueryParam("ignoreTimestamp") @Parameter boolean ignoreTimestamp) throws IOException { EnvironmentHandler.validateEnvironment("environment", environment); final Environment env = environmentHandler.getEnvironment(environment); final ListResult<Integration> integrations; final String envId = env.getId().get(); if (exportAll) { integrations = dataMgr.fetchAll(Integration.class); @SuppressWarnings("JdkObsolete") Date taggedAt = new Date(); integrations.getItems().forEach(i -> { final HashMap<String, ContinuousDeliveryEnvironment> state = new HashMap<>(i.getContinuousDeliveryState()); EnvironmentHandler.createOrUpdateTag(state, envId, taggedAt); dataMgr.update(i.builder().continuousDeliveryState(state).build()); }); LOG.debug("Exporting ALL ({}) integrations for environment {} at {} ...", integrations.getItems().size(), environment, taggedAt); } else { integrations = dataMgr.fetchAll(Integration.class, listResult -> listResult.getItems().stream().filter(i -> { boolean result = ignoreTimestamp; final Map<String, ContinuousDeliveryEnvironment> map = i.getContinuousDeliveryState(); final ContinuousDeliveryEnvironment cdEnv = map.get(envId); if (!ignoreTimestamp && cdEnv != null) { final Date taggedAt = cdEnv.getLastTaggedAt(); final Date exportedAt = cdEnv.getLastExportedAt().orElse(null); result = exportedAt == null || exportedAt.before(taggedAt); } return result; }).collect(ListResult.collector()).build()); LOG.debug("Exporting ({}) integrations for environment {} ...", integrations.getItems().size(), environment); } final List<String> ids = integrations.getItems().stream() .map(integration -> integration.getId().get()) .collect(Collectors.toList()); if (ids.isEmpty()) { return Response.status(Response.Status.NO_CONTENT.getStatusCode(), "No integrations to export").build(); } @SuppressWarnings("JdkObsolete") final Date exportedAt = new Date(); final StreamingOutput output = handler.export(ids); environmentHandler.updateCDEnvironments(integrations.getItems(), envId, exportedAt, b -> b.lastExportedAt(exportedAt)); LOG.debug("Exported ({}) integrations for environment {}", ids.size(), environment); return Response.ok(output).build(); } }
PublicApiHandler { @GET @Path("integrations/{env}/export.zip") @Produces(MediaType.APPLICATION_OCTET_STREAM) @SuppressWarnings("JdkObsolete") public Response exportResources(@NotNull @PathParam("env") @Parameter(required = true) String environment, @QueryParam("all") @Parameter boolean exportAll, @QueryParam("ignoreTimestamp") @Parameter boolean ignoreTimestamp) throws IOException { EnvironmentHandler.validateEnvironment("environment", environment); final Environment env = environmentHandler.getEnvironment(environment); final ListResult<Integration> integrations; final String envId = env.getId().get(); if (exportAll) { integrations = dataMgr.fetchAll(Integration.class); @SuppressWarnings("JdkObsolete") Date taggedAt = new Date(); integrations.getItems().forEach(i -> { final HashMap<String, ContinuousDeliveryEnvironment> state = new HashMap<>(i.getContinuousDeliveryState()); EnvironmentHandler.createOrUpdateTag(state, envId, taggedAt); dataMgr.update(i.builder().continuousDeliveryState(state).build()); }); LOG.debug("Exporting ALL ({}) integrations for environment {} at {} ...", integrations.getItems().size(), environment, taggedAt); } else { integrations = dataMgr.fetchAll(Integration.class, listResult -> listResult.getItems().stream().filter(i -> { boolean result = ignoreTimestamp; final Map<String, ContinuousDeliveryEnvironment> map = i.getContinuousDeliveryState(); final ContinuousDeliveryEnvironment cdEnv = map.get(envId); if (!ignoreTimestamp && cdEnv != null) { final Date taggedAt = cdEnv.getLastTaggedAt(); final Date exportedAt = cdEnv.getLastExportedAt().orElse(null); result = exportedAt == null || exportedAt.before(taggedAt); } return result; }).collect(ListResult.collector()).build()); LOG.debug("Exporting ({}) integrations for environment {} ...", integrations.getItems().size(), environment); } final List<String> ids = integrations.getItems().stream() .map(integration -> integration.getId().get()) .collect(Collectors.toList()); if (ids.isEmpty()) { return Response.status(Response.Status.NO_CONTENT.getStatusCode(), "No integrations to export").build(); } @SuppressWarnings("JdkObsolete") final Date exportedAt = new Date(); final StreamingOutput output = handler.export(ids); environmentHandler.updateCDEnvironments(integrations.getItems(), envId, exportedAt, b -> b.lastExportedAt(exportedAt)); LOG.debug("Exported ({}) integrations for environment {}", ids.size(), environment); return Response.ok(output).build(); } protected  PublicApiHandler(DataManager dataMgr, EncryptionComponent encryptionComponent,
        IntegrationDeploymentHandler deploymentHandler, ConnectionHandler connectionHandler,
        MonitoringProvider monitoringProvider, EnvironmentHandler environmentHandler,
        IntegrationSupportHandler handler, IntegrationHandler integrationHandler); }
PublicApiHandler { @GET @Path("integrations/{env}/export.zip") @Produces(MediaType.APPLICATION_OCTET_STREAM) @SuppressWarnings("JdkObsolete") public Response exportResources(@NotNull @PathParam("env") @Parameter(required = true) String environment, @QueryParam("all") @Parameter boolean exportAll, @QueryParam("ignoreTimestamp") @Parameter boolean ignoreTimestamp) throws IOException { EnvironmentHandler.validateEnvironment("environment", environment); final Environment env = environmentHandler.getEnvironment(environment); final ListResult<Integration> integrations; final String envId = env.getId().get(); if (exportAll) { integrations = dataMgr.fetchAll(Integration.class); @SuppressWarnings("JdkObsolete") Date taggedAt = new Date(); integrations.getItems().forEach(i -> { final HashMap<String, ContinuousDeliveryEnvironment> state = new HashMap<>(i.getContinuousDeliveryState()); EnvironmentHandler.createOrUpdateTag(state, envId, taggedAt); dataMgr.update(i.builder().continuousDeliveryState(state).build()); }); LOG.debug("Exporting ALL ({}) integrations for environment {} at {} ...", integrations.getItems().size(), environment, taggedAt); } else { integrations = dataMgr.fetchAll(Integration.class, listResult -> listResult.getItems().stream().filter(i -> { boolean result = ignoreTimestamp; final Map<String, ContinuousDeliveryEnvironment> map = i.getContinuousDeliveryState(); final ContinuousDeliveryEnvironment cdEnv = map.get(envId); if (!ignoreTimestamp && cdEnv != null) { final Date taggedAt = cdEnv.getLastTaggedAt(); final Date exportedAt = cdEnv.getLastExportedAt().orElse(null); result = exportedAt == null || exportedAt.before(taggedAt); } return result; }).collect(ListResult.collector()).build()); LOG.debug("Exporting ({}) integrations for environment {} ...", integrations.getItems().size(), environment); } final List<String> ids = integrations.getItems().stream() .map(integration -> integration.getId().get()) .collect(Collectors.toList()); if (ids.isEmpty()) { return Response.status(Response.Status.NO_CONTENT.getStatusCode(), "No integrations to export").build(); } @SuppressWarnings("JdkObsolete") final Date exportedAt = new Date(); final StreamingOutput output = handler.export(ids); environmentHandler.updateCDEnvironments(integrations.getItems(), envId, exportedAt, b -> b.lastExportedAt(exportedAt)); LOG.debug("Exported ({}) integrations for environment {}", ids.size(), environment); return Response.ok(output).build(); } protected  PublicApiHandler(DataManager dataMgr, EncryptionComponent encryptionComponent,
        IntegrationDeploymentHandler deploymentHandler, ConnectionHandler connectionHandler,
        MonitoringProvider monitoringProvider, EnvironmentHandler environmentHandler,
        IntegrationSupportHandler handler, IntegrationHandler integrationHandler); List<String> getReleaseEnvironments(); @GET @Path("environments") @Produces(MediaType.APPLICATION_JSON) Response getReleaseEnvironments(@QueryParam("withUses") @Parameter boolean withUses); @POST @Path("environments/{env}") void addNewEnvironment(@NotNull @PathParam("env") @Parameter(required = true) String environment); @DELETE @Path("environments/{env}") void deleteEnvironment(@NotNull @PathParam("env") @Parameter(required = true) String environment); @PUT @Path("environments/{env}") @Consumes(MediaType.APPLICATION_JSON) void renameEnvironment(@NotNull @PathParam("env") @Parameter(required = true) String environment,
        @NotNull @Parameter(required = true) String newEnvironment); @GET @Path("integrations/{id}/tags") @Produces(MediaType.APPLICATION_JSON) Map<String, ContinuousDeliveryEnvironment> getReleaseTags(@NotNull @PathParam("id") @Parameter(required = true) String integrationId); @DELETE @Path("integrations/{id}/tags/{env}") void deleteReleaseTag(@NotNull @PathParam("id") @Parameter(required = true) String integrationId,
        @NotNull @PathParam("env") @Parameter(required = true) String environment); @PUT @Path("integrations/{id}/tags") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) Map<String, ContinuousDeliveryEnvironment> putTagsForRelease(@NotNull @PathParam("id") @Parameter(required = true) String integrationId,
        @NotNull @Parameter(required = true) List<String> environments); @PATCH @Path("integrations/{id}/tags") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) Map<String, ContinuousDeliveryEnvironment> patchTagsForRelease(@NotNull @PathParam("id") @Parameter(required = true) String integrationId,
        @NotNull @Parameter(required = true) List<String> environments); @GET @Path("integrations/{env}/export.zip") @Produces(MediaType.APPLICATION_OCTET_STREAM) @SuppressWarnings("JdkObsolete") Response exportResources(@NotNull @PathParam("env") @Parameter(required = true) String environment,
                                    @QueryParam("all") @Parameter boolean exportAll,
                                    @QueryParam("ignoreTimestamp") @Parameter boolean ignoreTimestamp); @POST @Path("integrations") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.MULTIPART_FORM_DATA) @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE") // https://github.com/spotbugs/spotbugs/issues/259 ContinuousDeliveryImportResults importResources(@Context SecurityContext sec,
        @NotNull @MultipartForm @Parameter(required = true) ImportFormDataInput formInput); @POST @Path("connections/{id}/properties") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) ConnectionOverview configureConnection(@Context SecurityContext sec,
                                                  @NotNull @PathParam("id") @Parameter(required = true) String connectionId,
                                                  @NotNull @QueryParam("refreshIntegrations") @Parameter Boolean refeshIntegrations,
                                                  @NotNull @Parameter(required = true) Map<String, String> properties); @GET @Path("integrations/{id}/state") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) IntegrationState getIntegrationState(@Context SecurityContext sec, @NotNull @PathParam("id") @Parameter(required = true) String integrationId); @POST @Path("integrations/{id}/deployments") @Produces(MediaType.APPLICATION_JSON) IntegrationDeployment publishIntegration(@Context final SecurityContext sec,
        @NotNull @PathParam("id") @Parameter(required = true) final String integrationId); @PUT @Path("integrations/{id}/deployments/stop") @Produces(MediaType.APPLICATION_JSON) void stopIntegration(@Context final SecurityContext sec, @NotNull @PathParam("id") @Parameter(required = true) final String integrationId); }
PublicApiHandler { @GET @Path("integrations/{env}/export.zip") @Produces(MediaType.APPLICATION_OCTET_STREAM) @SuppressWarnings("JdkObsolete") public Response exportResources(@NotNull @PathParam("env") @Parameter(required = true) String environment, @QueryParam("all") @Parameter boolean exportAll, @QueryParam("ignoreTimestamp") @Parameter boolean ignoreTimestamp) throws IOException { EnvironmentHandler.validateEnvironment("environment", environment); final Environment env = environmentHandler.getEnvironment(environment); final ListResult<Integration> integrations; final String envId = env.getId().get(); if (exportAll) { integrations = dataMgr.fetchAll(Integration.class); @SuppressWarnings("JdkObsolete") Date taggedAt = new Date(); integrations.getItems().forEach(i -> { final HashMap<String, ContinuousDeliveryEnvironment> state = new HashMap<>(i.getContinuousDeliveryState()); EnvironmentHandler.createOrUpdateTag(state, envId, taggedAt); dataMgr.update(i.builder().continuousDeliveryState(state).build()); }); LOG.debug("Exporting ALL ({}) integrations for environment {} at {} ...", integrations.getItems().size(), environment, taggedAt); } else { integrations = dataMgr.fetchAll(Integration.class, listResult -> listResult.getItems().stream().filter(i -> { boolean result = ignoreTimestamp; final Map<String, ContinuousDeliveryEnvironment> map = i.getContinuousDeliveryState(); final ContinuousDeliveryEnvironment cdEnv = map.get(envId); if (!ignoreTimestamp && cdEnv != null) { final Date taggedAt = cdEnv.getLastTaggedAt(); final Date exportedAt = cdEnv.getLastExportedAt().orElse(null); result = exportedAt == null || exportedAt.before(taggedAt); } return result; }).collect(ListResult.collector()).build()); LOG.debug("Exporting ({}) integrations for environment {} ...", integrations.getItems().size(), environment); } final List<String> ids = integrations.getItems().stream() .map(integration -> integration.getId().get()) .collect(Collectors.toList()); if (ids.isEmpty()) { return Response.status(Response.Status.NO_CONTENT.getStatusCode(), "No integrations to export").build(); } @SuppressWarnings("JdkObsolete") final Date exportedAt = new Date(); final StreamingOutput output = handler.export(ids); environmentHandler.updateCDEnvironments(integrations.getItems(), envId, exportedAt, b -> b.lastExportedAt(exportedAt)); LOG.debug("Exported ({}) integrations for environment {}", ids.size(), environment); return Response.ok(output).build(); } protected  PublicApiHandler(DataManager dataMgr, EncryptionComponent encryptionComponent,
        IntegrationDeploymentHandler deploymentHandler, ConnectionHandler connectionHandler,
        MonitoringProvider monitoringProvider, EnvironmentHandler environmentHandler,
        IntegrationSupportHandler handler, IntegrationHandler integrationHandler); List<String> getReleaseEnvironments(); @GET @Path("environments") @Produces(MediaType.APPLICATION_JSON) Response getReleaseEnvironments(@QueryParam("withUses") @Parameter boolean withUses); @POST @Path("environments/{env}") void addNewEnvironment(@NotNull @PathParam("env") @Parameter(required = true) String environment); @DELETE @Path("environments/{env}") void deleteEnvironment(@NotNull @PathParam("env") @Parameter(required = true) String environment); @PUT @Path("environments/{env}") @Consumes(MediaType.APPLICATION_JSON) void renameEnvironment(@NotNull @PathParam("env") @Parameter(required = true) String environment,
        @NotNull @Parameter(required = true) String newEnvironment); @GET @Path("integrations/{id}/tags") @Produces(MediaType.APPLICATION_JSON) Map<String, ContinuousDeliveryEnvironment> getReleaseTags(@NotNull @PathParam("id") @Parameter(required = true) String integrationId); @DELETE @Path("integrations/{id}/tags/{env}") void deleteReleaseTag(@NotNull @PathParam("id") @Parameter(required = true) String integrationId,
        @NotNull @PathParam("env") @Parameter(required = true) String environment); @PUT @Path("integrations/{id}/tags") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) Map<String, ContinuousDeliveryEnvironment> putTagsForRelease(@NotNull @PathParam("id") @Parameter(required = true) String integrationId,
        @NotNull @Parameter(required = true) List<String> environments); @PATCH @Path("integrations/{id}/tags") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) Map<String, ContinuousDeliveryEnvironment> patchTagsForRelease(@NotNull @PathParam("id") @Parameter(required = true) String integrationId,
        @NotNull @Parameter(required = true) List<String> environments); @GET @Path("integrations/{env}/export.zip") @Produces(MediaType.APPLICATION_OCTET_STREAM) @SuppressWarnings("JdkObsolete") Response exportResources(@NotNull @PathParam("env") @Parameter(required = true) String environment,
                                    @QueryParam("all") @Parameter boolean exportAll,
                                    @QueryParam("ignoreTimestamp") @Parameter boolean ignoreTimestamp); @POST @Path("integrations") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.MULTIPART_FORM_DATA) @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE") // https://github.com/spotbugs/spotbugs/issues/259 ContinuousDeliveryImportResults importResources(@Context SecurityContext sec,
        @NotNull @MultipartForm @Parameter(required = true) ImportFormDataInput formInput); @POST @Path("connections/{id}/properties") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) ConnectionOverview configureConnection(@Context SecurityContext sec,
                                                  @NotNull @PathParam("id") @Parameter(required = true) String connectionId,
                                                  @NotNull @QueryParam("refreshIntegrations") @Parameter Boolean refeshIntegrations,
                                                  @NotNull @Parameter(required = true) Map<String, String> properties); @GET @Path("integrations/{id}/state") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) IntegrationState getIntegrationState(@Context SecurityContext sec, @NotNull @PathParam("id") @Parameter(required = true) String integrationId); @POST @Path("integrations/{id}/deployments") @Produces(MediaType.APPLICATION_JSON) IntegrationDeployment publishIntegration(@Context final SecurityContext sec,
        @NotNull @PathParam("id") @Parameter(required = true) final String integrationId); @PUT @Path("integrations/{id}/deployments/stop") @Produces(MediaType.APPLICATION_JSON) void stopIntegration(@Context final SecurityContext sec, @NotNull @PathParam("id") @Parameter(required = true) final String integrationId);  }