Skip to content

Commit

Permalink
Add methods to interact with hoverfly csv data source API
Browse files Browse the repository at this point in the history
  • Loading branch information
tommysitu committed Mar 28, 2024
1 parent cb91d88 commit d8f6319
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import io.specto.hoverfly.junit.api.command.JournalIndexCommand;
import io.specto.hoverfly.junit.api.command.SortParams;
import io.specto.hoverfly.junit.api.model.CsvDataSource;
import io.specto.hoverfly.junit.api.model.ModeArguments;
import io.specto.hoverfly.junit.api.view.DiffView;
import io.specto.hoverfly.junit.api.view.HoverflyInfoView;
Expand Down Expand Up @@ -53,13 +54,19 @@ public interface HoverflyClient {

Journal searchJournal(Request request);

void deleteJournal();

List<JournalIndexView> getJournalIndex();

void addJournalIndex(JournalIndexCommand journalIndexCommand);

void deleteJournalIndex(String indexName);

void deleteJournal();
List<CsvDataSource> getCsvDataSources();

void addCsvDataSource(CsvDataSource dataSource);

void deleteCsvDataSource(String name);

/**
* Deletes all state from Hoverfly.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package io.specto.hoverfly.junit.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.type.CollectionType;
import io.specto.hoverfly.junit.api.command.DestinationCommand;
import io.specto.hoverfly.junit.api.command.JournalIndexCommand;
import io.specto.hoverfly.junit.api.command.JournalSearchCommand;
import io.specto.hoverfly.junit.api.command.ModeCommand;
import io.specto.hoverfly.junit.api.command.SortParams;
import io.specto.hoverfly.junit.api.model.CsvDataSource;
import io.specto.hoverfly.junit.api.model.ModeArguments;
import io.specto.hoverfly.junit.api.view.DiffView;
import io.specto.hoverfly.junit.api.view.HoverflyInfoView;
Expand All @@ -20,6 +22,7 @@
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
Expand All @@ -40,6 +43,7 @@ class OkHttpHoverflyClient implements HoverflyClient {
private static final String MODE_PATH = "api/v2/hoverfly/mode";
private static final String JOURNAL_PATH = "api/v2/journal";
private static final String JOURNAL_INDEX_PATH = "api/v2/journal/index";
private static final String CSV_DATA_SOURCE_PATH = "api/v2/hoverfly/templating-data-source/csv";
private static final String STATE_PATH = "api/v2/state";
private static final String DIFF_PATH = "api/v2/diff";

Expand Down Expand Up @@ -203,7 +207,7 @@ public void addJournalIndex(JournalIndexCommand journalIndexCommand) {
exchange(request);
} catch (Exception e) {
LOGGER.warn("Failed to add journal index: {}", e.getMessage());
throw new HoverflyClientException("Failed to journal index: " + e.getMessage());
throw new HoverflyClientException("Failed to add journal index: " + e.getMessage());
}
}

Expand Down Expand Up @@ -232,6 +236,46 @@ public void deleteJournal() {
}
}

@Override
public List<CsvDataSource> getCsvDataSources() {
try {
final Request.Builder builder = createRequestBuilderWithUrl(CSV_DATA_SOURCE_PATH);
final Request request = builder.get().build();
Map<String, List<CsvDataSource>> response = exchange(request,
new TypeReference<Map<String, List<CsvDataSource>>>() {
});
return response.getOrDefault("csvDataSources", Collections.emptyList());
} catch (Exception e) {
LOGGER.warn("Failed to get csv data source: {}", e.getMessage());
throw new HoverflyClientException("Failed to get csv data source: " + e.getMessage());
}
}

@Override
public void addCsvDataSource(CsvDataSource dataSource) {
try {
final Request.Builder builder = createRequestBuilderWithUrl(CSV_DATA_SOURCE_PATH);
final RequestBody body = createRequestBody(dataSource);
final Request request = builder.put(body).build();
exchange(request);
} catch (Exception e) {
LOGGER.warn("Failed to csv data source: {}", e.getMessage());
throw new HoverflyClientException("Failed to add csv data source: " + e.getMessage());
}
}

@Override
public void deleteCsvDataSource(String name) {
try {
final Request.Builder builder = createRequestBuilderWithUrl(CSV_DATA_SOURCE_PATH + "/" + name);
final Request request = builder.delete().build();
exchange(request);
} catch (Exception e) {
LOGGER.warn("Failed to delete csv data source: {}", e.getMessage());
throw new HoverflyClientException("Failed to delete csv data source: " + e.getMessage());
}
}

@Override
public void deleteState() {
try {
Expand Down Expand Up @@ -411,6 +455,14 @@ private <T> T exchange(Request request, Class<T> clazz) throws IOException {
}
}

// Deserialize response body on success with TypeReference
private <T> T exchange(Request request, TypeReference<T> clazz) throws IOException {
try (Response response = client.newCall(request).execute()) {
onFailure(response);
return ObjectMapperFactory.getDefaultObjectMapper().readValue(response.body().string(), clazz);
}
}

// Does nothing on success
private void exchange(Request request) throws IOException {
try (Response response = client.newCall(request).execute()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.specto.hoverfly.junit.api.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CsvDataSource {

private final String name;
private final String data;

@JsonCreator
public CsvDataSource(@JsonProperty("name") String name, @JsonProperty("data") String data) {
this.name = name;
this.data = data;
}

public String getName() {
return name;
}

public String getData() {
return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.common.io.Resources;
import io.specto.hoverfly.junit.api.command.JournalIndexCommand;
import io.specto.hoverfly.junit.api.command.SortParams;
import io.specto.hoverfly.junit.api.model.CsvDataSource;
import io.specto.hoverfly.junit.api.model.ModeArguments;
import io.specto.hoverfly.junit.api.view.HoverflyInfoView;
import io.specto.hoverfly.junit.api.view.JournalIndexView;
Expand Down Expand Up @@ -292,6 +293,21 @@ public void shouldBeAbleToGetUpdateAndDeleteJournalIndex() {
assertThat(client.getJournalIndex()).isEmpty();
}

@Test
public void shouldBeAbleToGetUpdateAndDeleteCsvDataSources() {

assertThat(client.getCsvDataSources()).isEmpty();

CsvDataSource csvDataSource = new CsvDataSource("orders", "id,name,marks\n1,Test1,55\n2,Test2,56\n*,Dummy,ABSENT\n");
client.addCsvDataSource(csvDataSource);

assertThat(client.getCsvDataSources()).usingRecursiveFieldByFieldElementComparator()
.containsExactly(csvDataSource);

client.deleteCsvDataSource("orders");
assertThat(client.getCsvDataSources()).isEmpty();
}

@After
public void tearDown() {
if (hoverfly != null) {
Expand Down

0 comments on commit d8f6319

Please sign in to comment.