Skip to content

Commit

Permalink
[#82411992] ID must be in the url and we only use PUTs for creation,
Browse files Browse the repository at this point in the history
like the service broker API. Can't write more tests until update and get
is implemented.
  • Loading branch information
Rob Szumlakowski authored and rdgallagher committed Jan 5, 2015
1 parent e06b5f4 commit a65b830
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 36 deletions.
Expand Up @@ -2,27 +2,28 @@


import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;


import javax.validation.Valid; import javax.validation.Valid;


import static org.springframework.http.HttpStatus.CONFLICT; import static org.springframework.http.HttpStatus.CONFLICT;
import static org.springframework.http.HttpStatus.CREATED; import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.web.bind.annotation.RequestMethod.POST; import static org.springframework.web.bind.annotation.RequestMethod.*;


@RestController @RestController
@RequestMapping("/identity-zones") @RequestMapping("/identity-zones")
public class IdentityZoneEndpoints { public class IdentityZoneEndpoints {


private IdentityZoneProvisioning dao; private IdentityZoneProvisioning dao;


@RequestMapping(method = POST) @RequestMapping(value="{id}", method = PUT)
@ResponseStatus(CREATED) public ResponseEntity<Void> createOrUpdateIdentityZone(@RequestBody @Valid IdentityZone zone, @PathVariable String id) {
public IdentityZone createIdentityZone(@RequestBody @Valid IdentityZone zone) { zone.setId(id);
return dao.create(zone); dao.create(zone);
return new ResponseEntity<Void>(CREATED);
} }


public void setIdentityZoneProvisioning(IdentityZoneProvisioning dao) { public void setIdentityZoneProvisioning(IdentityZoneProvisioning dao) {
Expand Down
@@ -1,6 +1,5 @@
package org.cloudfoundry.identity.uaa.zone; package org.cloudfoundry.identity.uaa.zone;


import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter; import org.springframework.jdbc.core.PreparedStatementSetter;
Expand Down Expand Up @@ -33,19 +32,18 @@ public JdbcIdentityZoneProvisioning(JdbcTemplate jdbcTemplate) {


@Override @Override
public IdentityZone retrieve(String id) { public IdentityZone retrieve(String id) {
IdentityZone identityZone = jdbcTemplate.queryForObject(IDENTITY_ZONE_BY_ID_QUERY, mapper, id); IdentityZone identityZone = jdbcTemplate.queryForObject(IDENTITY_ZONE_BY_ID_QUERY, mapper, id);
return identityZone; return identityZone;
} }


@Override @Override
public IdentityZone create(final IdentityZone identityZone) { public IdentityZone create(final IdentityZone identityZone) {
final String id = UUID.randomUUID().toString();


try { try {
jdbcTemplate.update(CREATE_IDENTITY_ZONE_SQL, new PreparedStatementSetter() { jdbcTemplate.update(CREATE_IDENTITY_ZONE_SQL, new PreparedStatementSetter() {
@Override @Override
public void setValues(PreparedStatement ps) throws SQLException { public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, id); ps.setString(1, identityZone.getId());
ps.setInt(2, identityZone.getVersion()); ps.setInt(2, identityZone.getVersion());
ps.setTimestamp(3, new Timestamp(new Date().getTime())); ps.setTimestamp(3, new Timestamp(new Date().getTime()));
ps.setTimestamp(4, new Timestamp(new Date().getTime())); ps.setTimestamp(4, new Timestamp(new Date().getTime()));
Expand All @@ -58,7 +56,7 @@ public void setValues(PreparedStatement ps) throws SQLException {
throw new ZoneAlreadyExistsException(e.getMostSpecificCause().getMessage()); throw new ZoneAlreadyExistsException(e.getMostSpecificCause().getMessage());
} }


return retrieve(id); return retrieve(identityZone.getId());
} }


private static final class IdentityZoneRowMapper implements RowMapper<IdentityZone> { private static final class IdentityZoneRowMapper implements RowMapper<IdentityZone> {
Expand Down
4 changes: 2 additions & 2 deletions uaa/src/main/webapp/WEB-INF/spring-servlet.xml
Expand Up @@ -133,10 +133,10 @@
<property name="identityZoneProvisioning" ref="identityZoneProvisioning" /> <property name="identityZoneProvisioning" ref="identityZoneProvisioning" />
</bean> </bean>


<http pattern="/identity-zones" create-session="stateless" authentication-manager-ref="emptyAuthenticationManager" <http pattern="/identity-zones/**" create-session="stateless" authentication-manager-ref="emptyAuthenticationManager"
entry-point-ref="oauthAuthenticationEntryPoint" access-decision-manager-ref="userAccessDecisionManager" entry-point-ref="oauthAuthenticationEntryPoint" access-decision-manager-ref="userAccessDecisionManager"
xmlns="http://www.springframework.org/schema/security"> xmlns="http://www.springframework.org/schema/security">
<intercept-url pattern="/identity-zones" method="POST" /> <intercept-url pattern="/identity-zones/**" method="PUT" />
<custom-filter ref="resourceAgnosticAuthenticationFilter" position="PRE_AUTH_FILTER" /> <custom-filter ref="resourceAgnosticAuthenticationFilter" position="PRE_AUTH_FILTER" />
<access-denied-handler ref="oauthAccessDeniedHandler" /> <access-denied-handler ref="oauthAccessDeniedHandler" />
</http> </http>
Expand Down
Expand Up @@ -18,7 +18,7 @@


import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.springframework.http.MediaType.APPLICATION_JSON; import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


Expand Down Expand Up @@ -46,26 +46,21 @@ public static void tearDown() throws Exception {


@Test @Test
public void testCreateZone() throws Exception { public void testCreateZone() throws Exception {
IdentityZone identityZone = getIdentityZone(UUID.randomUUID().toString()); IdentityZone identityZone = getIdentityZone("mysubdomain");
String id = UUID.randomUUID().toString();


MvcResult result = mockMvc.perform(post("/identity-zones") mockMvc.perform(put("/identity-zones/" + id)
.contentType(APPLICATION_JSON) .contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON) .content(new ObjectMapper().writeValueAsString(identityZone)))
.content(new ObjectMapper().writeValueAsString(identityZone)))
.andExpect(status().isCreated()) .andExpect(status().isCreated())
.andExpect(content().contentTypeCompatibleWith(APPLICATION_JSON)) .andExpect(content().string(""))
.andReturn(); .andReturn();


IdentityZone createdIdentityZone = new ObjectMapper().readValue(result.getResponse().getContentAsString(), IdentityZone.class);
assertEquals(identityZone.getSubDomain(), createdIdentityZone.getSubDomain());
assertEquals(identityZone.getName(), createdIdentityZone.getName());
assertEquals(identityZone.getDescription(), createdIdentityZone.getDescription());
UUID.fromString(createdIdentityZone.getId());
} }


private IdentityZone getIdentityZone(String salt) { private IdentityZone getIdentityZone(String subdomain) {
IdentityZone identityZone = new IdentityZone(); IdentityZone identityZone = new IdentityZone();
identityZone.setSubDomain("subdomain-" + salt); identityZone.setSubDomain(subdomain);
identityZone.setName("The Twiglet Zone"); identityZone.setName("The Twiglet Zone");
identityZone.setDescription("Like the Twilight Zone but tastier."); identityZone.setDescription("Like the Twilight Zone but tastier.");
return identityZone; return identityZone;
Expand All @@ -74,30 +69,35 @@ private IdentityZone getIdentityZone(String salt) {
@Test @Test
public void testCreateInvalidZone() throws Exception { public void testCreateInvalidZone() throws Exception {
IdentityZone identityZone = new IdentityZone(); IdentityZone identityZone = new IdentityZone();
String id = UUID.randomUUID().toString();


mockMvc.perform(post("/identity-zones") mockMvc.perform(put("/identity-zones/" + id)
.contentType(APPLICATION_JSON) .contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsString(identityZone))) .content(new ObjectMapper().writeValueAsString(identityZone)))
.andExpect(status().isBadRequest()); .andExpect(status().isBadRequest());
} }

// update a zone with a subdomain that already exists
// update a zone in place with different data (happy)
// update a zone with exactly the same data (happy)


@Test @Test
public void testCreateDuplicateZone() throws Exception { public void testCreatesZonesWithDuplicateSubdomains() throws Exception {
IdentityZone identityZone = new IdentityZone(); IdentityZone identityZone = new IdentityZone();
identityZone.setSubDomain("other-subdomain"); identityZone.setSubDomain("other-subdomain");
identityZone.setName("The Twiglet Zone 2"); identityZone.setName("The Twiglet Zone 2");


mockMvc.perform(post("/identity-zones") mockMvc.perform(put("/identity-zones/" + UUID.randomUUID().toString())
.contentType(APPLICATION_JSON) .contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON) .accept(APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsString(identityZone))) .content(new ObjectMapper().writeValueAsString(identityZone)))
.andExpect(status().isCreated()); .andExpect(status().isCreated());


mockMvc.perform(post("/identity-zones") mockMvc.perform(put("/identity-zones/" + UUID.randomUUID().toString())
.contentType(APPLICATION_JSON) .contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON) .accept(APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsString(identityZone))) .content(new ObjectMapper().writeValueAsString(identityZone)))
.andExpect(status().isConflict()); .andExpect(status().isConflict());
} }

} }

0 comments on commit a65b830

Please sign in to comment.