-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Postman에서 Mock Server과 API 만들고 테스트하기
- Loading branch information
Showing
7 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/what/the/springdatajpa/RestTemplateConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package what.the.springdatajpa; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.client.BufferingClientHttpRequestFactory; | ||
import org.springframework.http.client.SimpleClientHttpRequestFactory; | ||
import org.springframework.http.converter.StringHttpMessageConverter; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.nio.charset.Charset; | ||
import java.time.Duration; | ||
|
||
@Configuration | ||
public class RestTemplateConfig { | ||
|
||
@Bean | ||
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) { | ||
return restTemplateBuilder | ||
.requestFactory(() -> new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())) | ||
.setConnectTimeout(Duration.ofMillis(5000)) // connection-timeout | ||
.setReadTimeout(Duration.ofMillis(5000)) // read-timeout | ||
.additionalMessageConverters(new StringHttpMessageConverter(Charset.forName("UTF-8"))) | ||
.build(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/what/the/springdatajpa/resttemplate/ApiService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package what.the.springdatajpa.resttemplate; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.*; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
|
||
@Service | ||
@AllArgsConstructor | ||
@Slf4j | ||
public class ApiService<T> { | ||
|
||
private final RestTemplate restTemplate; | ||
|
||
// @Autowired | ||
// public ApiService(RestTemplate restTemplate) { | ||
// this.restTemplate = restTemplate; | ||
// } | ||
/* | ||
public ResponseEntity<T> get(String url, HttpHeaders httpHeaders) { | ||
return callApiEndpoint(url, HttpMethod.GET, httpHeaders, null, (Class<T>) Object.class); | ||
} | ||
public ResponseEntity<T> get(String url, HttpHeaders httpHeaders, Class<T> clazz) { | ||
return callApiEndpoint(url, HttpMethod.GET, httpHeaders, null, clazz); | ||
} | ||
public ResponseEntity<T> post(String url, HttpHeaders httpHeaders, Object body) { | ||
return callApiEndpoint(url, HttpMethod.POST, httpHeaders, body, (Class<T>) Object.class); | ||
} | ||
*/ | ||
|
||
public ResponseEntity<T> post(String url, HttpHeaders httpHeaders, Object body, Class<T> clazz) { | ||
return callApiEndpoint(url, HttpMethod.POST, httpHeaders, body, clazz); | ||
} | ||
|
||
private ResponseEntity<T> callApiEndpoint(String url, HttpMethod httpMethod, HttpHeaders httpHeaders, Object body, Class<T> clazz) { | ||
return restTemplate.exchange(url, httpMethod, new HttpEntity<>(body, httpHeaders), clazz); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/what/the/springdatajpa/resttemplate/RestTemplateTestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package what.the.springdatajpa.resttemplate; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import what.the.springdatajpa.resttemplate.model.PersonResponse; | ||
|
||
@RestController | ||
@AllArgsConstructor | ||
@Slf4j | ||
public class RestTemplateTestController { | ||
|
||
private final RestTemplateTestService restTemplateTestService; | ||
|
||
@PostMapping | ||
public ResponseEntity<PersonResponse> restTemplateTest1() { | ||
return ResponseEntity.ok(restTemplateTestService.callPostExternalServer()); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/what/the/springdatajpa/resttemplate/RestTemplateTestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package what.the.springdatajpa.resttemplate; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.stereotype.Service; | ||
import what.the.springdatajpa.resttemplate.model.Person; | ||
import what.the.springdatajpa.resttemplate.model.PersonResponse; | ||
|
||
@Service | ||
@AllArgsConstructor | ||
@Slf4j | ||
public class RestTemplateTestService { | ||
|
||
private final ApiService<PersonResponse> apiService; | ||
|
||
public PersonResponse callPostExternalServer() { | ||
Person person = Person.builder() | ||
.age(22) | ||
.name("John") | ||
.build(); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
PersonResponse response = apiService | ||
.post("mockserver_url/testapi/first", headers, person, PersonResponse.class) | ||
.getBody(); | ||
System.out.println(">>>> response = " + response); | ||
return response; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/what/the/springdatajpa/resttemplate/model/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package what.the.springdatajpa.resttemplate.model; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class Person { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column | ||
private Long id; | ||
|
||
private String name; | ||
|
||
private int age; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/what/the/springdatajpa/resttemplate/model/PersonResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package what.the.springdatajpa.resttemplate.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class PersonResponse { | ||
private String result; | ||
private String username; | ||
} |
27 changes: 27 additions & 0 deletions
27
src/test/java/what/the/springdatajpa/resttemplate/RestTemplateTestServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package what.the.springdatajpa.resttemplate; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import what.the.springdatajpa.resttemplate.model.PersonResponse; | ||
|
||
@SpringBootTest | ||
@Transactional | ||
class RestTemplateTestServiceTest { | ||
|
||
@Autowired | ||
RestTemplateTestController controller; | ||
|
||
@Test | ||
public void restTemplateTest() { | ||
ResponseEntity<PersonResponse> entity = controller.restTemplateTest1(); | ||
System.out.println(">>> entity = " + entity.getStatusCode()); | ||
System.out.println(">>> entity = " + entity.getBody().getResult()); | ||
System.out.println(">>> entity = " + entity.getBody().getUsername()); | ||
Assertions.assertEquals(entity.getStatusCodeValue(), 200); | ||
Assertions.assertEquals(entity.getBody().getUsername(), "John"); | ||
} | ||
} |