diff --git a/build.gradle b/build.gradle index c967842..3a2bfa3 100644 --- a/build.gradle +++ b/build.gradle @@ -34,11 +34,13 @@ dependencies { compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-freemarker') compile('org.projectlombok:lombok:1.16.6') + compile('com.google.code.gson:gson:2.5') //compile('org.springframework.boot:spring-boot-starter-security') //compile('org.bgee.log4jdbc-log4j2:log4jdbc-log4j2:1.11') runtime('com.h2database:h2') //runtime('mysql:mysql-connector-java') testCompile('org.springframework.boot:spring-boot-starter-test') + testCompile('com.jayway.jsonpath:json-path:2.1.0') //testCompile('org.springframework.security:spring-security-test:4.0.3.RELEASE') } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a32a80a..0043b77 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Tue Jan 12 17:13:48 KST 2016 +#Wed Feb 17 17:48:19 KST 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip diff --git a/src/main/java/net/anyjava/SpringDemoApplication.java b/src/main/java/net/anyjava/SpringDemoApplication.java index 799d571..60c43f0 100644 --- a/src/main/java/net/anyjava/SpringDemoApplication.java +++ b/src/main/java/net/anyjava/SpringDemoApplication.java @@ -1,14 +1,49 @@ package net.anyjava; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.web.HttpEncodingProperties; +import org.springframework.boot.context.web.OrderedCharacterEncodingFilter; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; +import org.springframework.core.Ordered; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; + +import java.nio.charset.Charset; @SpringBootApplication @ComponentScan public class SpringDemoApplication { + @Autowired + private HttpEncodingProperties httpEncodingProperties; + public static void main(String[] args) { SpringApplication.run(SpringDemoApplication.class, args); } + + + /** + * UTF-8 설정 + */ + @Bean + public HttpMessageConverter responseBodyConverter() { + return new StringHttpMessageConverter(Charset.forName("UTF-8")); + } + + /** + * UTF-8 설정 + */ + @Bean + public OrderedCharacterEncodingFilter characterEncodingFilter() { + OrderedCharacterEncodingFilter filter = + new OrderedCharacterEncodingFilter(); + filter.setEncoding(this.httpEncodingProperties.getCharset().name()); + filter.setForceEncoding(this.httpEncodingProperties.isForce()); + filter.setOrder(Ordered.HIGHEST_PRECEDENCE); + return filter; + } + } diff --git a/src/main/java/net/anyjava/demo/controller/CustomerController.java b/src/main/java/net/anyjava/demo/controller/CustomerController.java index fee6153..a9e52b7 100644 --- a/src/main/java/net/anyjava/demo/controller/CustomerController.java +++ b/src/main/java/net/anyjava/demo/controller/CustomerController.java @@ -1,9 +1,13 @@ package net.anyjava.demo.controller; import net.anyjava.demo.domain.Customer; +import net.anyjava.demo.service.CustomerService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; /** @@ -13,8 +17,30 @@ @RequestMapping("/api/Customers") public class CustomerController { - @RequestMapping(value = "{id}", method = RequestMethod.GET) + @Autowired + private CustomerService customerService; + + /** + * 고객조회 API + */ + @RequestMapping(value = "/{id}", method = RequestMethod.GET) public Customer getCustomer(@PathVariable Long id) { - return null; + + Customer customer = customerService.select(id); + + // 조회결과가 없을경우 404 리턴 + if ( customer == null ) { + throw new UserNotFoundException(id); + } + + return customer; + } + + @ResponseStatus(HttpStatus.NOT_FOUND) + class UserNotFoundException extends RuntimeException { + + public UserNotFoundException(Long userId) { + super("could not find user '" + userId + "'."); + } } } diff --git a/src/main/java/net/anyjava/demo/service/CustomerService.java b/src/main/java/net/anyjava/demo/service/CustomerService.java index 3b01fe4..bc996da 100644 --- a/src/main/java/net/anyjava/demo/service/CustomerService.java +++ b/src/main/java/net/anyjava/demo/service/CustomerService.java @@ -26,10 +26,17 @@ public Customer create(Customer customer) { } + /** + * 조회 + */ public Customer select(Long id) { return customerRepository.findOne(id); } + /** + * 삭제 + */ + @Transactional public boolean delete(Long id) { Customer customer = customerRepository.findOne(id); boolean returnV = false; diff --git a/src/test/java/net/anyjava/demo/WelcomeControllerTest.java b/src/test/java/net/anyjava/demo/WelcomeControllerTest.java index 54b0135..1a72d5c 100644 --- a/src/test/java/net/anyjava/demo/WelcomeControllerTest.java +++ b/src/test/java/net/anyjava/demo/WelcomeControllerTest.java @@ -35,7 +35,6 @@ public class WelcomeControllerTest { public void setup() { mockMvc = MockMvcBuilders .webAppContextSetup(webApplicationContext) -// .apply(springSecurity()) .build(); } diff --git a/src/test/java/net/anyjava/demo/controller/CustomerControllerTest.java b/src/test/java/net/anyjava/demo/controller/CustomerControllerTest.java index a265655..75c62bf 100644 --- a/src/test/java/net/anyjava/demo/controller/CustomerControllerTest.java +++ b/src/test/java/net/anyjava/demo/controller/CustomerControllerTest.java @@ -1,19 +1,25 @@ package net.anyjava.demo.controller; +import com.google.gson.Gson; import net.anyjava.SpringDemoApplication; +import net.anyjava.demo.domain.Customer; +import net.anyjava.demo.service.CustomerService; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertTrue; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SpringDemoApplication.class) @@ -25,11 +31,17 @@ public class CustomerControllerTest { @Autowired private WebApplicationContext webApplicationContext; + @Autowired + private CustomerService customerService; + + /** * MockMvc */ @Before public void setup() { + + mockMvc = MockMvcBuilders .webAppContextSetup(webApplicationContext) .build(); @@ -37,11 +49,31 @@ public void setup() { @Test public void testGetCustomer() throws Exception { - mockMvc.perform(get("/api/Customers/1")) - .andExpect(status().isOk()); -// .andExpect(content().string(containsString("Hello"))); + Customer customer + = this.customerService.create(new Customer(null, "아", "이유")); + Gson gson = new Gson(); + String json = gson.toJson(customer); + + mockMvc.perform( + get("/api/Customers/1").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect( + content().contentType( + MediaType.APPLICATION_JSON_UTF8_VALUE)) + .andExpect(content().string(json)) + .andExpect(jsonPath("$.id", is(customer.getLastName()))) + .andExpect(jsonPath("$.firstName", is(customer.getFirstName()))) + .andExpect(jsonPath("$.lastName", is(customer.getLastName()))); + + mockMvc.perform( + get("/api/Customers/0").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound()); } + @Test + public void 한글테스트() { + assertTrue(true); + } } \ No newline at end of file diff --git a/src/test/java/net/anyjava/demo/service/CustomerServiceTest.java b/src/test/java/net/anyjava/demo/service/CustomerServiceTest.java index 545c3a9..8d89ac7 100644 --- a/src/test/java/net/anyjava/demo/service/CustomerServiceTest.java +++ b/src/test/java/net/anyjava/demo/service/CustomerServiceTest.java @@ -54,7 +54,7 @@ public void tearDown() throws Exception { public void testCreate() { Customer customer = getCustomer(); Customer target = getCustomer(); - target.setId(this.maxId+1L); + target.setId(this.maxId + 1L); assertEquals(target, customerService.create(customer)); } @@ -73,7 +73,8 @@ public void testUpdate() { customer.setLastName("이유"); assertEquals(new Customer(this.maxId, "아", "이유"), customer); -// assertEquals(new Customer(this.maxId, "아", "이유"), customerService.update(customer)); +// assertEquals(new Customer(this.maxId, "아", "이유") +// , customerService.update(customer)); } @Test