|
| 1 | +/* |
| 2 | + * Copyright © 2015 The Gravitee team (http://gravitee.io) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.gravitee.rest.api.management.v2.rest.resource.promotions; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.mockito.ArgumentMatchers.any; |
| 20 | +import static org.mockito.Mockito.reset; |
| 21 | +import static org.mockito.Mockito.verify; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | +import fixtures.core.model.PromotionFixtures; |
| 25 | +import inmemory.ApiCrudServiceInMemory; |
| 26 | +import inmemory.EnvironmentCrudServiceInMemory; |
| 27 | +import inmemory.InMemoryAlternative; |
| 28 | +import inmemory.PromotionCrudServiceInMemory; |
| 29 | +import io.gravitee.apim.core.api.model.Api; |
| 30 | +import io.gravitee.apim.core.api.model.NewApiMetadata; |
| 31 | +import io.gravitee.apim.core.api.model.import_definition.ApiDescriptor; |
| 32 | +import io.gravitee.apim.core.api.model.import_definition.GraviteeDefinition; |
| 33 | +import io.gravitee.apim.core.api.model.import_definition.PageExport; |
| 34 | +import io.gravitee.apim.core.api.model.import_definition.PlanDescriptor; |
| 35 | +import io.gravitee.apim.core.environment.model.Environment; |
| 36 | +import io.gravitee.apim.core.json.GraviteeDefinitionSerializer; |
| 37 | +import io.gravitee.apim.core.promotion.model.PromotionStatus; |
| 38 | +import io.gravitee.apim.core.promotion.use_case.ProcessPromotionUseCase; |
| 39 | +import io.gravitee.common.http.HttpStatusCode; |
| 40 | +import io.gravitee.definition.model.DefinitionVersion; |
| 41 | +import io.gravitee.rest.api.management.v2.rest.resource.AbstractResourceTest; |
| 42 | +import jakarta.ws.rs.client.Entity; |
| 43 | +import jakarta.ws.rs.client.WebTarget; |
| 44 | +import jakarta.ws.rs.core.Response; |
| 45 | +import java.util.Collections; |
| 46 | +import java.util.List; |
| 47 | +import java.util.stream.Stream; |
| 48 | +import lombok.SneakyThrows; |
| 49 | +import org.junit.jupiter.api.AfterEach; |
| 50 | +import org.junit.jupiter.api.BeforeEach; |
| 51 | +import org.junit.jupiter.api.Test; |
| 52 | +import org.mockito.ArgumentCaptor; |
| 53 | +import org.springframework.beans.factory.annotation.Autowired; |
| 54 | + |
| 55 | +class PromotionsResourceTest extends AbstractResourceTest { |
| 56 | + |
| 57 | + private static final String PROMOTION_ID = "promotion-id"; |
| 58 | + private static final String API_ID = "api-id"; |
| 59 | + private static final String API_CROSS_ID = "api-cross-id"; |
| 60 | + private static final String BASE64_PICTURE = "data:image/jpeg;base64,picture..."; |
| 61 | + private static final String DEFAULT_ENV_ID = "default-env-id"; |
| 62 | + private static final String TARGET_ENV_ID = "target-env-id"; |
| 63 | + private static final String TARGET_ENV_COCKPIT_ID = "target-env-cockpit-id"; |
| 64 | + private WebTarget target; |
| 65 | + |
| 66 | + @Autowired |
| 67 | + private ProcessPromotionUseCase processPromotionUseCase; |
| 68 | + |
| 69 | + @Autowired |
| 70 | + private PromotionCrudServiceInMemory promotionCrudServiceInMemory; |
| 71 | + |
| 72 | + @Autowired |
| 73 | + private ApiCrudServiceInMemory apiCrudServiceInMemory; |
| 74 | + |
| 75 | + @Autowired |
| 76 | + private EnvironmentCrudServiceInMemory environmentCrudServiceInMemory; |
| 77 | + |
| 78 | + @Autowired |
| 79 | + private GraviteeDefinitionSerializer graviteeDefinitionSerializer; |
| 80 | + |
| 81 | + @BeforeEach |
| 82 | + public void init() { |
| 83 | + environmentCrudServiceInMemory.initWith(List.of(Environment.builder().id(TARGET_ENV_ID).cockpitId(TARGET_ENV_COCKPIT_ID).build())); |
| 84 | + target = rootTarget(); |
| 85 | + } |
| 86 | + |
| 87 | + @AfterEach |
| 88 | + public void clean() { |
| 89 | + Stream.of(apiCrudServiceInMemory, promotionCrudServiceInMemory, environmentCrudServiceInMemory).forEach(InMemoryAlternative::reset); |
| 90 | + reset(processPromotionUseCase); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected String contextPath() { |
| 95 | + return "/organizations/" + ORGANIZATION + "/promotions/" + PROMOTION_ID + "/_process"; |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + @SneakyThrows |
| 100 | + void should_map_exported_definition_to_import_definition() { |
| 101 | + var export = GraviteeDefinition.from( |
| 102 | + ApiDescriptor.ApiDescriptorV4.builder().id(API_ID).crossId(API_CROSS_ID).build(), |
| 103 | + Collections.emptySet(), |
| 104 | + List.of(NewApiMetadata.builder().apiId(API_ID).name("metadata").value("metadata-value").build()), |
| 105 | + List.of(PageExport.builder().id("page-id").build()), |
| 106 | + List.of(PlanDescriptor.V4.builder().id("plan-id").build()), |
| 107 | + Collections.emptyList(), |
| 108 | + BASE64_PICTURE, |
| 109 | + BASE64_PICTURE |
| 110 | + ); |
| 111 | + apiCrudServiceInMemory.initWith(List.of(Api.builder().id(API_ID).crossId(API_CROSS_ID).environmentId(DEFAULT_ENV_ID).build())); |
| 112 | + |
| 113 | + var promotion = PromotionFixtures.aPromotion() |
| 114 | + .toBuilder() |
| 115 | + .id(PROMOTION_ID) |
| 116 | + .apiId(API_ID) |
| 117 | + .targetEnvCockpitId(TARGET_ENV_COCKPIT_ID) |
| 118 | + .apiDefinition(graviteeDefinitionSerializer.serialize(export)) |
| 119 | + .build(); |
| 120 | + promotionCrudServiceInMemory.initWith(List.of(promotion)); |
| 121 | + |
| 122 | + when(processPromotionUseCase.execute(any())).thenReturn( |
| 123 | + new ProcessPromotionUseCase.Output(PromotionFixtures.aPromotion().toBuilder().status(PromotionStatus.REJECTED).build()) |
| 124 | + ); |
| 125 | + |
| 126 | + Response response = target.request().post(Entity.json(false)); |
| 127 | + |
| 128 | + var captor = ArgumentCaptor.forClass(ProcessPromotionUseCase.Input.class); |
| 129 | + verify(processPromotionUseCase).execute(captor.capture()); |
| 130 | + |
| 131 | + var inputValue = captor.getValue(); |
| 132 | + assertThat(inputValue) |
| 133 | + .isNotNull() |
| 134 | + .satisfies(input -> { |
| 135 | + assertThat(input.promotion()).isEqualTo(promotion); |
| 136 | + assertThat(input.definitionVersion()).isEqualTo(DefinitionVersion.V4); |
| 137 | + assertThat(input.importDefinition()).isNotNull(); |
| 138 | + assertThat(input.isAccepted()).isFalse(); |
| 139 | + assertThat(input.auditInfo().environmentId()).isEqualTo(TARGET_ENV_ID); |
| 140 | + }); |
| 141 | + |
| 142 | + assertThat(response.getStatus()).isEqualTo(HttpStatusCode.OK_200); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + @SneakyThrows |
| 147 | + void should_call_use_case_with_promotion_and_v2_definition_version_without_mapping_exported_definition() { |
| 148 | + var promotion = PromotionFixtures.aPromotion() |
| 149 | + .toBuilder() |
| 150 | + .id(PROMOTION_ID) |
| 151 | + .apiId(API_ID) |
| 152 | + .apiDefinition("{ \"gravitee\" : \"2.0.0\", \"id\" : \"api-id\" }") |
| 153 | + .build(); |
| 154 | + promotionCrudServiceInMemory.initWith(List.of(promotion)); |
| 155 | + apiCrudServiceInMemory.initWith(List.of(Api.builder().id(API_ID).crossId(API_CROSS_ID).environmentId(DEFAULT_ENV_ID).build())); |
| 156 | + |
| 157 | + when(processPromotionUseCase.execute(any())).thenReturn( |
| 158 | + new ProcessPromotionUseCase.Output(PromotionFixtures.aPromotion().toBuilder().status(PromotionStatus.ACCEPTED).build()) |
| 159 | + ); |
| 160 | + |
| 161 | + Response response = target.request().post(Entity.json(true)); |
| 162 | + |
| 163 | + var captor = ArgumentCaptor.forClass(ProcessPromotionUseCase.Input.class); |
| 164 | + verify(processPromotionUseCase).execute(captor.capture()); |
| 165 | + |
| 166 | + var inputValue = captor.getValue(); |
| 167 | + assertThat(inputValue) |
| 168 | + .isNotNull() |
| 169 | + .satisfies(input -> { |
| 170 | + assertThat(input.promotion()).isEqualTo(promotion); |
| 171 | + assertThat(input.definitionVersion()).isEqualTo(DefinitionVersion.V2); |
| 172 | + assertThat(input.importDefinition()).isNull(); |
| 173 | + assertThat(input.isAccepted()).isTrue(); |
| 174 | + assertThat(input.auditInfo()).isNull(); |
| 175 | + }); |
| 176 | + |
| 177 | + assertThat(response.getStatus()).isEqualTo(HttpStatusCode.OK_200); |
| 178 | + } |
| 179 | +} |
0 commit comments