Skip to content

Commit 6313b01

Browse files
author
Joao Felipe Santoro Pinto
authored
Merge pull request #1 from concretesolutions/master
merging parent fork
2 parents 9102b98 + ab1254d commit 6313b01

File tree

13 files changed

+134
-128
lines changed

13 files changed

+134
-128
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ application-*.yml
1515
*.iws
1616
*.iml
1717
*.ipr
18+
*.DS_Store
1819

1920
### NetBeans ###
2021
nbproject/private/

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[![Build Status](https://travis-ci.org/concretesolutions/mock-api.svg?branch=master)](https://travis-ci.org/concretesolutions/mock-api)
22
[![codecov.io](https://codecov.io/github/concretesolutions/mock-api/coverage.svg?branch=master)](https://codecov.io/github/concretesolutions/mock-api?branch=master)
3+
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/67cdddf44d87495c84e3bddfdb5de074)](https://www.codacy.com/app/jfelipesp/mock-api?utm_source=github.com&utm_medium=referral&utm_content=concretesolutions/mock-api&utm_campaign=Badge_Grade)
34

45
# Mock API
56

src/main/java/br/com/concrete/mock/generic/repository/impl/EndpointRepositoryBackup.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import br.com.concrete.mock.generic.repository.EndpointRepository;
55
import br.com.concrete.mock.infra.component.file.BaseFileNameBuilder;
66
import br.com.concrete.mock.infra.property.FileExtensionProperty;
7-
import br.com.concrete.mock.infra.property.FileProperty;
87
import org.springframework.beans.factory.annotation.Autowired;
98
import org.springframework.beans.factory.annotation.Qualifier;
109
import org.springframework.stereotype.Repository;
@@ -13,9 +12,8 @@
1312
public class EndpointRepositoryBackup extends EndpointRepositoryBase implements EndpointRepository {
1413

1514
@Autowired
16-
public EndpointRepositoryBackup(@Qualifier("FilePropertyBackup") FileProperty fileProperty, FileExtensionProperty fileExtensionProperty, EndpointMapper endpointMapper, @Qualifier("BaseFileNameBuilderBackup") BaseFileNameBuilder baseFileNameBuilder, EndpointFileFilterRequest endpointFileFilterRequest) {
15+
public EndpointRepositoryBackup(FileExtensionProperty fileExtensionProperty, EndpointMapper endpointMapper, @Qualifier("BaseFileNameBuilderBackup") BaseFileNameBuilder baseFileNameBuilder, EndpointFileFilterRequest endpointFileFilterRequest) {
1716
super(
18-
fileProperty,
1917
fileExtensionProperty,
2018
endpointMapper,
2119
baseFileNameBuilder,

src/main/java/br/com/concrete/mock/generic/repository/impl/EndpointRepositoryBase.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import br.com.concrete.mock.generic.repository.EndpointRepository;
77
import br.com.concrete.mock.infra.component.file.BaseFileNameBuilder;
88
import br.com.concrete.mock.infra.property.FileExtensionProperty;
9-
import br.com.concrete.mock.infra.property.FileProperty;
109
import com.google.common.collect.ImmutableList;
1110
import org.slf4j.Logger;
1211
import org.slf4j.LoggerFactory;
@@ -27,14 +26,12 @@ public class EndpointRepositoryBase implements EndpointRepository {
2726

2827
private static final Logger LOGGER = LoggerFactory.getLogger(EndpointRepositoryBase.class);
2928

30-
private final FileProperty fileProperty;
3129
private final FileExtensionProperty fileExtensionProperty;
3230
private final EndpointMapper endpointMapper;
3331
private final BaseFileNameBuilder baseFileNameBuilder;
3432
private final EndpointFileFilterRequest endpointFileFilterRequest;
3533

36-
public EndpointRepositoryBase(FileProperty fileProperty, FileExtensionProperty fileExtensionProperty, EndpointMapper endpointMapper, BaseFileNameBuilder baseFileNameBuilder, EndpointFileFilterRequest endpointFileFilterRequest) {
37-
this.fileProperty = fileProperty;
34+
public EndpointRepositoryBase(FileExtensionProperty fileExtensionProperty, EndpointMapper endpointMapper, BaseFileNameBuilder baseFileNameBuilder, EndpointFileFilterRequest endpointFileFilterRequest) {
3835
this.fileExtensionProperty = fileExtensionProperty;
3936
this.endpointMapper = endpointMapper;
4037
this.baseFileNameBuilder = baseFileNameBuilder;

src/main/java/br/com/concrete/mock/generic/repository/impl/EndpointRepositoryModel.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import br.com.concrete.mock.generic.repository.EndpointRepository;
55
import br.com.concrete.mock.infra.component.file.BaseFileNameBuilder;
66
import br.com.concrete.mock.infra.property.FileExtensionProperty;
7-
import br.com.concrete.mock.infra.property.FileProperty;
87
import org.springframework.beans.factory.annotation.Autowired;
98
import org.springframework.beans.factory.annotation.Qualifier;
109
import org.springframework.stereotype.Repository;
@@ -13,9 +12,8 @@
1312
public class EndpointRepositoryModel extends EndpointRepositoryBase implements EndpointRepository {
1413

1514
@Autowired
16-
public EndpointRepositoryModel(@Qualifier("FilePropertyModel") FileProperty fileProperty, FileExtensionProperty fileExtensionProperty, EndpointMapper endpointMapper, @Qualifier("BaseFileNameBuilderModel") BaseFileNameBuilder baseFileNameBuilder, EndpointFileFilterRequest endpointFileFilterRequest) {
15+
public EndpointRepositoryModel(FileExtensionProperty fileExtensionProperty, EndpointMapper endpointMapper, @Qualifier("BaseFileNameBuilderModel") BaseFileNameBuilder baseFileNameBuilder, EndpointFileFilterRequest endpointFileFilterRequest) {
1716
super(
18-
fileProperty,
1917
fileExtensionProperty, endpointMapper,
2018
baseFileNameBuilder,
2119
endpointFileFilterRequest

src/main/java/br/com/concrete/mock/infra/component/file/BaseFileNameBuilderBase.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ public BaseFileNameBuilderBase(FileProperty fileProperty) {
1212
}
1313

1414
public String buildPath(RequestMethod requestMethod, String pathUri) {
15-
return buildPath(fileProperty.getFileBase(), requestMethod.name().toLowerCase(), pathUri);
15+
return buildPath(fileProperty.getFileBase(), requestMethod.name(), pathUri);
1616
}
1717

1818
public String buildPath(String fileBaseName, String methodName, String pathUri) {
1919
return new StringBuilder()
2020
.append(fileBaseName)
2121
.append("/")
22-
.append(methodName)
22+
.append(methodName.toLowerCase())
2323
.append(pathUri)
2424
.toString();
2525
}

src/test/java/br/com/concrete/mock/generic/api/v1/controller/GenericApiControllerExternalIntegrationTest.java

+73-72
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
import org.springframework.http.*;
1616
import org.springframework.test.context.junit4.SpringRunner;
1717

18+
import java.io.File;
1819
import java.io.IOException;
19-
import java.net.URL;
20+
import java.net.URISyntaxException;
2021
import java.nio.file.Files;
2122
import java.nio.file.Paths;
2223
import java.util.Map;
@@ -29,76 +30,76 @@
2930
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
3031
public class GenericApiControllerExternalIntegrationTest {
3132

32-
@Autowired
33-
private TestRestTemplate restTemplate;
34-
35-
@Value("${file.base}")
36-
private String fileBase;
37-
@Value("${file.extension}")
38-
private String fileExtension;
39-
40-
private URL resource;
41-
42-
@Before
43-
public void init() {
44-
this.resource = getClass().getClassLoader().getResource(fileBase);
45-
}
46-
47-
@Test
48-
public void shouldFileExistsInTest() {
49-
assertNotNull(resource);
50-
assertNotNull(resource.getFile());
51-
}
52-
53-
private String getJson(String fileNameExpected) throws IOException {
54-
return new String(Files.readAllBytes(Paths.get(fileNameExpected)));
55-
}
56-
57-
@Test(timeout = 10000)
58-
public void shouldResolvePostWithExternalMock() throws IOException, JSONException {
59-
shouldResolveWithExternalMock(HttpMethod.POST);
60-
}
61-
62-
@Test(timeout = 5000)
63-
public void shouldResolvePathWithExternalMock() throws IOException, JSONException {
64-
shouldResolveWithExternalMock(HttpMethod.PATCH);
65-
}
66-
67-
private void shouldResolveWithExternalMock(final HttpMethod httpMethod) throws IOException, JSONException {
68-
final ImmutableMap<String, String> headers = ImmutableMap.<String, String>builder()
69-
.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build();
70-
71-
shouldResolveWithExternalMock(httpMethod, Optional.of(headers));
72-
}
73-
74-
private void shouldResolveWithExternalMock(final HttpMethod httpMethod, final Optional<Map<String, String>> headers)
75-
throws IOException, JSONException {
76-
// given
77-
final String url = "/v2/57fbd6280f0000ed154fd470";
78-
79-
final HttpStatus httpStatus = HttpStatus.OK;
80-
final String fileName = resource.getFile().concat("/").concat(httpMethod.name().toLowerCase()).concat(url)
81-
.concat("/1").concat(fileExtension);
82-
83-
final EndpointDto endpointDto = new Gson().fromJson(getJson(fileName), EndpointDto.class);
84-
final String requestJson = new Gson().toJson(endpointDto.getRequest().getBody());
85-
final String responseJson = new Gson().toJson(endpointDto.getResponse().getBody());
86-
87-
final HttpHeaders httpHeaders = headers.filter(mapHeaders -> !mapHeaders.isEmpty()).map(map -> {
88-
final HttpHeaders result = new HttpHeaders();
89-
result.setContentType(MediaType.APPLICATION_JSON);
90-
return result;
91-
}).orElse(null);
92-
93-
final HttpEntity<String> httpEntity = new HttpEntity<>(requestJson, httpHeaders);
94-
95-
// when
96-
final ResponseEntity<String> response = restTemplate.exchange(url, httpMethod, httpEntity, String.class);
97-
98-
// then
99-
assertEquals(httpStatus, response.getStatusCode());
100-
JSONAssert.assertEquals(responseJson, response.getBody(), false);
101-
assertNotNull(response.getHeaders().get("Access-Control-Allow-Origin"));
102-
}
33+
@Autowired
34+
private TestRestTemplate restTemplate;
35+
36+
@Value("${file.base}")
37+
private String fileBase;
38+
@Value("${file.extension}")
39+
private String fileExtension;
40+
41+
private File resource;
42+
43+
@Before
44+
public void init() throws URISyntaxException {
45+
this.resource = Paths.get(getClass().getClassLoader().getResource(fileBase).toURI()).toFile();
46+
}
47+
48+
@Test
49+
public void shouldFileExistsInTest() {
50+
assertNotNull(resource);
51+
}
52+
53+
private String getJson(String fileNameExpected) throws IOException {
54+
return new String(Files.readAllBytes(Paths.get(fileNameExpected)));
55+
}
56+
57+
@Test(timeout = 10000)
58+
public void shouldResolvePostWithExternalMock() throws IOException, JSONException {
59+
shouldResolveWithExternalMock(HttpMethod.POST);
60+
}
61+
62+
@Test(timeout = 5000)
63+
public void shouldResolvePathWithExternalMock() throws IOException, JSONException {
64+
shouldResolveWithExternalMock(HttpMethod.PATCH);
65+
}
66+
67+
private void shouldResolveWithExternalMock(final HttpMethod httpMethod) throws IOException, JSONException {
68+
final ImmutableMap<String, String> headers = ImmutableMap.<String, String>builder()
69+
.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build();
70+
71+
shouldResolveWithExternalMock(httpMethod, Optional.of(headers));
72+
}
73+
74+
private void shouldResolveWithExternalMock(final HttpMethod httpMethod, final Optional<Map<String, String>> headers)
75+
throws IOException, JSONException {
76+
// given
77+
final String url = "/v2/57fbd6280f0000ed154fd470";
78+
79+
final HttpStatus httpStatus = HttpStatus.OK;
80+
final String fileName =
81+
Paths.get(resource.getAbsolutePath(), httpMethod.name().toLowerCase(), url, "1" + fileExtension)
82+
.toAbsolutePath().toString();
83+
84+
final EndpointDto endpointDto = new Gson().fromJson(getJson(fileName), EndpointDto.class);
85+
final String requestJson = new Gson().toJson(endpointDto.getRequest().getBody());
86+
final String responseJson = new Gson().toJson(endpointDto.getResponse().getBody());
87+
88+
final HttpHeaders httpHeaders = headers.filter(mapHeaders -> !mapHeaders.isEmpty()).map(map -> {
89+
final HttpHeaders result = new HttpHeaders();
90+
result.setContentType(MediaType.APPLICATION_JSON);
91+
return result;
92+
}).orElse(null);
93+
94+
final HttpEntity<String> httpEntity = new HttpEntity<>(requestJson, httpHeaders);
95+
96+
// when
97+
final ResponseEntity<String> response = restTemplate.exchange(url, httpMethod, httpEntity, String.class);
98+
99+
// then
100+
assertEquals(httpStatus, response.getStatusCode());
101+
JSONAssert.assertEquals(responseJson, response.getBody(), false);
102+
assertNotNull(response.getHeaders().get("Access-Control-Allow-Origin"));
103+
}
103104

104105
}

src/test/java/br/com/concrete/mock/generic/api/v1/controller/GenericApiControllerIntegrationTest.java

+23-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
import org.springframework.test.context.junit4.SpringRunner;
1818
import org.springframework.web.bind.annotation.RequestMethod;
1919

20+
import java.io.File;
2021
import java.io.IOException;
22+
import java.net.URISyntaxException;
2123
import java.net.URL;
2224
import java.nio.file.Files;
2325
import java.nio.file.Path;
@@ -41,17 +43,16 @@ public class GenericApiControllerIntegrationTest {
4143
@Value("${file.base}")
4244
private String fileBase;
4345

44-
private URL resource;
46+
private File resource;
4547

4648
@Before
47-
public void init() {
48-
this.resource = getClass().getClassLoader().getResource(fileBase.concat("/"));
49+
public void init() throws URISyntaxException {
50+
this.resource = Paths.get(getClass().getClassLoader().getResource(fileBase).toURI()).toFile();
4951
}
5052

5153
@Test
5254
public void shouldFileExistsInTest() {
5355
assertNotNull(resource);
54-
assertNotNull(resource.getFile());
5556
}
5657

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

6263
private void shouldResolveGetWithLocalMockMatchQueryCaseX(String uri, String caseX) throws IOException, JSONException {
6364
// given
64-
final String fileName = resource.getFile().concat("get").concat(uri).concat("/").concat(caseX).concat(fileExtension);
65+
final String fileName =
66+
Paths.get(resource.getAbsolutePath(), "get", uri, caseX + fileExtension)
67+
.toAbsolutePath().toString();
6568

6669
final String endpointJson = getJson(fileName);
6770

@@ -89,7 +92,9 @@ private void shouldResolveGetWithLocalMockMatchQueryCaseX(String uri, String cas
8992
public void shouldResolveGetWithSimpleResponseWithoutRequest() throws IOException, JSONException {
9093
// given
9194
final String uri = "/guests/132/users/21/cc";
92-
final String fileName = resource.getFile().concat("get" + uri + "/1" + fileExtension);
95+
final String fileName =
96+
Paths.get(resource.getAbsolutePath(), "get", uri, "1" + fileExtension)
97+
.toAbsolutePath().toString();
9398
final String endpointJson = getJson(fileName);
9499
final EndpointDto endpointDto = new Gson().fromJson(endpointJson, EndpointDto.class);
95100
final String responseJson = new Gson().toJson(endpointDto.getResponse().getBody());
@@ -107,7 +112,9 @@ public void shouldResolveGetWithSimpleResponseWithoutRequest() throws IOExceptio
107112
public void shouldResolveGetWithSimpleResponseWithRequest() throws IOException, JSONException {
108113
// given
109114
final String uri = "/guests/132/users/22/cc";
110-
final String fileName = resource.getFile().concat("get" + uri + "/1" + fileExtension);
115+
final String fileName =
116+
Paths.get(resource.getAbsolutePath(), "get", uri, "1" + fileExtension)
117+
.toAbsolutePath().toString();
111118
final String endpointJson = getJson(fileName);
112119
final EndpointDto endpointDto = new Gson().fromJson(endpointJson, EndpointDto.class);
113120
final String responseJson = new Gson().toJson(endpointDto.getResponse().getBody());
@@ -126,7 +133,9 @@ public void shouldResolveGetWithLocalMock() throws IOException, JSONException {
126133
// given
127134
final String uri = "/users/123";
128135

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

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

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

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

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

src/test/java/br/com/concrete/mock/generic/mapper/EndpointDtoTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void shouldConvertRequest() {
4141
@Test
4242
public void shouldConvertResponse() {
4343
// given
44-
final String json = "{\"request\":{\"body\":[{\"run\":\"7\"}]},\"response\":{\"body\":[{\"age\":8.0}]}}";
44+
final String json = "{\"request\":{\"body\":[{\"run\":\"7\"}]},\"response\":{\"body\":[{\"age\":8}]}}";
4545

4646
// when
4747
final EndpointDto endpointDto = new Gson().fromJson(json, EndpointDto.class);
@@ -51,7 +51,7 @@ public void shouldConvertResponse() {
5151
assertNotNull(endpoint);
5252
assertNotNull(endpoint.getResponse());
5353
assertNotNull(endpoint.getResponse().getBody());
54-
assertEquals("[{\"age\":8.0}]", endpoint.getResponse().getBody());
54+
assertEquals("[{\"age\":8}]", endpoint.getResponse().getBody());
5555
}
5656

5757
@Test

src/test/java/br/com/concrete/mock/generic/mapper/ResponseDtoTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void shouldConvertFromJsonAnObject() throws JSONException {
3939
@Test
4040
public void shouldConvertFromJsonAListOfObjects() {
4141
// given
42-
final String json = "{ \"body\": [{ \"age\": 10.0 }, { \"age\": 11.0 }] }";
42+
final String json = "{ \"body\": [{ \"age\": 10 }, { \"age\": 11 }] }";
4343

4444
// when
4545
final ResponseDto responseDto = new Gson().fromJson(json, ResponseDto.class);
@@ -48,7 +48,7 @@ public void shouldConvertFromJsonAListOfObjects() {
4848
// then
4949
assertNotNull(response);
5050
assertNotNull(response.getBody());
51-
assertEquals("[{\"age\":10.0},{\"age\":11.0}]", response.getBody());
51+
assertEquals("[{\"age\":10},{\"age\":11}]", response.getBody());
5252
}
5353

5454
@Test

0 commit comments

Comments
 (0)