본문 바로가기

- Spring

[Spring Hateoas 1.2] RepresentationModel 테스트

반응형

https://docs.spring.io/spring-hateoas/docs/1.2.0-SNAPSHOT/reference/html/

 

Spring HATEOAS - Reference Documentation

Example 47. Configuring WebTestClient when using Spring Boot @SpringBootTest @AutoConfigureWebTestClient (1) class WebClientBasedTests { @Test void exampleTest(@Autowired WebTestClient.Builder builder, @Autowired HypermediaWebTestClientConfigurer configure

docs.spring.io

 

스프링 래퍼런스에 따르면 헤이토스의 표현 모델은 총 4가지가 있습니다.

class RepresentationModel

class EntityModel

class CollectionModel

class PagedModel

 

그 중 Entity와 Collection은 Representation을 상속받고,

Paged는 Collection을 상속받기 때문에 결국 Representation모델을 상속받아 사용합니다.

 

EntityModel        -|> RepresentationModel

CollectionModel -|> RepresentationModel

PagedModel       -|> CollectionModel

 

표현 모델만 테스트할 컨트롤을 아래와 같이 만들었습니다.

 

RepresentationModel은 아래와 같이 상속받은 모델을 사용합니다.

1
2
3
4
5
6
7
 
@Getter
@Setter
public class PersonModel extends RepresentationModel<PersonModel>
{
  String firstName, lastName;
}

 

 

그 외 표현모델에서는 RepresentationModel을 상속받지않고 기본생성자만 선언하여 작성한 Person 객체를 사용했습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.jjangms.study.hateoas.controller;
 
import com.jjangms.study.hateoas.model.Person;
import com.jjangms.study.hateoas.model.PersonModel;
import java.util.Collection;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.PagedModel;
import org.springframework.hateoas.PagedModel.PageMetadata;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class RepresentationModelController {
  @GetMapping("/representation")
  public PersonModel representation()
  {
    PersonModel model = new PersonModel();
    model.setFirstName("half");
    model.setLastName("jang");
    model.add(Link.of("http://localhost:9613/entity"));
    model.add(Link.of("http://localhost:9613/collection"));
 
    return model;
  }
 
  @GetMapping("/entity")
  public EntityModel<Person> entity()
  {
    Person person = new Person("half""jang");
    EntityModel<Person> model = EntityModel.of(person);
    model.add(Link.of("http://localhost:9613/entity"));
    Link link = Link.of("http://localhost:9613/representation", IanaLinkRelations.NEXT);
    model.add(link.withRel("next"));
 
    return model;
  }
 
  @GetMapping("/collection")
  public CollectionModel<Person> collection()
  {
    Collection<Person> people = new java.util.ArrayList<>();
    people.add(new Person("half""jang"));
    people.add(new Person("ms""jjang"));
    CollectionModel<Person> model = CollectionModel.of(people);
 
    Link oriLink = Link.of("http://localhost:9613/collection");
    Link firstLink = Link.of("http://localhost:9613/representation""representation");
    Link secondLink = Link.of("http://localhost:9613/paged""paged");
    model.add(oriLink.withSelfRel());
    model.add(firstLink.withRel("representation"));
    model.add(secondLink.withRel("paged"));
 
    return model;
  }
 
  @GetMapping("/paged")
  public PagedModel<Person> paged()
  {
    Collection<Person> people = new java.util.ArrayList<>();
    people.add(new Person("half""jang"));
    people.add(new Person("ms""jjang"));
    PageMetadata metadata = new PageMetadata(1L, 1L, 1L, 1L);
    PagedModel<Person> model = PagedModel.of(people, metadata);
 
    Link link = Link.of("http://localhost:9613/paged");
    model.add(link.withSelfRel());
    link = Link.of("http://localhost:9613/representation""representation");
    model.add(link.withRel("representation"));
    link = Link.of("http://localhost:9613/entity""entity");
    model.add(link.withRel("entity"));
    link = Link.of("http://localhost:9613/collection""collection");
    model.add(link.withRel("collection"));
 
    return model;
  }
}
 

 

 

아래는 mockMvc를 통해 위 컨트롤러를 테스트한 코드입니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.jjangms.study.hateoas.controller;
 
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
 
@ExtendWith(SpringExtension.class)
@WebMvcTest(RepresentationModelController.class)
@Slf4j
class RepresentationModelControllerTest {
  @Autowired
  MockMvc mockMvc;
 
  @Test
  public void representation() throws Exception
  {
    mockMvc.perform(get("/representation"))
        .andDo(print())
        .andExpect(status().isOk())
        .andExpect(jsonPath("$._links.self").exists());
  }
 
  @Test
  public void entity() throws Exception
  {
    mockMvc.perform(get("/entity"))
        .andDo(print())
        .andExpect(status().isOk())
        .andExpect(jsonPath("$._links.self").exists());
  }
 
  @Test
  public void collection() throws Exception
  {
    mockMvc.perform(get("/collection"))
        .andDo(print())
        .andExpect(status().isOk())
        .andExpect(jsonPath("$._links.self").exists());
  }
 
  @Test
  public void paged() throws Exception
  {
    mockMvc.perform(get("/paged"))
        .andDo(print())
        .andExpect(status().isOk())
        .andExpect(jsonPath("$._links.self").exists());
  }
}
 

 

테스트 결과입니다.

Body에 이동할 링크가 "_links"로 선언된 것을 확인할 수 있습니다.

반응형