Skip to content

Commit

Permalink
Fixing the sequenced response stub configuration when data is in file
Browse files Browse the repository at this point in the history
  • Loading branch information
prakash committed Aug 11, 2013
1 parent 3fa6ec9 commit 93f76e3
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ Thumbs.db
stubby4j.iml
stubby4j.ipr
stubby4j.iws
*.iml
31 changes: 31 additions & 0 deletions functional/java/by/stub/StubsPortalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,35 @@ public void should_MakeSuccesfulRequest_AndReturnMultipleSequencedResponses() th
assertThat(HttpStatus.CREATED_201).isEqualTo(firstSequenceResponse.getStatusCode());
assertThat(firstResponseContent).isEqualTo("OK");
}

@Test
public void should_MakeSuccesfulRequest_AndReturnMultipleSequencedResponses_FromFile() throws Exception {

final String requestUrl = String.format("%s%s", STUBS_URL, "/uri/with/sequenced/responses/infile");
final HttpRequest request = HttpUtils.constructHttpRequest(HttpMethods.GET, requestUrl);

HttpResponse firstSequenceResponse = request.execute();
String firstResponseContent = firstSequenceResponse.parseAsString().trim();

assertThat(HttpStatus.CREATED_201).isEqualTo(firstSequenceResponse.getStatusCode());
assertThat(firstResponseContent).isEqualTo("OK");

final HttpResponse secondSequenceResponse = request.execute();
final String secondResponseContent = secondSequenceResponse.parseAsString().trim();

assertThat(HttpStatus.CREATED_201).isEqualTo(secondSequenceResponse.getStatusCode());
assertThat(secondResponseContent).isEqualTo("Still going strong!");

final HttpResponse thridSequenceResponse = request.execute();
final String thirdResponseContent = thridSequenceResponse.parseAsString().trim();

assertThat(HttpStatus.INTERNAL_SERVER_ERROR_500).isEqualTo(thridSequenceResponse.getStatusCode());
assertThat(thirdResponseContent).isEqualTo("OMFG!!!");

firstSequenceResponse = request.execute();
firstResponseContent = firstSequenceResponse.parseAsString().trim();

assertThat(HttpStatus.CREATED_201).isEqualTo(firstSequenceResponse.getStatusCode());
assertThat(firstResponseContent).isEqualTo("OK");
}
}
1 change: 1 addition & 0 deletions functional/resources/json/goingstrong.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Still going strong!
1 change: 1 addition & 0 deletions functional/resources/json/ok.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OK
1 change: 1 addition & 0 deletions functional/resources/json/omfg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OMFG!!!
21 changes: 21 additions & 0 deletions functional/resources/yaml/stubs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,27 @@
body: OMFG!!!


- request:
method: [GET]
url: /uri/with/sequenced/responses/infile

response:
- status: 201
headers:
content-type: application/json
file: ../json/ok.json

- status: 201
headers:
content-stype: application/json
file: ../json/goingstrong.json

- status: 500
headers:
content-type: application/json
file: ../json/omfg.json



- request:
method: [GET]
Expand Down
39 changes: 25 additions & 14 deletions main/java/by/stub/yaml/YamlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,8 @@ private <T> T unmarshallYamlMapToTargetStub(final Map<String, Object> yamlProper
methods.add(StringUtils.objectToString(rawPairValue));
massagedPairValue = methods;

} else if (pairKey.toLowerCase().equals(YAML_NODE_FILE)) {

final String filePath = StringUtils.objectToString(rawPairValue);
byte[] bytes = new byte[]{};

try {
bytes = FileUtils.fileToBytes(dataConfigHomeDirectory, filePath);
} catch (final IOException ex) {
ANSITerminal.error(ex.getMessage() + " " + FAILED_TO_LOAD_FILE_ERR);
}
massagedPairValue = bytes;
} else if (isDataProvidedInFile(pairKey)) {
massagedPairValue = readDataFromFile(rawPairValue);
} else {
massagedPairValue = StringUtils.objectToString(rawPairValue);
}
Expand All @@ -170,7 +161,25 @@ private <T> T unmarshallYamlMapToTargetStub(final Map<String, Object> yamlProper
return targetStub;
}

private void handleListNode(final StubHttpLifecycle stubHttpLifecycle, final Map.Entry<String, Object> parentNode) throws Exception {
private boolean isDataProvidedInFile(String pairKey) {
return pairKey.toLowerCase().equals(YAML_NODE_FILE);
}

private Object readDataFromFile(Object rawPairValue) throws IOException {
Object massagedPairValue;
final String filePath = StringUtils.objectToString(rawPairValue);
byte[] bytes = new byte[]{};

try {
bytes = FileUtils.fileToBytes(dataConfigHomeDirectory, filePath);
} catch (final IOException ex) {
ANSITerminal.error(ex.getMessage() + " " + FAILED_TO_LOAD_FILE_ERR);
}
massagedPairValue = bytes;
return massagedPairValue;
}

private void handleListNode(final StubHttpLifecycle stubHttpLifecycle, final Map.Entry<String, Object> parentNode) throws Exception {

final List yamlProperties = (List) parentNode.getValue();
final List<StubResponse> populatedResponseStub = unmarshallYamlListToTargetStub(yamlProperties, StubResponse.class);
Expand All @@ -187,8 +196,10 @@ private <T> List<T> unmarshallYamlListToTargetStub(final List yamlProperties, fi

for (final Map.Entry<String, Object> mapEntry : rawSequenceEntry.entrySet()) {
final String rawSequenceEntryKey = mapEntry.getKey();
final Object rawSequenceEntryValue = mapEntry.getValue();

Object rawSequenceEntryValue = mapEntry.getValue();
if(isDataProvidedInFile(rawSequenceEntryKey)){
rawSequenceEntryValue = readDataFromFile(rawSequenceEntryValue);
}
ReflectionUtils.setPropertyValue(targetStub, rawSequenceEntryKey, rawSequenceEntryValue);
}

Expand Down

0 comments on commit 93f76e3

Please sign in to comment.