Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions src/main/java/io/crowdcode/maven/plugins/rancher/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONArray;
import org.springframework.http.*;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestClientResponseException;
import org.springframework.web.client.RestTemplate;

import java.io.File;
Expand Down Expand Up @@ -74,7 +79,7 @@ private void removeStack() {
log.info("About to delete the stack: {}",stackUrl);

try {
restTemplate.exchange(stackUrl,HttpMethod.POST,new HttpEntity(headers),String.class);
exchange(stackUrl,HttpMethod.POST,new HttpEntity(headers));
log.info("Stack {} successfully deleted",stackUrl);
url = "";
} catch( RuntimeException ex ) {
Expand Down Expand Up @@ -139,18 +144,39 @@ private void createStack() {

//Perform http request

responseEntity = exchange(stacksUrl, HttpMethod.POST, entity);
try {
responseEntity = restTemplate.exchange(stacksUrl,HttpMethod.POST,entity,String.class);
ReadContext ctx = JsonPath.parse(responseEntity.getBody());
url = ctx.read("links.self");
log.info("New stack successfully created");
} catch( RuntimeException ex ) {
ReadContext ctx = JsonPath.parse(responseEntity.getBody());
url = ctx.read("links.self");
}
catch( RuntimeException ex ) {
log.error("Error while parsing stack payload to json {}",ex);
throw ex;
}
log.info("New stack successfully created");

}

private ResponseEntity<String> exchange(String url, HttpMethod httpMethod, HttpEntity<String> entity) {
ResponseEntity<String> responseEntity;
try {

responseEntity = restTemplate.exchange(url, httpMethod, entity, String.class);
}
catch (RestClientResponseException rcre)
{
log.error("Error while parsing stack payload to json {}",rcre);
String responseBodyAsString = rcre.getResponseBodyAsString();
if (!StringUtils.isEmpty(responseBodyAsString))
{
//Let caller know the response body due tu receive detail informations in case of an error.
log.error("Response Body: {}", responseBodyAsString);
}
throw rcre;
}
return responseEntity;
}

/**
* wait N milliseconds
*
Expand All @@ -175,9 +201,8 @@ private String verify() {

log.info("Verify the stack: {}",getName());

ResponseEntity<String> responseEntity =exchange(url, HttpMethod.GET, new HttpEntity(headers));
try {
ResponseEntity<String> responseEntity = restTemplate.exchange(url,HttpMethod.GET,new HttpEntity(headers),String.class);

ReadContext ctx = JsonPath.parse(responseEntity.getBody());
String state = ctx.read("state");
log.info("State={}",state);
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/io/crowdcode/maven/plugins/rancher/StackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,30 @@
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import lombok.extern.slf4j.Slf4j;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import org.springframework.http.*;
import org.springframework.web.client.RestClientResponseException;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;

@Slf4j
Expand All @@ -46,6 +52,9 @@ public void before(){

@Mock
private RestTemplate restTemplate;

@Mock
RestClientResponseException clientResponseException;

private static Stack defaultStack() {
Stack stack = new Stack();
Expand Down Expand Up @@ -147,4 +156,29 @@ Matchers.<Class<String>> any())
}
}

@Test
public void runVerifyFailStackTestWithResponseBody() {
try {
String environmentResponse = Resources.toString(enviromentResponseUrl,Charsets.UTF_8);

Stack s = defaultStack();
when(restTemplate.exchange(
Matchers.<String>any(),
Matchers.any(),
Matchers.any(),
Matchers.<Class<?>> any())
).thenThrow(clientResponseException);

s.init(restTemplate,headers,environmentResponse,"default");
s.setActions("verify");
try {
s.run();
Assert.fail("Exception must be thrown here");
} catch (RestClientResponseException e) {
}
Mockito.verify(clientResponseException, times(1)).getResponseBodyAsString();
} catch( IOException e ) {
Assert.fail(e.getMessage());
}
}
}