Skip to content

Commit

Permalink
StubProxyConfig model contains 'headers' property (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
azagniotov authored Apr 2, 2021
1 parent f5429ac commit e441a26
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,10 @@ public void shouldUnmarshall_toProxyConfigs() throws Exception {
" description: this is a default catch-all config\n" +
" strategy: as-is\n" +
" properties:\n" +
" endpoint: https://jsonplaceholder.typicode.com\n");
" endpoint: https://jsonplaceholder.typicode.com\n" +
" headers:\n" +
" headerKeyOne: headerValueOne\n" +
" headerKeyTwo: headerValueTwo\n");

assertThat(customStubProxyConfig.getProxyConfigAsYAML()).isEqualTo(
"- proxy-config:\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
strategy: as-is
properties:
endpoint: https://jsonplaceholder.typicode.com
headers:
headerKeyOne: headerValueOne
headerKeyTwo: headerValueTwo

- proxy-config:
description: woah! this is a unique proxy-config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static io.github.azagniotov.generics.TypeSafeConverter.asCheckedLinkedHashMap;
import static io.github.azagniotov.stubby4j.yaml.ConfigurableYAMLProperty.DESCRIPTION;
import static io.github.azagniotov.stubby4j.yaml.ConfigurableYAMLProperty.ENDPOINT;
import static io.github.azagniotov.stubby4j.yaml.ConfigurableYAMLProperty.HEADERS;
import static io.github.azagniotov.stubby4j.yaml.ConfigurableYAMLProperty.PROPERTIES;
import static io.github.azagniotov.stubby4j.yaml.ConfigurableYAMLProperty.STRATEGY;
import static io.github.azagniotov.stubby4j.yaml.ConfigurableYAMLProperty.UUID;
Expand All @@ -19,17 +20,20 @@ public class StubProxyConfig implements ReflectableStub {
private final String description;
private final String uuid;
private final StubProxyStrategy strategy;
private final Map<String, String> headers;
private final Map<String, String> properties;
private final String proxyConfigAsYAML;

private StubProxyConfig(final String description,
final String uuid,
final StubProxyStrategy strategy,
final Map<String, String> headers,
final Map<String, String> properties,
final String proxyConfigAsYAML) {
this.description = description;
this.uuid = uuid;
this.strategy = strategy;
this.headers = headers;
this.properties = properties;
this.proxyConfigAsYAML = proxyConfigAsYAML;
}
Expand All @@ -46,6 +50,14 @@ public StubProxyStrategy getStrategy() {
return strategy;
}

public final Map<String, String> getHeaders() {
return new HashMap<>(headers);
}

public boolean hasHeaders() {
return !getHeaders().isEmpty();
}

public Map<String, String> getProperties() {
return new HashMap<>(properties);
}
Expand All @@ -69,6 +81,7 @@ public boolean equals(Object o) {
StubProxyConfig that = (StubProxyConfig) o;
return Objects.equals(uuid, that.uuid) &&
strategy == that.strategy &&
headers.equals(that.headers) &&
properties.equals(that.properties);
}

Expand All @@ -83,6 +96,7 @@ public static final class Builder extends AbstractBuilder<StubProxyConfig> {
private String description;
private String uuid;
private StubProxyStrategy strategy;
private Map<String, String> headers;
private Map<String, String> properties;
private String proxyConfigAsYAML;

Expand All @@ -91,6 +105,7 @@ public Builder() {
this.description = null;
this.uuid = DEFAULT_UUID;
this.strategy = null;
this.headers = new LinkedHashMap<>();
this.properties = new LinkedHashMap<>();
this.proxyConfigAsYAML = null;
}
Expand All @@ -114,6 +129,12 @@ public Builder withStrategy(final String strategy) {
return this;
}

public Builder withHeader(final String key, final String value) {
this.headers.put(key, value);

return this;
}

public Builder withProperty(final String key, final String value) {
this.properties.put(key, value);

Expand All @@ -138,18 +159,21 @@ public StubProxyConfig build() {
this.description = getStaged(String.class, DESCRIPTION, description);
this.uuid = getStaged(String.class, UUID, uuid);
this.strategy = getStaged(StubProxyStrategy.class, STRATEGY, strategy);
this.headers = asCheckedLinkedHashMap(getStaged(Map.class, HEADERS, headers), String.class, String.class);
this.properties = asCheckedLinkedHashMap(getStaged(Map.class, PROPERTIES, properties), String.class, String.class);

final StubProxyConfig stubProxyConfig = new StubProxyConfig(
description,
uuid,
strategy,
headers,
properties,
proxyConfigAsYAML);

this.description = null;
this.uuid = DEFAULT_UUID;
this.strategy = null;
this.headers = new LinkedHashMap<>();
this.properties = new LinkedHashMap<>();
this.proxyConfigAsYAML = null;
this.fieldNameAndValues.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ public Builder withUrl(final String value) {
}

public Builder withHeader(final String key, final String value) {
// Although it is weird to see .valueOf invoked on string, it will convert a null value to "null"
this.headers.put(valueOf(key), valueOf(value));

return this;
Expand Down
51 changes: 36 additions & 15 deletions src/main/java/io/github/azagniotov/stubby4j/yaml/YamlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public final class YamlBuilder {
private final static String RESPONSE = String.format(TWO_TOKENS_TEMPLATE, THREE_SPACE, "response:");

private final static String HEADERS = String.format(TWO_TOKENS_TEMPLATE, SIX_SPACE, "headers:");
private final static String PROXY_HEADERS = String.format(TWO_TOKENS_TEMPLATE, SIX_SPACE, "headers:");
private final static String PROXY_PROPERTIES = String.format(TWO_TOKENS_TEMPLATE, SIX_SPACE, "properties:");
private final static String PROXY_STRATEGY = String.format(TWO_TOKENS_TEMPLATE, SIX_SPACE, "strategy: ");
private final static String PROXY_UUID = String.format(TWO_TOKENS_TEMPLATE, SIX_SPACE, "uuid: ");
Expand Down Expand Up @@ -63,6 +64,7 @@ public final class YamlBuilder {
private final static String NL = FileUtils.BR;

private static final String TWO_DASHED_TOKENS_TEMPLATE = "%s-%s";
private final static String PROXY_HEADERS_KEY = String.format(TWO_DASHED_TOKENS_TEMPLATE, PROXY_CONFIG_AS_TOP, PROXY_HEADERS);
private final static String PROXY_PROPERTIES_KEY = String.format(TWO_DASHED_TOKENS_TEMPLATE, PROXY_CONFIG_AS_TOP, PROXY_PROPERTIES);
private final static String REQUEST_HEADERS_KEY = String.format(TWO_DASHED_TOKENS_TEMPLATE, REQUEST_AS_TOP, HEADERS);
private final static String REQUEST_QUERY_KEY = String.format(TWO_DASHED_TOKENS_TEMPLATE, REQUEST_AS_TOP, QUERY);
Expand All @@ -77,6 +79,7 @@ public final class YamlBuilder {

private final Set<String> storedStubbedMethods = new LinkedHashSet<>();
private final Set<String> unusedNodes = new HashSet<String>() {{
add(PROXY_HEADERS_KEY);
add(PROXY_PROPERTIES_KEY);
add(REQUEST_HEADERS_KEY);
add(REQUEST_QUERY_KEY);
Expand All @@ -100,6 +103,22 @@ public ProxyConfig newStubbedProxyConfig() {
return new ProxyConfig();
}

private void clear() {
unusedNodes.clear();
unusedNodes.add(PROXY_HEADERS_KEY);
unusedNodes.add(PROXY_PROPERTIES_KEY);
unusedNodes.add(REQUEST_HEADERS_KEY);
unusedNodes.add(REQUEST_QUERY_KEY);
unusedNodes.add(RESPONSE_HEADERS_KEY);
unusedNodes.add(RESPONSE_QUERY_KEY);
storedStubbedMethods.clear();

FEATURE_STRING_BUILDER.setLength(0);
REQUEST_STRING_BUILDER.setLength(0);
RESPONSE_STRING_BUILDER.setLength(0);
PROXY_CONFIG_STRING_BUILDER.setLength(0);
}

public final class Feature {

private boolean topLevelSet;
Expand Down Expand Up @@ -159,6 +178,16 @@ public ProxyConfig withProxyStrategyAsIs() {
return this;
}

public ProxyConfig withHeader(final String key, final String value) {

checkProxyHeadersNodeRequired();

final String tabbedKey = String.format(YAML_KEY_SPACE_TEMPLATE, NINE_SPACE, key);
PROXY_CONFIG_STRING_BUILDER.append(tabbedKey).append(value).append(NL);

return this;
}

public ProxyConfig withProperty(final String key, final String value) {

checkProxyPropertiesNodeRequired();
Expand Down Expand Up @@ -187,6 +216,13 @@ private void checkProxyPropertiesNodeRequired() {
}
}

private void checkProxyHeadersNodeRequired() {
if (unusedNodes.contains(PROXY_HEADERS_KEY)) {
PROXY_CONFIG_STRING_BUILDER.append(PROXY_HEADERS).append(NL);
unusedNodes.remove(PROXY_HEADERS_KEY);
}
}

public String toString() {
return PROXY_CONFIG_STRING_BUILDER.toString();
}
Expand Down Expand Up @@ -539,19 +575,4 @@ public String build() {
return yaml;
}
}

private void clear() {
unusedNodes.clear();
unusedNodes.add(PROXY_PROPERTIES_KEY);
unusedNodes.add(REQUEST_HEADERS_KEY);
unusedNodes.add(REQUEST_QUERY_KEY);
unusedNodes.add(RESPONSE_HEADERS_KEY);
unusedNodes.add(RESPONSE_QUERY_KEY);
storedStubbedMethods.clear();

FEATURE_STRING_BUILDER.setLength(0);
REQUEST_STRING_BUILDER.setLength(0);
RESPONSE_STRING_BUILDER.setLength(0);
PROXY_CONFIG_STRING_BUILDER.setLength(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public void cleanup() throws Exception {
RegexParser.REGEX_PATTERN_CACHE.clear();
}

@Test
public void stubbedProxyConfigHasNoHeaders() throws Exception {

final StubProxyConfig stubProxyConfig = builder.build();
assertThat(stubProxyConfig.hasHeaders()).isFalse();
assertThat(stubProxyConfig.getHeaders().isEmpty()).isTrue();
}

@Test
public void stubbedProxyConfigHasDefaultUuid() throws Exception {

Expand Down Expand Up @@ -107,20 +115,53 @@ public void stubbedProxyConfigEqualsAssertingConfig() throws Exception {
final StubProxyConfig expectedStubProxyConfig = builder
.withUuid("unique")
.withStrategy("as-is")
.withHeader("headerKey", "headerValue")
.withProperty("key", "value")
.withPropertyEndpoint("http://google.com")
.build();

final StubProxyConfig assertingStubProxyConfig = builder
.withUuid("unique")
.withStrategy("as-is")
.withHeader("headerKey", "headerValue")
.withProperty("key", "value")
.withPropertyEndpoint("http://google.com")
.build();

assertThat(expectedStubProxyConfig.hasHeaders()).isTrue();
assertThat(expectedStubProxyConfig.getHeaders().isEmpty()).isFalse();

assertThat(assertingStubProxyConfig).isEqualTo(expectedStubProxyConfig);
}

@Test
public void stubbedProxyConfigNotEqualsAssertingConfigWithDifferentHeader() throws Exception {

final StubProxyConfig expectedStubProxyConfig = builder
.withUuid("unique")
.withStrategy("as-is")
.withHeader("headerKey", "headerValue")
.withProperty("key", "value")
.withPropertyEndpoint("http://google.com")
.build();

final StubProxyConfig assertingStubProxyConfig = builder
.withUuid("unique")
.withStrategy("as-is")
.withHeader("headerKey", "headerDifferentValue")
.withProperty("key", "value")
.withPropertyEndpoint("http://google.com")
.build();

assertThat(expectedStubProxyConfig.hasHeaders()).isTrue();
assertThat(expectedStubProxyConfig.getHeaders().isEmpty()).isFalse();

assertThat(assertingStubProxyConfig.hasHeaders()).isTrue();
assertThat(assertingStubProxyConfig.getHeaders().isEmpty()).isFalse();

assertThat(assertingStubProxyConfig).isNotEqualTo(expectedStubProxyConfig);
}

@Test
public void stubbedProxyConfigNotEqualsAssertingConfig() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ public void shouldBuildStubbedProxyConfig() throws Exception {
" description: very-interesting-description" + BR +
" uuid: very-unique-name" + BR +
" strategy: as-is" + BR +
" headers:" + BR +
" keyOne: valueOne" + BR +
" keyTwo: valueTwo" + BR +
" properties:" + BR +
" endpoint: http://google.com" + BR +
" key: value";
Expand All @@ -278,6 +281,8 @@ public void shouldBuildStubbedProxyConfig() throws Exception {
.withDescription("very-interesting-description")
.withUuid("very-unique-name")
.withProxyStrategyAsIs()
.withHeader("keyOne", "valueOne")
.withHeader("keyTwo", "valueTwo")
.withPropertyEndpoint("http://google.com")
.withProperty("key", "value")
.toString().trim();
Expand Down

0 comments on commit e441a26

Please sign in to comment.