Skip to content

Commit

Permalink
Merge pull request #9 from resilva87/chore/fix-tests
Browse files Browse the repository at this point in the history
Fix unit and integration tests
  • Loading branch information
andreformentoc authored Aug 15, 2017
2 parents bcb365c + 72ce461 commit 6adc41a
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
import org.springframework.http.*;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
Expand All @@ -29,76 +30,76 @@
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class GenericApiControllerExternalIntegrationTest {

@Autowired
private TestRestTemplate restTemplate;

@Value("${file.base}")
private String fileBase;
@Value("${file.extension}")
private String fileExtension;

private URL resource;

@Before
public void init() {
this.resource = getClass().getClassLoader().getResource(fileBase);
}

@Test
public void shouldFileExistsInTest() {
assertNotNull(resource);
assertNotNull(resource.getFile());
}

private String getJson(String fileNameExpected) throws IOException {
return new String(Files.readAllBytes(Paths.get(fileNameExpected)));
}

@Test(timeout = 10000)
public void shouldResolvePostWithExternalMock() throws IOException, JSONException {
shouldResolveWithExternalMock(HttpMethod.POST);
}

@Test(timeout = 5000)
public void shouldResolvePathWithExternalMock() throws IOException, JSONException {
shouldResolveWithExternalMock(HttpMethod.PATCH);
}

private void shouldResolveWithExternalMock(final HttpMethod httpMethod) throws IOException, JSONException {
final ImmutableMap<String, String> headers = ImmutableMap.<String, String>builder()
.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build();

shouldResolveWithExternalMock(httpMethod, Optional.of(headers));
}

private void shouldResolveWithExternalMock(final HttpMethod httpMethod, final Optional<Map<String, String>> headers)
throws IOException, JSONException {
// given
final String url = "/v2/57fbd6280f0000ed154fd470";

final HttpStatus httpStatus = HttpStatus.OK;
final String fileName = resource.getFile().concat("/").concat(httpMethod.name().toLowerCase()).concat(url)
.concat("/1").concat(fileExtension);

final EndpointDto endpointDto = new Gson().fromJson(getJson(fileName), EndpointDto.class);
final String requestJson = new Gson().toJson(endpointDto.getRequest().getBody());
final String responseJson = new Gson().toJson(endpointDto.getResponse().getBody());

final HttpHeaders httpHeaders = headers.filter(mapHeaders -> !mapHeaders.isEmpty()).map(map -> {
final HttpHeaders result = new HttpHeaders();
result.setContentType(MediaType.APPLICATION_JSON);
return result;
}).orElse(null);

final HttpEntity<String> httpEntity = new HttpEntity<>(requestJson, httpHeaders);

// when
final ResponseEntity<String> response = restTemplate.exchange(url, httpMethod, httpEntity, String.class);

// then
assertEquals(httpStatus, response.getStatusCode());
JSONAssert.assertEquals(responseJson, response.getBody(), false);
assertNotNull(response.getHeaders().get("Access-Control-Allow-Origin"));
}
@Autowired
private TestRestTemplate restTemplate;

@Value("${file.base}")
private String fileBase;
@Value("${file.extension}")
private String fileExtension;

private File resource;

@Before
public void init() throws URISyntaxException {
this.resource = Paths.get(getClass().getClassLoader().getResource(fileBase).toURI()).toFile();
}

@Test
public void shouldFileExistsInTest() {
assertNotNull(resource);
}

private String getJson(String fileNameExpected) throws IOException {
return new String(Files.readAllBytes(Paths.get(fileNameExpected)));
}

@Test(timeout = 10000)
public void shouldResolvePostWithExternalMock() throws IOException, JSONException {
shouldResolveWithExternalMock(HttpMethod.POST);
}

@Test(timeout = 5000)
public void shouldResolvePathWithExternalMock() throws IOException, JSONException {
shouldResolveWithExternalMock(HttpMethod.PATCH);
}

private void shouldResolveWithExternalMock(final HttpMethod httpMethod) throws IOException, JSONException {
final ImmutableMap<String, String> headers = ImmutableMap.<String, String>builder()
.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build();

shouldResolveWithExternalMock(httpMethod, Optional.of(headers));
}

private void shouldResolveWithExternalMock(final HttpMethod httpMethod, final Optional<Map<String, String>> headers)
throws IOException, JSONException {
// given
final String url = "/v2/57fbd6280f0000ed154fd470";

final HttpStatus httpStatus = HttpStatus.OK;
final String fileName =
Paths.get(resource.getAbsolutePath(), httpMethod.name().toLowerCase(), url, "1" + fileExtension)
.toAbsolutePath().toString();

final EndpointDto endpointDto = new Gson().fromJson(getJson(fileName), EndpointDto.class);
final String requestJson = new Gson().toJson(endpointDto.getRequest().getBody());
final String responseJson = new Gson().toJson(endpointDto.getResponse().getBody());

final HttpHeaders httpHeaders = headers.filter(mapHeaders -> !mapHeaders.isEmpty()).map(map -> {
final HttpHeaders result = new HttpHeaders();
result.setContentType(MediaType.APPLICATION_JSON);
return result;
}).orElse(null);

final HttpEntity<String> httpEntity = new HttpEntity<>(requestJson, httpHeaders);

// when
final ResponseEntity<String> response = restTemplate.exchange(url, httpMethod, httpEntity, String.class);

// then
assertEquals(httpStatus, response.getStatusCode());
JSONAssert.assertEquals(responseJson, response.getBody(), false);
assertNotNull(response.getHeaders().get("Access-Control-Allow-Origin"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -41,17 +43,16 @@ public class GenericApiControllerIntegrationTest {
@Value("${file.base}")
private String fileBase;

private URL resource;
private File resource;

@Before
public void init() {
this.resource = getClass().getClassLoader().getResource(fileBase.concat("/"));
public void init() throws URISyntaxException {
this.resource = Paths.get(getClass().getClassLoader().getResource(fileBase).toURI()).toFile();
}

@Test
public void shouldFileExistsInTest() {
assertNotNull(resource);
assertNotNull(resource.getFile());
}

private String getJson(String fileNameExpected) throws IOException {
Expand All @@ -61,7 +62,9 @@ private String getJson(String fileNameExpected) throws IOException {

private void shouldResolveGetWithLocalMockMatchQueryCaseX(String uri, String caseX) throws IOException, JSONException {
// given
final String fileName = resource.getFile().concat("get").concat(uri).concat("/").concat(caseX).concat(fileExtension);
final String fileName =
Paths.get(resource.getAbsolutePath(), "get", uri, caseX + fileExtension)
.toAbsolutePath().toString();

final String endpointJson = getJson(fileName);

Expand Down Expand Up @@ -89,7 +92,9 @@ private void shouldResolveGetWithLocalMockMatchQueryCaseX(String uri, String cas
public void shouldResolveGetWithSimpleResponseWithoutRequest() throws IOException, JSONException {
// given
final String uri = "/guests/132/users/21/cc";
final String fileName = resource.getFile().concat("get" + uri + "/1" + fileExtension);
final String fileName =
Paths.get(resource.getAbsolutePath(), "get", uri, "1" + fileExtension)
.toAbsolutePath().toString();
final String endpointJson = getJson(fileName);
final EndpointDto endpointDto = new Gson().fromJson(endpointJson, EndpointDto.class);
final String responseJson = new Gson().toJson(endpointDto.getResponse().getBody());
Expand All @@ -107,7 +112,9 @@ public void shouldResolveGetWithSimpleResponseWithoutRequest() throws IOExceptio
public void shouldResolveGetWithSimpleResponseWithRequest() throws IOException, JSONException {
// given
final String uri = "/guests/132/users/22/cc";
final String fileName = resource.getFile().concat("get" + uri + "/1" + fileExtension);
final String fileName =
Paths.get(resource.getAbsolutePath(), "get", uri, "1" + fileExtension)
.toAbsolutePath().toString();
final String endpointJson = getJson(fileName);
final EndpointDto endpointDto = new Gson().fromJson(endpointJson, EndpointDto.class);
final String responseJson = new Gson().toJson(endpointDto.getResponse().getBody());
Expand All @@ -126,7 +133,9 @@ public void shouldResolveGetWithLocalMock() throws IOException, JSONException {
// given
final String uri = "/users/123";

final String fileName = resource.getFile().concat("get").concat(uri).concat("/1").concat(fileExtension);
final String fileName =
Paths.get(resource.getAbsolutePath(), "get", uri, "1" + fileExtension)
.toAbsolutePath().toString();
final EndpointDto endpointDto = new Gson().fromJson(getJson(fileName), EndpointDto.class);
final String responseJson = new Gson().toJson(endpointDto.getResponse().getBody());

Expand All @@ -143,7 +152,9 @@ public void shouldResolveWithHttpStatusCreated() throws IOException, JSONExcepti
// given
final String uri = "/users/123";

final String fileName = resource.getFile().concat("get").concat(uri).concat("/2").concat(fileExtension);
final String fileName =
Paths.get(resource.getAbsolutePath(), "get", uri, "2" + fileExtension)
.toAbsolutePath().toString();
final EndpointDto endpointDto = new Gson().fromJson(getJson(fileName), EndpointDto.class);
final String responseJson = new Gson().toJson(endpointDto.getResponse().getBody());
final String query = queryStringBuilder.fromMap(endpointDto.getRequest().getQuery());
Expand Down Expand Up @@ -183,7 +194,9 @@ private void shouldResolvePostWithLocalMockMatcheRequest(final String url, final

private void shouldResolveWithLocalMockMatcheRequest(final String uri, final String caseX, final HttpStatus httpStatus, HttpMethod httpMethod) throws IOException, JSONException {
// given
final String fileName = resource.getFile().concat(httpMethod.name().toLowerCase()).concat(uri).concat("/").concat(caseX).concat(fileExtension);
final String fileName =
Paths.get(resource.getAbsolutePath(), httpMethod.name().toLowerCase(), uri, caseX + fileExtension)
.toAbsolutePath().toString();

final EndpointDto endpointDto = new Gson().fromJson(getJson(fileName), EndpointDto.class);
final String requestJson = new Gson().toJson(endpointDto.getRequest().getBody());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void shouldConvertResponse() {
assertNotNull(endpoint);
assertNotNull(endpoint.getResponse());
assertNotNull(endpoint.getResponse().getBody());
assertEquals("[{\"age\":8.0}]", endpoint.getResponse().getBody());
assertEquals("[{\"age\":8}]", endpoint.getResponse().getBody());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void shouldConvertFromJsonAListOfObjects() {
// then
assertNotNull(response);
assertNotNull(response.getBody());
assertEquals("[{\"age\":10.0},{\"age\":11.0}]", response.getBody());
assertEquals("[{\"age\":10},{\"age\":11}]", response.getBody());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Optional;

Expand All @@ -47,7 +49,9 @@ public class EndpointRepositoryModelTest {
private BaseFileNameBuilderModel baseFileNameBuilder;
@Mock
private EndpointFileFilterRequest endpointMockFileFilterRequest;
private URL resource;

private File resource;

@Value("${file.base}")
private String fileBase;
@Value("${file.extension}")
Expand All @@ -59,22 +63,21 @@ public static void initClass() {
}

@Before
public void init() {
this.resource = getClass().getClassLoader().getResource(fileBase.concat(NAME));
public void init() throws URISyntaxException {
this.resource = Paths.get(getClass().getClassLoader().getResource(fileBase).toURI()).toFile();
}

@Test
public void shouldFileExistsInTest() {
assertNotNull(resource);
assertNotNull(resource.getFile());
}

@Test
public void shouldFindSomeResponse() throws IOException {
// given
final RequestMethod requestMethod = RequestMethod.GET;
final String requestUrl = "person/11";
final String basePath = resource.getFile() + requestUrl;
final String basePath = Paths.get(resource.getAbsolutePath(), requestMethod.toString(), requestUrl).toAbsolutePath().toString();
final Optional<Endpoint> endpoint = Optional.of(Fixture.from(Endpoint.class).gimme(EndpointTemplate.VALID));

// when
Expand All @@ -94,7 +97,7 @@ public void shouldNotFindResponseWhenDoNotExists() throws IOException {
// given
final RequestMethod requestMethod = RequestMethod.GET;
final String requestUrl = "/person/66";
final String basePath = resource.getFile() + requestUrl;
final String basePath = Paths.get(resource.getAbsolutePath(), requestUrl).toAbsolutePath().toString();

// when
when(baseFileNameBuilder.buildPath(any(), any())).thenReturn(basePath);
Expand All @@ -110,7 +113,7 @@ public void shouldNotFindResponseWhenDoNotExists() throws IOException {
public void shouldFilterByMethodAndUriAndQuery() {
// given
final String requestUrl = "person/11";
final String basePath = resource.getFile() + requestUrl;
final String basePath = Paths.get(resource.getAbsolutePath(), requestUrl).toAbsolutePath().toString();
final Optional<Endpoint> result = Optional.empty();

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.containsString;
Expand All @@ -24,26 +27,26 @@ public class FileJsonReaderTest {
@InjectMocks
private FileJsonReader fileJsonReader;

private URL resource;
private File resource;

@Value("${file.base}")
private String fileBase;

@Before
public void init() {
this.resource = getClass().getClassLoader().getResource(fileBase.concat(NAME));
public void init() throws URISyntaxException {
File dir = Paths.get(getClass().getClassLoader().getResource(fileBase).toURI()).toFile();
this.resource = Paths.get(dir.getAbsolutePath(), NAME).toFile();
}

@Test
public void shouldFileExistsInTest() {
assertNotNull(resource);
assertNotNull(resource.getFile());
}

@Test
public void shouldReadFile() throws IOException {
// when
final Optional<String> jsonFile = fileJsonReader.getJsonByFileName(resource.getFile());
final Optional<String> jsonFile = fileJsonReader.getJsonByFileName(resource.getAbsolutePath());

// then
assertTrue(jsonFile.isPresent());
Expand All @@ -53,7 +56,10 @@ public void shouldReadFile() throws IOException {
@Test
public void shouldReturnEmptyWhenFileNotFound() throws IOException {
// when
final Optional<String> jsonFile = fileJsonReader.getJsonByFileName(resource.getFile() + "-file-not-found");
final Optional<String> jsonFile =
fileJsonReader.getJsonByFileName(
Paths.get(resource.getAbsolutePath(), "-file-not-found"
).toAbsolutePath().toString());

// then
assertFalse(jsonFile.isPresent());
Expand Down
Loading

0 comments on commit 6adc41a

Please sign in to comment.