Skip to content

Commit 3dcd153

Browse files
authored
Merge pull request #142 from jeyrschabu/remove_hal_in_projects_api
Refactor Project;s API
2 parents 891beb5 + a61757a commit 3dcd153

File tree

2 files changed

+16
-35
lines changed

2 files changed

+16
-35
lines changed

front50-web/src/main/groovy/com/netflix/spinnaker/front50/controllers/v2/ProjectsController.groovy

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ import com.netflix.spectator.api.Registry
2121
import com.netflix.spinnaker.front50.exception.NotFoundException
2222
import com.netflix.spinnaker.front50.model.project.Project
2323
import com.netflix.spinnaker.front50.model.project.ProjectDAO
24-
import com.netflix.spinnaker.front50.resources.project.ProjectResource
25-
import com.netflix.spinnaker.front50.resources.project.ProjectResourceAssembler
2624
import groovy.transform.InheritConstructors
2725
import groovy.util.logging.Slf4j
2826
import io.swagger.annotations.Api
2927
import io.swagger.annotations.ApiOperation
3028
import org.springframework.beans.factory.annotation.Autowired
3129
import org.springframework.context.MessageSource
32-
import org.springframework.hateoas.Resources
3330
import org.springframework.http.HttpStatus
3431
import org.springframework.http.MediaType
3532
import org.springframework.web.bind.annotation.PathVariable
@@ -39,7 +36,6 @@ import org.springframework.web.bind.annotation.RequestMethod
3936
import org.springframework.web.bind.annotation.RequestParam
4037
import org.springframework.web.bind.annotation.ResponseStatus
4138
import org.springframework.web.bind.annotation.RestController
42-
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*
4339

4440
import javax.servlet.http.HttpServletResponse
4541

@@ -57,47 +53,41 @@ public class ProjectsController {
5753
@Autowired
5854
Registry registry
5955

60-
private final ProjectResourceAssembler assembler = new ProjectResourceAssembler()
61-
6256
@RequestMapping(value = "/search", method = RequestMethod.GET)
6357
@ApiOperation(value = "", notes = """Search for projects given one or more attributes.
6458
6559
- /search?q=ProjectName
6660
- /search?q=ApplicationName
6761
""")
68-
Resources<ProjectResource> search(@RequestParam("q") String query) {
62+
Set<Project> search(@RequestParam("q") String query) {
6963
def projects = projectDAO.all().findAll { Project project ->
7064
project.name.toLowerCase().contains(query.toLowerCase()) || project.config.applications.find {
7165
it.toLowerCase().contains(query.toLowerCase())
7266
}
7367
}
7468

75-
def resources = assembler.toResources(projects)
76-
def link = linkTo(ProjectsController).slash("/search?q=${query}").withSelfRel()
77-
return new Resources<ProjectResource>(resources, link)
69+
return projects
7870
}
7971

8072
@ApiOperation(value = "", notes = "Fetch all projects")
8173
@RequestMapping(method = RequestMethod.GET)
82-
Resources<ProjectResource> projects() {
83-
def resources = assembler.toResources(projectDAO.all())
84-
def link = linkTo(ProjectsController).slash('/').withSelfRel()
85-
return new Resources<ProjectResource>(resources, link)
74+
Set<Project> projects() {
75+
return projectDAO.all()
8676
}
8777

8878
@ApiOperation(value = "", notes = "Fetch a single project")
8979
@RequestMapping(method = RequestMethod.GET, value = "/{projectId}")
90-
ProjectResource project(@PathVariable String projectId) {
80+
Project project(@PathVariable String projectId) {
9181
try {
92-
return assembler.toResource(projectDAO.findByName(projectId))
82+
return projectDAO.findByName(projectId)
9383
} catch (NotFoundException e) {
94-
return assembler.toResource(projectDAO.findById(projectId))
84+
return projectDAO.findById(projectId)
9585
}
9686
}
9787

9888
@ApiOperation(value = "", notes = "Update an existing project")
9989
@RequestMapping(method = RequestMethod.PUT, value = "/{projectId}")
100-
ProjectResource put(@PathVariable final String projectId, @RequestBody final Project project) {
90+
Project put(@PathVariable final String projectId, @RequestBody final Project project) {
10191
def existingProject = projectDAO.findById(projectId)
10292

10393
project.id = existingProject.id
@@ -112,12 +102,12 @@ public class ProjectsController {
112102
} catch (NotFoundException ignored) {}
113103

114104
projectDAO.update(projectId, project)
115-
return assembler.toResource(project)
105+
return project
116106
}
117107

118108
@ApiOperation(value = "", notes = "Create a project")
119109
@RequestMapping(method = RequestMethod.POST)
120-
ProjectResource create(@RequestBody final Project project) {
110+
Project create(@RequestBody final Project project) {
121111
project.createTs = System.currentTimeMillis()
122112
project.updateTs = System.currentTimeMillis()
123113

@@ -126,7 +116,7 @@ public class ProjectsController {
126116
throw new ProjectAlreadyExistsException()
127117
} catch (NotFoundException ignored) {}
128118

129-
return assembler.toResource(projectDAO.create(project.id, project))
119+
return projectDAO.create(project.id, project)
130120
}
131121

132122
@ApiOperation(value = "", notes = "Delete a project")

front50-web/src/test/groovy/com/netflix/spinnaker/front50/controllers/v2/ProjectsControllerTck.groovy

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
4444
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
4545
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
4646
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put
47+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
4748
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
4849

4950
abstract class ProjectsControllerTck extends Specification {
@@ -79,19 +80,17 @@ abstract class ProjectsControllerTck extends Specification {
7980

8081
when:
8182
def response = mockMvc.perform(get("/v2/projects/search?q=" + criteria))
82-
def result = response.andReturn()
8383

8484
then:
8585
response.andExpect status().isOk()
86-
extractList(result.getResponse().getContentAsString()) == (project ? [toMap(dao.findByName(project.name))] : [])
86+
response.andExpect content().string(new ObjectMapper().writeValueAsString([dao.findByName(project.name)]))
8787

8888
where:
8989
criteria | project
9090
"Project1" | new Project(name: "Project1")
9191
"Application1" | new Project(name: "Project1", config: new Project.ProjectConfig(
9292
applications: ["Application1", "Application2"]
9393
))
94-
"n/a" | null
9594
}
9695

9796
void "should fetch all projects"() {
@@ -101,9 +100,9 @@ abstract class ProjectsControllerTck extends Specification {
101100
}
102101

103102
expect:
104-
extractList(mockMvc.perform(get("/v2/projects")).andReturn().response.contentAsString) == dao.all().collect {
105-
objectMapper.convertValue(it, Map)
106-
}
103+
mockMvc.perform(
104+
get("/v2/projects")
105+
).andExpect content().string(new ObjectMapper().writeValueAsString(dao.all()))
107106
}
108107

109108
@Unroll
@@ -188,16 +187,8 @@ abstract class ProjectsControllerTck extends Specification {
188187
thrown(NotFoundException)
189188
}
190189

191-
List<Map> extractList(String content) {
192-
(objectMapper.readValue(content, Map).content as List<Map>).collect {
193-
it.remove("links")
194-
it
195-
}
196-
}
197-
198190
Map extractMap(String content) {
199191
def result = objectMapper.readValue(content, Map) as Map
200-
result.remove("links")
201192
result
202193
}
203194

0 commit comments

Comments
 (0)