@Test public void shouldSetVersionTo1ForInitialUpdate() { final SecurityContext security = mock(SecurityContext.class); final Principal principal = mock(Principal.class); when(security.getUserPrincipal()).thenReturn(principal); when(principal.getName()).thenReturn("user"); final Integration integration = new Integration.Builder().build(); when(dataManager.fetch(Integration.class, INTEGRATION_ID)).thenReturn(integration); Map<String, String> labels = new HashMap<>(); List<DeploymentConfig> emptyList = new ArrayList<>(); when(openShiftService.getDeploymentsByLabel(labels)).thenReturn(emptyList); when(dataManager.fetchAllByPropertyValue(IntegrationDeployment.class, "integrationId", INTEGRATION_ID)) .thenReturn(Stream.empty()); handler.update(security, INTEGRATION_ID); final IntegrationDeployment expected = new IntegrationDeployment.Builder().id(compositeId(INTEGRATION_ID, 1)) .spec(integration).version(1).userId("user").build(); verify(dataManager).create(argThat(new ArgumentMatcher<IntegrationDeployment>() { @Override public boolean matches(IntegrationDeployment given) { return expected.builder() .createdAt(given.getCreatedAt()) .updatedAt(given.getUpdatedAt()) .build() .equals(given); } })); }
@PUT @Produces(MediaType.APPLICATION_JSON) public IntegrationDeployment update(@Context final SecurityContext sec, @NotNull @PathParam("id") @Parameter(required = true) final String id) { final DataManager dataManager = getDataManager(); final Integration integration = dataManager.fetch(Integration.class, id); if (integration == null) { throw new EntityNotFoundException(); } Optional<Integer> maybeLatestVersion = dataManager .fetchAllByPropertyValue(IntegrationDeployment.class, "integrationId", id).map(deployment -> { if (deployment.getTargetState() != IntegrationDeploymentState.Unpublished) { dataManager.update(deployment.withTargetState(IntegrationDeploymentState.Unpublished)); } return deployment.getVersion(); }).max(Integer::compareTo); final int version = maybeLatestVersion.map(v -> v + 1).orElse(1); final Optional<String> validationStatus = validateQuotas(id, sec.getUserPrincipal().getName()); final IntegrationDeployment deployment = new IntegrationDeployment.Builder() .id(IntegrationDeployment.compositeId(id, version)).spec(integration).version(version) .userId(sec.getUserPrincipal().getName()) .createdAt(System.currentTimeMillis()) .statusMessage(validationStatus).build(); return getDataManager().create(deployment); }
IntegrationDeploymentHandler extends BaseHandler { @PUT @Produces(MediaType.APPLICATION_JSON) public IntegrationDeployment update(@Context final SecurityContext sec, @NotNull @PathParam("id") @Parameter(required = true) final String id) { final DataManager dataManager = getDataManager(); final Integration integration = dataManager.fetch(Integration.class, id); if (integration == null) { throw new EntityNotFoundException(); } Optional<Integer> maybeLatestVersion = dataManager .fetchAllByPropertyValue(IntegrationDeployment.class, "integrationId", id).map(deployment -> { if (deployment.getTargetState() != IntegrationDeploymentState.Unpublished) { dataManager.update(deployment.withTargetState(IntegrationDeploymentState.Unpublished)); } return deployment.getVersion(); }).max(Integer::compareTo); final int version = maybeLatestVersion.map(v -> v + 1).orElse(1); final Optional<String> validationStatus = validateQuotas(id, sec.getUserPrincipal().getName()); final IntegrationDeployment deployment = new IntegrationDeployment.Builder() .id(IntegrationDeployment.compositeId(id, version)).spec(integration).version(version) .userId(sec.getUserPrincipal().getName()) .createdAt(System.currentTimeMillis()) .statusMessage(validationStatus).build(); return getDataManager().create(deployment); } }
IntegrationDeploymentHandler extends BaseHandler { @PUT @Produces(MediaType.APPLICATION_JSON) public IntegrationDeployment update(@Context final SecurityContext sec, @NotNull @PathParam("id") @Parameter(required = true) final String id) { final DataManager dataManager = getDataManager(); final Integration integration = dataManager.fetch(Integration.class, id); if (integration == null) { throw new EntityNotFoundException(); } Optional<Integer> maybeLatestVersion = dataManager .fetchAllByPropertyValue(IntegrationDeployment.class, "integrationId", id).map(deployment -> { if (deployment.getTargetState() != IntegrationDeploymentState.Unpublished) { dataManager.update(deployment.withTargetState(IntegrationDeploymentState.Unpublished)); } return deployment.getVersion(); }).max(Integer::compareTo); final int version = maybeLatestVersion.map(v -> v + 1).orElse(1); final Optional<String> validationStatus = validateQuotas(id, sec.getUserPrincipal().getName()); final IntegrationDeployment deployment = new IntegrationDeployment.Builder() .id(IntegrationDeployment.compositeId(id, version)).spec(integration).version(version) .userId(sec.getUserPrincipal().getName()) .createdAt(System.currentTimeMillis()) .statusMessage(validationStatus).build(); return getDataManager().create(deployment); } @Autowired  IntegrationDeploymentHandler(final DataManager dataMgr, OpenShiftService openShiftService,
            UserConfigurationProperties properties); }
IntegrationDeploymentHandler extends BaseHandler { @PUT @Produces(MediaType.APPLICATION_JSON) public IntegrationDeployment update(@Context final SecurityContext sec, @NotNull @PathParam("id") @Parameter(required = true) final String id) { final DataManager dataManager = getDataManager(); final Integration integration = dataManager.fetch(Integration.class, id); if (integration == null) { throw new EntityNotFoundException(); } Optional<Integer> maybeLatestVersion = dataManager .fetchAllByPropertyValue(IntegrationDeployment.class, "integrationId", id).map(deployment -> { if (deployment.getTargetState() != IntegrationDeploymentState.Unpublished) { dataManager.update(deployment.withTargetState(IntegrationDeploymentState.Unpublished)); } return deployment.getVersion(); }).max(Integer::compareTo); final int version = maybeLatestVersion.map(v -> v + 1).orElse(1); final Optional<String> validationStatus = validateQuotas(id, sec.getUserPrincipal().getName()); final IntegrationDeployment deployment = new IntegrationDeployment.Builder() .id(IntegrationDeployment.compositeId(id, version)).spec(integration).version(version) .userId(sec.getUserPrincipal().getName()) .createdAt(System.currentTimeMillis()) .statusMessage(validationStatus).build(); return getDataManager().create(deployment); } @Autowired  IntegrationDeploymentHandler(final DataManager dataMgr, OpenShiftService openShiftService,
            UserConfigurationProperties properties); @GET @Produces(MediaType.APPLICATION_JSON) @Path("/{version}") IntegrationDeployment get(@NotNull @PathParam("id") @Parameter(required = true) final String id,
        @NotNull @PathParam("version") @Parameter(required = true) final int version); @GET @Produces(MediaType.APPLICATION_JSON) ListResult<IntegrationDeployment> list(
        @NotNull @PathParam("id") @Parameter(required = true) final String id,
        @Parameter(required = false, description = "Page number to return") @QueryParam("page") @DefaultValue("1") int page,
        @Parameter(required = false, description = "Number of records per page") @QueryParam("per_page") @DefaultValue("20") int perPage
    ); @PUT @Produces(MediaType.APPLICATION_JSON) IntegrationDeployment update(@Context final SecurityContext sec,
        @NotNull @PathParam("id") @Parameter(required = true) final String id); @POST @Produces(MediaType.APPLICATION_JSON) @Path("/{version}/targetState") void updateTargetState(@NotNull @PathParam("id") @Parameter(required = true) final String id,
        @NotNull @PathParam("version") @Parameter(required = true) final int version,
        @Parameter(required = true) final TargetStateRequest request); }
IntegrationDeploymentHandler extends BaseHandler { @PUT @Produces(MediaType.APPLICATION_JSON) public IntegrationDeployment update(@Context final SecurityContext sec, @NotNull @PathParam("id") @Parameter(required = true) final String id) { final DataManager dataManager = getDataManager(); final Integration integration = dataManager.fetch(Integration.class, id); if (integration == null) { throw new EntityNotFoundException(); } Optional<Integer> maybeLatestVersion = dataManager .fetchAllByPropertyValue(IntegrationDeployment.class, "integrationId", id).map(deployment -> { if (deployment.getTargetState() != IntegrationDeploymentState.Unpublished) { dataManager.update(deployment.withTargetState(IntegrationDeploymentState.Unpublished)); } return deployment.getVersion(); }).max(Integer::compareTo); final int version = maybeLatestVersion.map(v -> v + 1).orElse(1); final Optional<String> validationStatus = validateQuotas(id, sec.getUserPrincipal().getName()); final IntegrationDeployment deployment = new IntegrationDeployment.Builder() .id(IntegrationDeployment.compositeId(id, version)).spec(integration).version(version) .userId(sec.getUserPrincipal().getName()) .createdAt(System.currentTimeMillis()) .statusMessage(validationStatus).build(); return getDataManager().create(deployment); } @Autowired  IntegrationDeploymentHandler(final DataManager dataMgr, OpenShiftService openShiftService,
            UserConfigurationProperties properties); @GET @Produces(MediaType.APPLICATION_JSON) @Path("/{version}") IntegrationDeployment get(@NotNull @PathParam("id") @Parameter(required = true) final String id,
        @NotNull @PathParam("version") @Parameter(required = true) final int version); @GET @Produces(MediaType.APPLICATION_JSON) ListResult<IntegrationDeployment> list(
        @NotNull @PathParam("id") @Parameter(required = true) final String id,
        @Parameter(required = false, description = "Page number to return") @QueryParam("page") @DefaultValue("1") int page,
        @Parameter(required = false, description = "Number of records per page") @QueryParam("per_page") @DefaultValue("20") int perPage
    ); @PUT @Produces(MediaType.APPLICATION_JSON) IntegrationDeployment update(@Context final SecurityContext sec,
        @NotNull @PathParam("id") @Parameter(required = true) final String id); @POST @Produces(MediaType.APPLICATION_JSON) @Path("/{version}/targetState") void updateTargetState(@NotNull @PathParam("id") @Parameter(required = true) final String id,
        @NotNull @PathParam("version") @Parameter(required = true) final int version,
        @Parameter(required = true) final TargetStateRequest request);  }