AutosleepServiceInstanceService implements ServiceInstanceService { @Override public CreateServiceInstanceResponse createServiceInstance(CreateServiceInstanceRequest request) throws ServiceInstanceExistsException, ServiceBrokerException { String serviceId = request.getServiceInstanceId(); log.debug("createServiceInstance - {}", serviceId); rejectDuplicateServiceInstanceRequest(serviceId); Map<String, Object> createParameters = Optional .ofNullable(request.getParameters()) .orElse(Collections.emptyMap()); Enrollment autoEnrollment = consumeParameter(createParameters, true, autoEnrollmentReader); String secret = consumeParameter(createParameters, true, secretReader); Duration idleDuration = consumeParameter(createParameters, true, idleDurationReader); Pattern excludeFromAutoEnrollment = consumeParameter(createParameters, true, excludeFromAutoEnrollmentReader); Boolean ignoreRouteServiceError = consumeParameter(createParameters, true, ignoreRouteServiceErrorReader); rejectExtraUnknownParameters(createParameters); if (autoEnrollment == Enrollment.forced || autoEnrollment == Enrollment.transient_opt_out) { checkSecuredParameter(autoEnrollmentReader.getParameterName(), secret); } SpaceEnrollerConfig spaceEnrollerConfig = SpaceEnrollerConfig.builder() .id(request.getServiceInstanceId()) .serviceDefinitionId(request.getServiceDefinitionId()) .planId(request.getPlanId()) .organizationId(request.getOrganizationGuid()) .spaceId(request.getSpaceGuid()) .idleDuration(idleDuration) .ignoreRouteServiceError(ignoreRouteServiceError) .excludeFromAutoEnrollment(excludeFromAutoEnrollment) .enrollment(autoEnrollment) .secret(secret != null ? passwordEncoder.encode(secret) : null) .build(); spaceEnrollerConfigRepository.save(spaceEnrollerConfig); workerManager.registerSpaceEnroller(spaceEnrollerConfig); String firstUri = deployment == null ? null : deployment.getFirstUri(); if (firstUri == null) { firstUri = "local-deployment"; } return new CreateServiceInstanceResponse() .withDashboardUrl(firstUri + Config.Path.DASHBOARD_CONTEXT + "/" + serviceId) .withAsync(false); }  @Override CreateServiceInstanceResponse createServiceInstance(CreateServiceInstanceRequest request); @Override DeleteServiceInstanceResponse deleteServiceInstance(
            DeleteServiceInstanceRequest request); @Override GetLastServiceOperationResponse getLastOperation(GetLastServiceOperationRequest
                                                                    getLastServiceOperationRequest); @Override UpdateServiceInstanceResponse updateServiceInstance(
            UpdateServiceInstanceRequest request);  }
@Test public void test_arbitrary_duration_is_stored() throws Exception { when(spaceEnrollerConfigRepository.exists(SERVICE_INSTANCE_ID)).thenReturn(false); Map<String, Object> params = singletonMap(Config.ServiceInstanceParameters.IDLE_DURATION, "PT10H"); CreateServiceInstanceRequest createRequest = getCreateRequestWithArbitraryParams(params); instanceService.createServiceInstance(createRequest); verify(spaceEnrollerConfigRepository, times(1)).save(any(SpaceEnrollerConfig.class)); assertThat(serviceInstances.size(), is(equalTo(1))); SpaceEnrollerConfig serviceInstance = serviceInstances.get(0); assertThat(serviceInstance.getIdleDuration(), is(equalTo(Duration.ofHours(10)))); }
@Test public void test_creation_with_default() throws Exception { when(spaceEnrollerConfigRepository.exists(SERVICE_INSTANCE_ID)).thenReturn(false); CreateServiceInstanceResponse si = instanceService.createServiceInstance(getCreateRequestWithArbitraryParams(null)); verify(workerManager, times(1)).registerSpaceEnroller(any(SpaceEnrollerConfig.class)); verify(passwordEncoder, never()).encode(anyString()); assertThat(si, is(notNullValue())); verify(spaceEnrollerConfigRepository, times(1)).save(any(SpaceEnrollerConfig.class)); assertThat(serviceInstances.size(), is(equalTo(1))); SpaceEnrollerConfig serviceInstance = serviceInstances.get(0); assertFalse(serviceInstance.getEnrollment() == Enrollment.forced); assertThat(serviceInstance.getIdleDuration(), is(equalTo(Config.DEFAULT_INACTIVITY_PERIOD))); assertThat(serviceInstance.isIgnoreRouteServiceError(), is(equalTo(Boolean.FALSE))); assertThat(serviceInstance.getSecret(), is(nullValue())); assertThat(serviceInstance.getExcludeFromAutoEnrollment(), is(nullValue())); }
@Test public void test_exclusion_is_well_read() throws Exception { when(spaceEnrollerConfigRepository.exists(SERVICE_INSTANCE_ID)).thenReturn(false); Map<String, Object> params = singletonMap( Config.ServiceInstanceParameters.EXCLUDE_FROM_AUTO_ENROLLMENT, ".*"); CreateServiceInstanceRequest createRequest = getCreateRequestWithArbitraryParams(params); instanceService.createServiceInstance(createRequest); assertThat(serviceInstances.size(), is(equalTo(1))); SpaceEnrollerConfig serviceInstance = serviceInstances.get(0); assertThat(serviceInstance.getExcludeFromAutoEnrollment().pattern(), is(equalTo(".*"))); }
@Test public void test_forced_auto_enrollment_fails_without_secret() throws Exception { when(spaceEnrollerConfigRepository.exists(SERVICE_INSTANCE_ID)).thenReturn(false); Map<String, Object> params = singletonMap(Config.ServiceInstanceParameters.AUTO_ENROLLMENT, Enrollment.forced.name()); CreateServiceInstanceRequest createRequest = getCreateRequestWithArbitraryParams(params); verifyThrown(() -> instanceService.createServiceInstance(createRequest), InvalidParameterException.class, exceptionThrown -> assertThat(exceptionThrown.getParameterName(), is(equalTo(Config.ServiceInstanceParameters.AUTO_ENROLLMENT)))); }
@Test public void test_forced_auto_enrollment_succeeds_with_secret() throws Exception { when(spaceEnrollerConfigRepository.exists(SERVICE_INSTANCE_ID)).thenReturn(false); Map<String, Object> parameters = new HashMap<>(); parameters.put(Config.ServiceInstanceParameters.SECRET, "secret"); parameters.put(Config.ServiceInstanceParameters.AUTO_ENROLLMENT, Enrollment.forced.name()); CreateServiceInstanceRequest createRequest = getCreateRequestWithArbitraryParams(parameters); CreateServiceInstanceResponse si = instanceService.createServiceInstance(createRequest); assertThat(si, is(notNullValue())); assertThat(serviceInstances.size(), is(equalTo(1))); SpaceEnrollerConfig serviceInstance = serviceInstances.get(0); assertTrue(serviceInstance.getEnrollment() == Enrollment.forced); }
@Test public void test_ignore_route_error_is_well_read() throws Exception { when(spaceEnrollerConfigRepository.exists(SERVICE_INSTANCE_ID)).thenReturn(false); Map<String, Object> params = singletonMap( ServiceInstanceParameters.IGNORE_ROUTE_SERVICE_ERROR, Boolean.TRUE.toString()); CreateServiceInstanceRequest createRequest = getCreateRequestWithArbitraryParams(params); instanceService.createServiceInstance(createRequest); assertThat(serviceInstances.size(), is(equalTo(1))); SpaceEnrollerConfig serviceInstance = serviceInstances.get(0); assertThat(serviceInstance.isIgnoreRouteServiceError(), is(equalTo(Boolean.TRUE))); }
@Test public void test_no_creation_accepted_when_already_exists() { when(spaceEnrollerConfigRepository.exists(SERVICE_INSTANCE_ID)).thenReturn(true); verifyThrown(() -> instanceService.createServiceInstance(getCreateRequestWithArbitraryParams(null)), ServiceInstanceExistsException.class); }
@Test public void test_no_creation_accepted_when_unknown_parameter() { when(spaceEnrollerConfigRepository.exists(SERVICE_INSTANCE_ID)).thenReturn(false); String unknwonParameter = "unknownParameter"; Map<String, Object> params = singletonMap(unknwonParameter, "unknownParameterValue"); CreateServiceInstanceRequest createRequest = getCreateRequestWithArbitraryParams(params); verifyThrown(() -> instanceService.createServiceInstance(createRequest), InvalidParameterException.class, parameterChecked -> assertThat(parameterChecked.getParameterName(), is(equalTo(unknwonParameter)))); }
@Test public void test_secret_is_well_read() throws Exception { when(spaceEnrollerConfigRepository.exists(SERVICE_INSTANCE_ID)).thenReturn(false); Map<String, Object> params = singletonMap(Config.ServiceInstanceParameters.SECRET, "password"); CreateServiceInstanceRequest createRequest = getCreateRequestWithArbitraryParams(params); CreateServiceInstanceResponse si = instanceService.createServiceInstance(createRequest); verify(passwordEncoder, times(1)).encode(eq("password")); assertThat(si, is(notNullValue())); assertThat(serviceInstances.size(), is(equalTo(1))); SpaceEnrollerConfig serviceInstance = serviceInstances.get(0); assertThat(serviceInstance.getSecret(), is(equalTo(passwordEncoded))); }