Skip to content

Commit b88fdd4

Browse files
authored
Merge pull request #26 from DarwisNarvaezDev/RSE-852
RSE-852 Fix: Visible Error in HttpWorkflowNodeStepPlugin Output
2 parents cdc0eb9 + 8439d63 commit b88fdd4

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

src/main/java/edu/ohio/ais/rundeck/HttpBuilder.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ public void doRequest(Map<String, Object> options, HttpUriRequest request, Integ
125125
throw new StepException("Unable to complete request after maximum number of attempts.", StepFailureReason.IOFailure);
126126
}
127127
CloseableHttpResponse response = null;
128+
String output = "";
128129
try {
129130
response = this.getHttpClient(options).execute(request);
130131

@@ -135,15 +136,18 @@ public void doRequest(Map<String, Object> options, HttpUriRequest request, Integ
135136

136137
//print the response content
137138
if(options.containsKey("printResponse") && Boolean.parseBoolean(options.get("printResponse").toString())) {
138-
String output = this.prettyPrint(response);
139+
output = getOutputForResponse(this.prettyPrint(response));
139140
//print response
140141
log.log(2, output);
141142
}
142143

143144
if(options.containsKey("printResponseToFile") && Boolean.parseBoolean(options.get("printResponseToFile").toString())){
144-
String output = this.prettyPrint(response);
145145
File file = new File(options.get("file").toString());
146146
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
147+
if( output.isEmpty() ){
148+
output = getOutputForResponse(this.prettyPrint(response));
149+
}
150+
147151
writer.write (output);
148152

149153
//Close writer
@@ -244,6 +248,10 @@ public void doRequest(Map<String, Object> options, HttpUriRequest request, Integ
244248
}
245249
}
246250

251+
private String getOutputForResponse(String printer){
252+
return !printer.isEmpty() ? printer : "";
253+
}
254+
247255
private StringBuffer getPageContent(HttpResponse response) {
248256

249257
BufferedReader rd = null;

src/test/java/edu/ohio/ais/rundeck/HttpWorkflowNodeStepPluginTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@
1111
import com.github.tomakehurst.wiremock.client.WireMock;
1212
import com.github.tomakehurst.wiremock.junit.WireMockRule;
1313
import edu.ohio.ais.rundeck.util.OAuthClientTest;
14+
import org.apache.tools.ant.util.FileUtils;
15+
import org.junit.After;
1416
import org.junit.Before;
1517
import org.junit.Rule;
1618
import org.junit.Test;
1719
import org.mockito.Mockito;
1820

21+
import java.io.File;
22+
import java.io.IOException;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.nio.file.Paths;
1926
import java.util.HashMap;
2027
import java.util.Map;
2128

@@ -46,6 +53,8 @@ public class HttpWorkflowNodeStepPluginTest {
4653
protected PluginStepContext pluginContext;
4754
protected PluginLogger pluginLogger;
4855
protected INodeEntry node;
56+
protected File resourcePath = new File("src" + File.separator + "test" + File.separator + "resources");
57+
protected File testResource = new File(resourcePath + File.separator + "example.json");
4958

5059
/**
5160
* Setup options for simple execution for the given method.
@@ -92,9 +101,22 @@ public Map<String, Object> getOAuthOptions(String method) {
92101
return options;
93102
}
94103

104+
private static String readFileAsString(String filePath) throws IOException {
105+
Path path = Paths.get(filePath);
106+
byte[] bytes = Files.readAllBytes(path);
107+
return new String(bytes);
108+
}
109+
95110
@Rule
96111
public WireMockRule wireMockRule = new WireMockRule(18089);
97112

113+
@After
114+
public void tearDown(){
115+
if( Files.exists(this.testResource.toPath()) ){
116+
FileUtils.delete(this.testResource);
117+
}
118+
}
119+
98120
@Before
99121
public void setUp() {
100122
plugin = new HttpWorkflowNodeStepPlugin();
@@ -346,4 +368,34 @@ public void canPrintNoContent() throws NodeStepException {
346368

347369
this.plugin.executeNodeStep(pluginContext, options, node );
348370
}
371+
372+
@Test
373+
public void canPrintContentToFile() throws NodeStepException, IOException {
374+
Map<String, Object> options = new HashMap<>();
375+
376+
options.put("remoteUrl", OAuthClientTest.BASE_URI + NO_CONTENT_URL);
377+
options.put("method", "GET");
378+
options.put("printResponse",true);
379+
options.put("printResponseToFile",true);
380+
options.put("file", testResource);
381+
382+
assertNotNull(Paths.get(testResource.toString()));
383+
this.plugin.executeNodeStep(pluginContext, options, node );
384+
assertNotNull(readFileAsString(testResource.toString()));
385+
}
386+
387+
@Test
388+
public void canPrintContentToFileIfPrintResponseIsFalse() throws NodeStepException, IOException {
389+
Map<String, Object> options = new HashMap<>();
390+
391+
options.put("remoteUrl", OAuthClientTest.BASE_URI + NO_CONTENT_URL);
392+
options.put("method", "GET");
393+
options.put("printResponse",false);
394+
options.put("printResponseToFile",true);
395+
options.put("file", testResource);
396+
397+
assertNotNull(Paths.get(testResource.toString()));
398+
this.plugin.executeNodeStep(pluginContext, options, node );
399+
assertNotNull(readFileAsString(testResource.toString()));
400+
}
349401
}

0 commit comments

Comments
 (0)