Created
May 9, 2018 22:37
-
-
Save jreijn/7dabed8076a6cab39efa05a51bee826d to your computer and use it in GitHub Desktop.
IntelliJ Live template for Spring REST TDD demo
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
package ${PACKAGE_NAME}; | |
import org.springframework.hateoas.Link; | |
import org.springframework.hateoas.Resource; | |
public class ${NAME} extends Resource<${ENTITY}> { | |
public ${NAME}(${ENTITY} content, Link... links) { | |
super(content, links); | |
} | |
public ${NAME}(${ENTITY} content, Iterable<Link> links) { | |
super(content, links); | |
} | |
} |
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
package ${PACKAGE_NAME}; | |
import org.springframework.hateoas.Link; | |
import org.springframework.hateoas.Resources; | |
import org.springframework.hateoas.mvc.ResourceAssemblerSupport; | |
import java.util.List; | |
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; | |
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; | |
/** | |
* Assembler for creating resources from entities | |
*/ | |
class ${NAME} extends ResourceAssemblerSupport<${ENTITY}, ${ENTITY_RESOURCE}> { | |
public ${NAME}() { | |
super(${REST_CONTROLLER}.class, ${ENTITY_RESOURCE}.class); | |
} | |
@Override | |
public ${ENTITY_RESOURCE} toResource(${ENTITY} entity) { | |
Link link = linkTo(methodOn(${REST_CONTROLLER}.class, entity.getId()) | |
.get${ENTITY}ById(entity.getId())).withSelfRel(); | |
return new ${ENTITY_RESOURCE}(entity, link); | |
} | |
public Resources<${ENTITY_RESOURCE}> to${ENTITY}Resources(Iterable<? extends ${ENTITY}> entities) { | |
List<${ENTITY_RESOURCE}> entityResources = super.toResources(entities); | |
Link link = linkTo(methodOn(${REST_CONTROLLER}.class) | |
.getAll${ENTITY}s()).withSelfRel(); | |
return new Resources<>(entityResources, link); | |
} | |
} |
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
package ${PACKAGE_NAME}; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.hateoas.ExposesResourceFor; | |
import org.springframework.hateoas.Link; | |
import org.springframework.hateoas.MediaTypes; | |
import org.springframework.hateoas.Resources; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.MediaType; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.PatchMapping; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.PostMapping; | |
import org.springframework.web.bind.annotation.RequestBody; | |
import org.springframework.web.bind.annotation.ResponseStatus; | |
import org.springframework.web.bind.annotation.RestController; | |
import javax.validation.Valid; | |
import java.net.URI; | |
import java.util.List; | |
import java.util.Optional; | |
@ExposesResourceFor(${ENTITY}Resource.class) | |
@RestController | |
public class ${NAME} { | |
private ${ENTITY}Repository ${RESOURCE}Repository; | |
private ${ENTITY}ResourceAssembler ${RESOURCE}ResourceAssembler = new ${ENTITY}ResourceAssembler(); | |
@Autowired | |
public ${NAME}(${ENTITY}Repository ${RESOURCE}Repository) { | |
this.${RESOURCE}Repository = ${RESOURCE}Repository; | |
} | |
@GetMapping(value = "/${RESOURCE}s", produces = {MediaType.APPLICATION_JSON_VALUE, MediaTypes.HAL_JSON_VALUE}) | |
public ResponseEntity<Resources<${ENTITY}Resource>> getAll${ENTITY}s() { | |
List<${ENTITY}> all = ${RESOURCE}Repository.findAll(); | |
Resources<${ENTITY}Resource> ${RESOURCE}Resources = ${RESOURCE}ResourceAssembler.to${ENTITY}Resources(all); | |
return new ResponseEntity<>(${RESOURCE}Resources, HttpStatus.OK); | |
} | |
@PostMapping(value = "/${RESOURCE}s", produces = {MediaType.APPLICATION_JSON_VALUE, MediaTypes.HAL_JSON_VALUE}) | |
public ResponseEntity<${ENTITY}Resource> add${ENTITY}(@Valid @RequestBody ${ENTITY} ${RESOURCE}) { | |
${ENTITY} saved${ENTITY} = ${RESOURCE}Repository.save(${RESOURCE}); | |
${ENTITY}Resource ${RESOURCE}Resource = ${RESOURCE}ResourceAssembler.toResource(saved${ENTITY}); | |
return ResponseEntity.created(URI.create(${RESOURCE}Resource.getLink(Link.REL_SELF).getHref())).build(); | |
} | |
@GetMapping(value = "/${RESOURCE}s/{id}", produces = {MediaType.APPLICATION_JSON_VALUE, MediaTypes.HAL_JSON_VALUE}) | |
public ResponseEntity<${ENTITY}Resource> get${ENTITY}ById(@PathVariable Long id) { | |
Optional<${ENTITY}> ${RESOURCE} = ${RESOURCE}Repository.findById(id); | |
if (${RESOURCE}.isPresent()) { | |
${ENTITY}Resource ${RESOURCE}Resource = ${RESOURCE}ResourceAssembler.toResource(${RESOURCE}.get()); | |
return new ResponseEntity<>(${RESOURCE}Resource, HttpStatus.OK); | |
} else { | |
return new ResponseEntity<>(HttpStatus.NOT_FOUND); | |
} | |
} | |
} |
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
package ${PACKAGE_NAME}; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.Id; | |
@Entity | |
public class ${NAME} { | |
@Id | |
@GeneratedValue | |
private Long id; | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
} |
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
package ${PACKAGE_NAME}; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
import org.springframework.stereotype.Repository; | |
@Repository | |
public interface ${NAME} extends JpaRepository<${ENTITY},Long> { | |
} |
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
package ${PACKAGE_NAME}; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.test.context.SpringBootTest; | |
import org.springframework.http.MediaType; | |
import org.springframework.restdocs.JUnitRestDocumentation; | |
import org.springframework.test.context.junit4.SpringRunner; | |
import org.springframework.test.web.servlet.MockMvc; | |
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | |
import org.springframework.web.context.WebApplicationContext; | |
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; | |
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; | |
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; | |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | |
@RunWith(SpringRunner.class) | |
@SpringBootTest | |
public class ${NAME} { | |
@Rule | |
public final JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation(); | |
@Autowired | |
private WebApplicationContext context; | |
private MockMvc mockMvc; | |
@Before | |
public void setUp() { | |
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context) | |
.apply(documentationConfiguration(this.restDocumentation).operationPreprocessors() | |
.withResponseDefaults(prettyPrint())) | |
.build(); | |
} | |
@Test | |
public void testGetAll${ENTITY}s() throws Exception { | |
mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)) | |
.andExpect(status().isOk()); | |
} | |
} |
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
package ${PACKAGE_NAME}; | |
import org.springframework.hateoas.ResourceSupport; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; | |
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn; | |
@RestController | |
class ${NAME} { | |
@GetMapping("/") | |
public ResponseEntity<ResourceSupport> root() { | |
ResourceSupport resourceSupport = new ResourceSupport(); | |
resourceSupport.add(linkTo(methodOn(${NAME}.class).root()).withSelfRel()); | |
return ResponseEntity.ok(resourceSupport); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment