Skip to content

Commit

Permalink
Filter Plan objects provided in Request objects
Browse files Browse the repository at this point in the history
Resolves #141
  • Loading branch information
royclarkson committed Dec 19, 2018
1 parent 6b2ede4 commit e88d839
Show file tree
Hide file tree
Showing 17 changed files with 394 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,25 @@ public abstract class AbstractServiceInstanceControllerIntegrationTest extends C

protected String updateRequestBody;

protected String updateRequestBodyWithPlan;

@Before
public void setUpCommonFixtures() {
this.createRequestBody = JsonUtils.toJson(CreateServiceInstanceRequest.builder()
this.createRequestBody = JsonUtils.toJson(CreateServiceInstanceRequest
.builder()
.serviceDefinitionId(serviceDefinition.getId())
.planId("plan-one-id")
.build());

this.updateRequestBody = JsonUtils.toJson(UpdateServiceInstanceRequest
.builder()
.serviceDefinitionId(serviceDefinition.getId())
.planId("standard")
.build());

this.updateRequestBody = JsonUtils.toJson(UpdateServiceInstanceRequest.builder()
this.updateRequestBodyWithPlan = JsonUtils.toJson(UpdateServiceInstanceRequest
.builder()
.serviceDefinitionId(serviceDefinition.getId())
.planId("plan-three-id")
.build());
}

Expand Down Expand Up @@ -132,7 +142,7 @@ protected String buildDeleteUrl(String platformInstanceId, boolean asyncAccepted
return buildBaseUrl(platformInstanceId)
.path(SERVICE_INSTANCE_ID)
.queryParam("service_id", serviceDefinition.getId())
.queryParam("plan_id", "standard")
.queryParam("plan_id", "plan-three-id")
.queryParam("accepts_incomplete", asyncAccepted)
.toUriString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ private void assertResult(ResultActions result) throws Exception {
SERVICE_REQUIRES_SYSLOG_DRAIN.toString(),
SERVICE_REQUIRES_ROUTE_FORWARDING.toString())
))
.andExpect(jsonPath("$.services[*].plans[*]", hasSize(2)))
.andExpect(jsonPath("$.services[*].plans[*].id", containsInAnyOrder(plans.get(0).getId(), plans.get(1).getId())))
.andExpect(jsonPath("$.services[*].plans[*].name", containsInAnyOrder(plans.get(0).getName(), plans.get(1).getName())))
.andExpect(jsonPath("$.services[*].plans[*].description", containsInAnyOrder(plans.get(0).getDescription(), plans.get(1).getDescription())))
.andExpect(jsonPath("$.services[*].plans[*]", hasSize(3)))
.andExpect(jsonPath("$.services[*].plans[*].id", containsInAnyOrder(plans.get(0).getId(), plans.get(1).getId(), plans.get(2).getId())))
.andExpect(jsonPath("$.services[*].plans[*].name", containsInAnyOrder(plans.get(0).getName(), plans.get(1).getName(), plans.get(2).getName())))
.andExpect(jsonPath("$.services[*].plans[*].description", containsInAnyOrder(plans.get(0).getDescription(), plans.get(1).getDescription(), plans.get(2).getDescription())))
.andExpect(jsonPath("$.services[*].plans[*].metadata", contains(plans.get(1).getMetadata())))
.andExpect(jsonPath("$.services[*].plans[*].bindable", hasSize(1)))
.andExpect(jsonPath("$.services[*].plans[*].bindable", contains(plans.get(1).isBindable())))
.andExpect(jsonPath("$.services[*].plans[*].free", containsInAnyOrder(plans.get(0).isFree(), plans.get(1).isFree())))
.andExpect(jsonPath("$.services[*].plans[*].free", containsInAnyOrder(plans.get(0).isFree(), plans.get(1).isFree(), plans.get(2).isFree())))
.andExpect(jsonPath("$.services[*].plans[*].schemas.service_instance.create.parameters", contains(createServiceInstanceSchema)))
.andExpect(jsonPath("$.services[*].plans[*].schemas.service_instance.update.parameters", contains(updateServiceInstanceSchema)))
.andExpect(jsonPath("$.services[*].plans[*].schemas.service_binding.create.parameters", contains(createServiceBindingSchema)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ public void createBindingToAppWithoutAsyncAndHeadersSucceeds() throws Exception
assertHeaderValuesSet(actualRequest);
}

@Test
public void createBindingToAppFiltersPlansSucceeds() throws Exception {
setupCatalogService();

setupServiceInstanceBindingService(CreateServiceInstanceAppBindingResponse
.builder()
.bindingExisted(false)
.build());

mockMvc.perform(put(buildCreateUrl(PLATFORM_INSTANCE_ID, false))
.content(createRequestBody)
.header(API_INFO_LOCATION_HEADER, API_INFO_LOCATION)
.header(ORIGINATING_IDENTITY_HEADER, buildOriginatingIdentityHeader())
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated());

CreateServiceInstanceBindingRequest actualRequest = verifyCreateBinding();
assertThat(actualRequest.isAsyncAccepted()).isEqualTo(false);
assertThat(actualRequest.getPlan().getId()).isEqualTo(actualRequest.getPlanId());
assertHeaderValuesSet(actualRequest);
}

@Test
public void createBindingToAppWithAsyncAndHeadersSucceeds() throws Exception {
setupCatalogService();
Expand Down Expand Up @@ -344,6 +367,29 @@ public void deleteBindingWithoutAsyncAndHeadersSucceeds() throws Exception {
assertHeaderValuesSet(actualRequest);
}

@Test
public void deleteBindingFiltersPlansSucceeds() throws Exception {
setupCatalogService();

setupServiceInstanceBindingService(DeleteServiceInstanceBindingResponse
.builder()
.build());

mockMvc.perform(delete(buildDeleteUrl(PLATFORM_INSTANCE_ID, false))
.header(API_INFO_LOCATION_HEADER, API_INFO_LOCATION)
.header(ORIGINATING_IDENTITY_HEADER, buildOriginatingIdentityHeader())
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string("{}"));

verify(serviceInstanceBindingService).deleteServiceInstanceBinding(any(DeleteServiceInstanceBindingRequest.class));

DeleteServiceInstanceBindingRequest actualRequest = verifyDeleteBinding();
assertThat(actualRequest.isAsyncAccepted()).isEqualTo(false);
assertThat(actualRequest.getPlan().getId()).isEqualTo(actualRequest.getPlanId());
assertHeaderValuesSet(actualRequest);
}

@Test
public void deleteBindingWithUnknownInstanceIdFails() throws Exception {
setupCatalogService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public abstract class ServiceInstanceBindingIntegrationTest extends ControllerIn
public void setupBase() {
CreateServiceInstanceBindingRequest request = CreateServiceInstanceBindingRequest.builder()
.serviceDefinitionId(serviceDefinition.getId())
.planId("standard")
.planId("plan-one-id")
.build();

createRequestBody = JsonUtils.toJson(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,29 @@ public void createServiceInstanceWithExistingInstanceSucceeds() throws Exception
.andExpect(status().isOk());
}

@Test
public void createServiceInstanceFiltersPlansSucceeds() throws Exception {
setupCatalogService();

setupServiceInstanceService(CreateServiceInstanceResponse
.builder()
.async(true)
.build());

mockMvc.perform(put(buildCreateUpdateUrl(PLATFORM_INSTANCE_ID, true))
.content(createRequestBody)
.header(API_INFO_LOCATION_HEADER, API_INFO_LOCATION)
.header(ORIGINATING_IDENTITY_HEADER, buildOriginatingIdentityHeader())
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isAccepted());

CreateServiceInstanceRequest actualRequest = verifyCreateServiceInstance();
assertThat(actualRequest.isAsyncAccepted()).isEqualTo(true);
assertThat(actualRequest.getPlan().getId()).isEqualTo(actualRequest.getPlanId());
assertHeaderValuesSet(actualRequest);
}

@Test
public void createServiceInstanceWithUnknownServiceDefinitionIdFails() throws Exception {
setupCatalogService(null);
Expand Down Expand Up @@ -350,6 +373,25 @@ public void deleteServiceInstanceWithoutAsyncAndHeadersSucceeds() throws Excepti
assertHeaderValuesNotSet(actualRequest);
}

@Test
public void deleteServiceInstanceFiltersPlansSucceeds() throws Exception {
setupCatalogService();

setupServiceInstanceService(DeleteServiceInstanceResponse
.builder()
.build());

mockMvc.perform(delete(buildDeleteUrl())
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string("{}"));

DeleteServiceInstanceRequest actualRequest = verifyDeleteServiceInstance();
assertThat(actualRequest.isAsyncAccepted()).isEqualTo(false);
assertThat(actualRequest.getPlan().getId()).isEqualTo(actualRequest.getPlanId());
assertHeaderValuesNotSet(actualRequest);
}

@Test
public void deleteServiceInstanceWithUnknownIdFails() throws Exception {
setupCatalogService();
Expand Down Expand Up @@ -413,6 +455,28 @@ public void updateServiceInstanceWithoutSyncAndHeadersSucceeds() throws Exceptio

UpdateServiceInstanceRequest actualRequest = verifyUpdateServiceInstance();
assertThat(actualRequest.isAsyncAccepted()).isEqualTo(false);
assertThat(actualRequest.getServiceDefinition().getPlans().size()).isEqualTo(3);
assertHeaderValuesNotSet(actualRequest);
}

@Test
public void updateServiceInstanceFiltersPlansSucceeds() throws Exception {
setupCatalogService();

setupServiceInstanceService(UpdateServiceInstanceResponse
.builder()
.build());

mockMvc.perform(patch(buildCreateUpdateUrl())
.content(updateRequestBodyWithPlan)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string("{}"));

UpdateServiceInstanceRequest actualRequest = verifyUpdateServiceInstance();
assertThat(actualRequest.isAsyncAccepted()).isEqualTo(false);
assertThat(actualRequest.getPlan().getId()).isEqualTo(actualRequest.getPlanId());
assertHeaderValuesNotSet(actualRequest);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static ServiceDefinition getSimpleService() {
.name("Service One")
.description("Description for Service One")
.bindable(true)
.plans(getPlanOne(), getPlanTwo())
.plans(getPlanOne(), getPlanTwo(), getPlanThree())
.requires(ServiceDefinitionRequires.SERVICE_REQUIRES_SYSLOG_DRAIN.toString(),
ServiceDefinitionRequires.SERVICE_REQUIRES_ROUTE_FORWARDING.toString())
.build();
Expand Down Expand Up @@ -80,4 +80,12 @@ private static Plan getPlanTwo() {
.schemas(schemas)
.build();
}

private static Plan getPlanThree() {
return Plan.builder()
.id("plan-three-id")
.name("Plan Three")
.description("Description for Plan Three")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.util.Map;
import java.util.Optional;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -31,11 +32,13 @@
import org.springframework.cloud.servicebroker.model.KubernetesContext;
import org.springframework.cloud.servicebroker.model.PlatformContext;
import org.springframework.cloud.servicebroker.model.ServiceBrokerRequest;
import org.springframework.cloud.servicebroker.model.catalog.Plan;
import org.springframework.cloud.servicebroker.model.catalog.ServiceDefinition;
import org.springframework.cloud.servicebroker.service.CatalogService;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;

/**
* Base functionality shared by controllers.
Expand Down Expand Up @@ -77,6 +80,15 @@ protected ServiceDefinition getServiceDefinition(String serviceDefinitionId) {
return catalogService.getServiceDefinition(serviceDefinitionId);
}

protected Plan getServiceDefinitionPlan(ServiceDefinition serviceDefinition, String planId) {
Optional<Plan> optionalPlan = Optional.empty();
if (serviceDefinition != null) {
optionalPlan = serviceDefinition.getPlans().stream().filter(plan ->
plan.getId().equals(planId)).findFirst();
}
return optionalPlan.orElse(null);
}

protected Context parseOriginatingIdentity(String originatingIdentityString) {
if (originatingIdentityString == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.springframework.cloud.servicebroker.model.binding.GetLastServiceBindingOperationResponse;
import org.springframework.cloud.servicebroker.model.binding.GetServiceInstanceBindingRequest;
import org.springframework.cloud.servicebroker.model.binding.GetServiceInstanceBindingResponse;
import org.springframework.cloud.servicebroker.model.catalog.Plan;
import org.springframework.cloud.servicebroker.model.catalog.ServiceDefinition;
import org.springframework.cloud.servicebroker.model.instance.OperationState;
import org.springframework.cloud.servicebroker.service.CatalogService;
import org.springframework.cloud.servicebroker.service.ServiceInstanceBindingService;
Expand All @@ -55,6 +57,7 @@
*
* @author sgreenberg@pivotal.io
* @author Scott Frederick
* @author Roy Clarkson
*/
@ServiceBrokerRestController
public class ServiceInstanceBindingController extends BaseController {
Expand Down Expand Up @@ -82,9 +85,12 @@ public ResponseEntity<CreateServiceInstanceBindingResponse> createServiceInstanc
@RequestHeader(value = ServiceBrokerRequest.API_INFO_LOCATION_HEADER, required = false) String apiInfoLocation,
@RequestHeader(value = ServiceBrokerRequest.ORIGINATING_IDENTITY_HEADER, required = false) String originatingIdentityString,
@Valid @RequestBody CreateServiceInstanceBindingRequest request) {
ServiceDefinition serviceDefinition = getRequiredServiceDefinition(request.getServiceDefinitionId());
Plan plan = getServiceDefinitionPlan(serviceDefinition, request.getPlanId());
request.setServiceInstanceId(serviceInstanceId);
request.setBindingId(bindingId);
request.setServiceDefinition(getRequiredServiceDefinition(request.getServiceDefinitionId()));
request.setServiceDefinition(serviceDefinition);
request.setPlan(plan);
setCommonRequestFields(request, pathVariables.get(ServiceBrokerRequest.PLATFORM_INSTANCE_ID_VARIABLE), apiInfoLocation,
originatingIdentityString, acceptsIncomplete);

Expand Down Expand Up @@ -185,12 +191,16 @@ public ResponseEntity<DeleteServiceInstanceBindingResponse> deleteServiceInstanc
@RequestParam(value = AsyncServiceBrokerRequest.ASYNC_REQUEST_PARAMETER, required = false) boolean acceptsIncomplete,
@RequestHeader(value = ServiceBrokerRequest.API_INFO_LOCATION_HEADER, required = false) String apiInfoLocation,
@RequestHeader(value = ServiceBrokerRequest.ORIGINATING_IDENTITY_HEADER, required = false) String originatingIdentityString) {
DeleteServiceInstanceBindingRequest request = DeleteServiceInstanceBindingRequest.builder()
ServiceDefinition serviceDefinition = getServiceDefinition(serviceDefinitionId);
Plan plan = getServiceDefinitionPlan(serviceDefinition, planId);
DeleteServiceInstanceBindingRequest request = DeleteServiceInstanceBindingRequest
.builder()
.serviceInstanceId(serviceInstanceId)
.bindingId(bindingId)
.serviceDefinitionId(serviceDefinitionId)
.planId(planId)
.serviceDefinition(getServiceDefinition(serviceDefinitionId))
.serviceDefinition(serviceDefinition)
.plan(plan)
.asyncAccepted(acceptsIncomplete)
.platformInstanceId(pathVariables.get(ServiceBrokerRequest.PLATFORM_INSTANCE_ID_VARIABLE))
.apiInfoLocation(apiInfoLocation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.cloud.servicebroker.exception.ServiceInstanceDoesNotExistException;
import org.springframework.cloud.servicebroker.model.AsyncServiceBrokerRequest;
import org.springframework.cloud.servicebroker.model.ServiceBrokerRequest;
import org.springframework.cloud.servicebroker.model.catalog.Plan;
import org.springframework.cloud.servicebroker.model.catalog.ServiceDefinition;
import org.springframework.cloud.servicebroker.model.instance.CreateServiceInstanceRequest;
import org.springframework.cloud.servicebroker.model.instance.CreateServiceInstanceResponse;
Expand Down Expand Up @@ -86,9 +87,11 @@ public ResponseEntity<CreateServiceInstanceResponse> createServiceInstance(
@RequestHeader(value = ServiceBrokerRequest.ORIGINATING_IDENTITY_HEADER, required = false) String originatingIdentityString,
@Valid @RequestBody CreateServiceInstanceRequest request) {
ServiceDefinition serviceDefinition = getRequiredServiceDefinition(request.getServiceDefinitionId());
Plan plan = getServiceDefinitionPlan(serviceDefinition, request.getPlanId());

request.setServiceInstanceId(serviceInstanceId);
request.setServiceDefinition(serviceDefinition);
request.setPlan(plan);
setCommonRequestFields(request, pathVariables.get(ServiceBrokerRequest.PLATFORM_INSTANCE_ID_VARIABLE), apiInfoLocation,
originatingIdentityString, acceptsIncomplete);

Expand Down Expand Up @@ -186,12 +189,15 @@ public ResponseEntity<DeleteServiceInstanceResponse> deleteServiceInstance(
@RequestHeader(value = ServiceBrokerRequest.API_INFO_LOCATION_HEADER, required = false) String apiInfoLocation,
@RequestHeader(value = ServiceBrokerRequest.ORIGINATING_IDENTITY_HEADER, required = false) String originatingIdentityString) {
ServiceDefinition serviceDefinition = getRequiredServiceDefinition(serviceDefinitionId);
Plan plan = getServiceDefinitionPlan(serviceDefinition, planId);

DeleteServiceInstanceRequest request = DeleteServiceInstanceRequest.builder()
DeleteServiceInstanceRequest request = DeleteServiceInstanceRequest
.builder()
.serviceInstanceId(serviceInstanceId)
.serviceDefinitionId(serviceDefinitionId)
.planId(planId)
.serviceDefinition(serviceDefinition)
.plan(plan)
.asyncAccepted(acceptsIncomplete)
.platformInstanceId(pathVariables.get(ServiceBrokerRequest.PLATFORM_INSTANCE_ID_VARIABLE))
.apiInfoLocation(apiInfoLocation)
Expand Down Expand Up @@ -225,9 +231,11 @@ public ResponseEntity<UpdateServiceInstanceResponse> updateServiceInstance(
@RequestHeader(value = ServiceBrokerRequest.ORIGINATING_IDENTITY_HEADER, required = false) String originatingIdentityString,
@Valid @RequestBody UpdateServiceInstanceRequest request) {
ServiceDefinition serviceDefinition = getRequiredServiceDefinition(request.getServiceDefinitionId());
Plan plan = getServiceDefinitionPlan(serviceDefinition, request.getPlanId());

request.setServiceInstanceId(serviceInstanceId);
request.setServiceDefinition(serviceDefinition);
request.setPlan(plan);
setCommonRequestFields(request, pathVariables.get(ServiceBrokerRequest.PLATFORM_INSTANCE_ID_VARIABLE), apiInfoLocation,
originatingIdentityString, acceptsIncomplete);

Expand Down
Loading

0 comments on commit e88d839

Please sign in to comment.