Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions src/main/java/net/anyjava/SpringDemoApplication.java
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}

}
30 changes: 28 additions & 2 deletions src/main/java/net/anyjava/demo/controller/CustomerController.java
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -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 + "'.");
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/net/anyjava/demo/service/CustomerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/test/java/net/anyjava/demo/WelcomeControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class WelcomeControllerTest {
public void setup() {
mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
// .apply(springSecurity())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -25,23 +31,49 @@ public class CustomerControllerTest {
@Autowired
private WebApplicationContext webApplicationContext;

@Autowired
private CustomerService customerService;


/**
* MockMvc
*/
@Before
public void setup() {


mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.build();
}

@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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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
Expand Down