|
| 1 | +/*************************************************************************************************** |
| 2 | + * Copyright (c) 2023, Salesforce, Inc. All rights reserved. SPDX-License-Identifier: |
| 3 | + * Apache License Version 2.0 |
| 4 | + * For full license text, see the LICENSE file in the repo root or |
| 5 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + **************************************************************************************************/ |
| 7 | + |
| 8 | +package com.salesforce.revoman.integration.pokemon; |
| 9 | + |
| 10 | +import static com.google.common.truth.Truth.assertThat; |
| 11 | +import static com.salesforce.revoman.input.config.HookConfig.post; |
| 12 | +import static com.salesforce.revoman.input.config.HookConfig.pre; |
| 13 | +import static com.salesforce.revoman.input.config.KickDef.intoMap; |
| 14 | +import static com.salesforce.revoman.input.config.ResponseConfig.unmarshallResponse; |
| 15 | +import static com.salesforce.revoman.input.config.StepPick.PostTxnStepPick.afterStepContainingHeader; |
| 16 | +import static com.salesforce.revoman.input.config.StepPick.PostTxnStepPick.afterStepContainingURIPathOfAny; |
| 17 | +import static com.salesforce.revoman.input.config.StepPick.PostTxnStepPick.afterStepName; |
| 18 | +import static com.salesforce.revoman.input.config.StepPick.PreTxnStepPick.beforeStepContainingHeader; |
| 19 | +import static com.salesforce.revoman.input.config.StepPick.PreTxnStepPick.beforeStepName; |
| 20 | +import static org.assertj.vavr.api.VavrAssertions.assertThat; |
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.Mockito.times; |
| 23 | + |
| 24 | +import com.salesforce.revoman.ReVoman; |
| 25 | +import com.salesforce.revoman.input.config.HookConfig.StepHook.PostStepHook; |
| 26 | +import com.salesforce.revoman.input.config.HookConfig.StepHook.PreStepHook; |
| 27 | +import com.salesforce.revoman.input.config.Kick; |
| 28 | +import com.salesforce.revoman.integration.core.adapters.IDAdapter; |
| 29 | +import com.salesforce.revoman.integration.core.pq.connect.response.ID; |
| 30 | +import com.salesforce.revoman.output.Rundown; |
| 31 | +import com.salesforce.revoman.output.report.Step; |
| 32 | +import com.salesforce.revoman.output.report.StepReport; |
| 33 | +import com.salesforce.revoman.output.report.TxnInfo; |
| 34 | +import com.salesforce.revoman.output.report.failure.HookFailure.PostStepHookFailure; |
| 35 | +import com.squareup.moshi.Json; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | +import kotlin.Pair; |
| 39 | +import kotlin.collections.MapsKt; |
| 40 | +import org.http4k.core.Request; |
| 41 | +import org.jetbrains.annotations.NotNull; |
| 42 | +import org.junit.jupiter.api.Test; |
| 43 | +import org.mockito.Mockito; |
| 44 | +import org.slf4j.Logger; |
| 45 | +import org.slf4j.LoggerFactory; |
| 46 | + |
| 47 | +class PokemonHttpTest { |
| 48 | + |
| 49 | + private static final String HTTP_TEMPLATE_PATH = "http-templates/pokemon/pokemon.http"; |
| 50 | + private static final String HTTP_ENVIRONMENT_PATH = "http-templates/pokemon/http-client.env.json"; |
| 51 | + private static final String ALL_POKEMON_STEP = "all-pokemon"; |
| 52 | + private static final String ENV_LIMIT = "limit"; |
| 53 | + private static final int LIMIT = 3; |
| 54 | + private static final int OFFSET = 0; |
| 55 | + private static final Logger LOGGER = LoggerFactory.getLogger(PokemonHttpTest.class); |
| 56 | + private static final RuntimeException RUNTIME_EXCEPTION = |
| 57 | + new RuntimeException("This won't interrupt the execution as `haltOnAnyFailure` is not set"); |
| 58 | + |
| 59 | + @Test |
| 60 | + void pokemonHttp() { |
| 61 | + final var newLimit = 1; |
| 62 | + final var dynamicEnvironment1 = Map.of("offset", OFFSET); |
| 63 | + final var dynamicEnvironment2 = |
| 64 | + MapsKt.mapOf(new Pair<>(ENV_LIMIT, LIMIT), new Pair<>("null", null)); |
| 65 | + @SuppressWarnings("Convert2Lambda") |
| 66 | + final var preLogHook = |
| 67 | + Mockito.spy( |
| 68 | + new PreStepHook() { |
| 69 | + @Override |
| 70 | + public void accept( |
| 71 | + @NotNull Step currentStep, |
| 72 | + @NotNull TxnInfo<Request> ignore1, |
| 73 | + @NotNull Rundown ignore2) { |
| 74 | + LOGGER.info("Picked `preLogHook` before stepName: {}", currentStep); |
| 75 | + } |
| 76 | + }); |
| 77 | + @SuppressWarnings("Convert2Lambda") |
| 78 | + final var postLogHook = |
| 79 | + Mockito.spy( |
| 80 | + new PostStepHook() { |
| 81 | + @Override |
| 82 | + public void accept(@NotNull StepReport stepReport, @NotNull Rundown ignore) { |
| 83 | + LOGGER.info("Picked `postLogHook` after stepName: {}", stepReport.step.displayName); |
| 84 | + throw RUNTIME_EXCEPTION; |
| 85 | + } |
| 86 | + }); |
| 87 | + @SuppressWarnings("Convert2Lambda") |
| 88 | + final var preStepHookBeforeStepName = |
| 89 | + Mockito.spy( |
| 90 | + new PreStepHook() { |
| 91 | + @Override |
| 92 | + public void accept( |
| 93 | + @NotNull Step ignore1, |
| 94 | + @NotNull TxnInfo<Request> ignore2, |
| 95 | + @NotNull Rundown rundown) { |
| 96 | + rundown.mutableEnv.set(ENV_LIMIT, newLimit); |
| 97 | + } |
| 98 | + }); |
| 99 | + @SuppressWarnings("Convert2Lambda") |
| 100 | + final var postStepHookAfterStepName = |
| 101 | + Mockito.spy( |
| 102 | + new PostStepHook() { |
| 103 | + @Override |
| 104 | + public void accept(@NotNull StepReport stepReport, @NotNull Rundown rundown) { |
| 105 | + assertThat(rundown.mutableEnv).containsEntry(ENV_LIMIT, newLimit); |
| 106 | + final var results = |
| 107 | + stepReport.responseInfo.get().<AllPokemon>getTypedTxnObj().results; |
| 108 | + assertThat(results.size()).isEqualTo(newLimit); |
| 109 | + } |
| 110 | + }); |
| 111 | + @SuppressWarnings("Convert2Lambda") |
| 112 | + final var postStepHookAfterURIPath = |
| 113 | + Mockito.spy( |
| 114 | + new PostStepHook() { |
| 115 | + @Override |
| 116 | + public void accept(@NotNull StepReport stepReport, @NotNull Rundown rundown) { |
| 117 | + LOGGER.info( |
| 118 | + "Picked `postStepHookAfterURIPath` after stepName: {} with raw URI: {}", |
| 119 | + stepReport.step.displayName, |
| 120 | + stepReport.step.rawPMStep.getRequest().url); |
| 121 | + final var id = |
| 122 | + stepReport |
| 123 | + .responseInfo |
| 124 | + .map( |
| 125 | + ri -> |
| 126 | + ri.<Color>getTypedTxnObj(Color.class, List.of(new IDAdapter())) |
| 127 | + .id) |
| 128 | + .getOrNull(); |
| 129 | + assertThat(id.id()).isEqualTo(rundown.mutableEnv.get("id")); |
| 130 | + } |
| 131 | + }); |
| 132 | + final var config = |
| 133 | + Kick.configure() |
| 134 | + .templatePath(HTTP_TEMPLATE_PATH) |
| 135 | + .environmentPath(HTTP_ENVIRONMENT_PATH) |
| 136 | + .responseConfig(unmarshallResponse(afterStepName(ALL_POKEMON_STEP), AllPokemon.class)) |
| 137 | + .hooks( |
| 138 | + pre(beforeStepName(ALL_POKEMON_STEP), preStepHookBeforeStepName), |
| 139 | + post(afterStepName(ALL_POKEMON_STEP), postStepHookAfterStepName), |
| 140 | + post(afterStepContainingURIPathOfAny("pokemon-color"), postStepHookAfterURIPath), |
| 141 | + pre(beforeStepContainingHeader("preLog"), preLogHook), |
| 142 | + post(afterStepContainingHeader("postLog"), postLogHook)) |
| 143 | + .dynamicEnvironment(dynamicEnvironment1) |
| 144 | + .off(); |
| 145 | + final var pokeRundown = |
| 146 | + ReVoman.revUp( |
| 147 | + config.overrideDynamicEnvironment(intoMap(dynamicEnvironment1, dynamicEnvironment2))); |
| 148 | + |
| 149 | + final var postHookFailure = pokeRundown.firstUnIgnoredUnsuccessfulStepReport().failure; |
| 150 | + assertThat(postHookFailure).containsLeftInstanceOf(PostStepHookFailure.class); |
| 151 | + assertThat(postHookFailure.getLeft().getFailure()).isEqualTo(RUNTIME_EXCEPTION); |
| 152 | + assertThat(pokeRundown.stepReports).hasSize(5); |
| 153 | + assertThat(pokeRundown.mutableEnv) |
| 154 | + .containsExactlyEntriesIn( |
| 155 | + MapsKt.mapOf( |
| 156 | + new Pair<>("offset", OFFSET), |
| 157 | + new Pair<>(ENV_LIMIT, newLimit), |
| 158 | + new Pair<>("baseUrl", "https://pokeapi.co/api/v2"), |
| 159 | + new Pair<>("id", "1"), |
| 160 | + new Pair<>("pokemonName", "bulbasaur"), |
| 161 | + new Pair<>("color", "black"), |
| 162 | + new Pair<>("gender", "female"), |
| 163 | + new Pair<>("ability", "stench"), |
| 164 | + new Pair<>("nature", "hardy"), |
| 165 | + new Pair<>("null", null))); |
| 166 | + Mockito.verify(preStepHookBeforeStepName, times(1)).accept(any(), any(), any()); |
| 167 | + Mockito.verify(postStepHookAfterStepName, times(1)).accept(any(), any()); |
| 168 | + Mockito.verify(postStepHookAfterURIPath, times(1)).accept(any(), any()); |
| 169 | + Mockito.verify(preLogHook, times(1)).accept(any(), any(), any()); |
| 170 | + Mockito.verify(postLogHook, times(1)).accept(any(), any()); |
| 171 | + } |
| 172 | + |
| 173 | + public record AllPokemon(int count, String next, String previous, List<Result> results) { |
| 174 | + public record Result(String name, String url) {} |
| 175 | + } |
| 176 | + |
| 177 | + public record Color( |
| 178 | + ID id, |
| 179 | + String name, |
| 180 | + List<Name> names, |
| 181 | + @Json(name = "pokemon_species") List<PokemonSpecies> pokemonSpecies) { |
| 182 | + public record Name(Language language, String name) { |
| 183 | + public record Language(String name, String url) {} |
| 184 | + } |
| 185 | + |
| 186 | + public record PokemonSpecies(String name, String url) {} |
| 187 | + } |
| 188 | +} |
0 commit comments