
br.com.jhonsapp.bootstrap.test.restTemplate.command.PutCommand Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bootstrap-test Show documentation
Show all versions of bootstrap-test Show documentation
A complete architecture for creating and managing tests.
package br.com.jhonsapp.bootstrap.test.restTemplate.command;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
public class PutCommand {
private static final String authorizationKey = "Authorization";
private ObjectMapper mapper = new ObjectMapper();
private AuthenticationCommand authentication;
private Class typeClass;
private String requestMapping;
public PutCommand(AuthenticationCommand authentication, Class typeClass, String requestMapping) {
this.authentication = authentication;
this.typeClass = typeClass;
this.requestMapping = requestMapping;
}
private ResultActions obtainResultActions(String restMapping, MockMvc mockMvc, Object object) throws Exception {
MockHttpServletRequestBuilder builder = null;
if(StringUtils.hasText(restMapping))
builder = put("/" + requestMapping+ "/" + restMapping);
else
builder = put("/" + requestMapping);
//Add token authorization
builder.header(authorizationKey, authentication.obtainHeaderAuthorization(mockMvc))
.contentType("application/json;charset=UTF-8")
.accept("application/json;charset=UTF-8")
.content(convertToString(object));
return mockMvc.perform(builder);
}
public String convertToString(Object obj) throws Exception {
return mapper.writeValueAsString(obj);
}
private String getStringObject(String restMapping, MockMvc mockMvc, Object object) throws Exception {
ResultActions result = obtainResultActions(restMapping, mockMvc, object)
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(status().isOk());
return result.andReturn().getResponse().getContentAsString();
}
public T isOk(String restMapping, MockMvc mockMvc, Object object) throws Exception {
String resultString = getStringObject(restMapping, mockMvc, object);
return mapper.readValue(resultString, typeClass);
}
public T isOk(MockMvc mockMvc, Object object) throws Exception {
String resultString = getStringObject("", mockMvc, object);
return mapper.readValue(resultString, typeClass);
}
public void isBadRequest(String restMapping, MockMvc mockMvc, Object object) throws Exception {
obtainResultActions(restMapping, mockMvc, object).andExpect(status().isBadRequest());
}
public void isNotFound(String restMapping, MockMvc mockMvc, Object object) throws Exception {
obtainResultActions(restMapping, mockMvc, object).andExpect(status().isNotFound());
}
public void isNoContent(String restMapping, MockMvc mockMvc, Object object) throws Exception {
obtainResultActions(restMapping, mockMvc, object).andExpect(status().isNoContent());
}
public void isUnauthorized(String restMapping, MockMvc mockMvc, Object object) throws Exception {
obtainResultActions(restMapping, mockMvc, object).andExpect(status().isUnauthorized());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy