@RequestMapping(value = { "/{cfInstanceId}/v2/service_instances/{instanceId}/service_bindings/{bindingId}", "/v2/service_instances/{instanceId}/service_bindings/{bindingId}" }, method = RequestMethod.PUT) public ResponseEntity<?> createServiceInstanceBinding(@PathVariable Map<String, String> pathVariables, @PathVariable("instanceId") String serviceInstanceId, @PathVariable("bindingId") String bindingId, @RequestHeader(value = API_INFO_LOCATION_HEADER, required = false) String apiInfoLocation, @RequestHeader(value = ORIGINATING_IDENTITY_HEADER, required = false) String originatingIdentityString, @Valid @RequestBody CreateServiceInstanceBindingRequest request) { request.withServiceInstanceId(serviceInstanceId) .withBindingId(bindingId) .withServiceDefinition(getServiceDefinition(request.getServiceDefinitionId())) .withCfInstanceId(pathVariables.get("cfInstanceId")) .withApiInfoLocation(apiInfoLocation) .withOriginatingIdentity(parseOriginatingIdentity(originatingIdentityString)); log.debug("Creating a service instance binding: request={}", request); CreateServiceInstanceBindingResponse response = serviceInstanceBindingService.createServiceInstanceBinding(request); log.debug("Creating a service instance binding succeeded: serviceInstanceId={}, bindingId={}, response={}", serviceInstanceId, bindingId, response); return new ResponseEntity<>(response, response.isBindingExisted() ? HttpStatus.OK : HttpStatus.CREATED); }
@Test public void new_binding_raise_exception_if_unknow_routing_type() { //given that the request contains an unknown request type createRouteBindingTemplate = new CreateServiceInstanceBindingRequest(SERVICE_DEFINITION_ID, PLAN_ID, null, Collections.singletonMap("NorRouteNeitherApp", "à!"), null); //when receive a new binding //then it should scream, and not save anything verifyThrown(() -> bindingService.createServiceInstanceBinding(createRouteBindingTemplate .withServiceInstanceId("Sid") .withBindingId("Bidroute")), ServiceBrokerException.class); verify(bindingRepository, never()).save(any(Binding.class)); }
@Test public void new_route_binding_should_not_store_binding() throws Exception { //given that the app is linked to the route in CF when(cfApi.listRouteApplications(ROUTE_UID)).thenReturn(singletonList(APP_UID)); //given that the application is known from autosleep when(appRepo.countByApplicationIds(singletonList(APP_UID))).thenReturn(1L); //when receive a new ROUTE binding bindingService.createServiceInstanceBinding(createRouteBindingTemplate.withServiceInstanceId("Sid") .withBindingId("Bidroute")); //then create it and store it to repository verify(appRepo, never()).save(any(ApplicationInfo.class)); verify(bindingRepository, never()).save(any(Binding.class)); }
/** * Init request templates with calaog definition, prepare mocks. */ @Before public void init() { createAppBindingTemplate = new CreateServiceInstanceBindingRequest(SERVICE_DEFINITION_ID, PLAN_ID, null, Collections.singletonMap(ServiceBindingResource.BIND_RESOURCE_KEY_APP.toString(), APP_UID), null); createRouteBindingTemplate = new CreateServiceInstanceBindingRequest(SERVICE_DEFINITION_ID, PLAN_ID, null, Collections.singletonMap(ServiceBindingResource.BIND_RESOURCE_KEY_ROUTE.toString(), ROUTE_UID), null); when(applicationInfo.getUuid()).thenReturn(APP_UID); when(applicationInfo.getEnrollmentState()).thenReturn(enrollmentState); when(spaceEnrollerConfigRepository.findOne(any(String.class))).thenReturn(spaceEnrollerConfig); //avoir nullpointer when getting credentials when(spaceEnrollerConfig.getIdleDuration()).thenReturn(Duration.ofSeconds(10)); doAnswer(invocationOnMock -> { ((Runnable) invocationOnMock.getArguments()[1]).run(); return null; }).when(applicationLocker).executeThreadSafe(anyString(), any(Runnable.class)); }
@Test public void new_app_binding_should_store_appinfo_binding_and_start_a_stopper() { //given that the application is unknown when(appRepo.findOne(APP_UID)).thenReturn(null); //when receive a new binding bindingService.createServiceInstanceBinding(createAppBindingTemplate.withServiceInstanceId("Sid") .withBindingId("Bid")); //then create it and store it to repository verify(appRepo, times(1)).save(any(ApplicationInfo.class)); verify(bindingRepository, times(1)).save(any(Binding.class)); verify(workerManager, times(1)).registerApplicationStopper(any(SpaceEnrollerConfig.class), anyString(), anyString()); }