BAEL-4026: Spring Cloud Open Service Broker (#9365)

* BAEL-4026: Spring Cloud Open Service Broker

* add unit tests

* fix review comments
This commit is contained in:
Eduard Ardeleanu 2020-06-03 16:04:45 +03:00 committed by GitHub
parent 078481781f
commit 6df3b2b6fb
14 changed files with 784 additions and 0 deletions

View File

@ -32,6 +32,7 @@
<module>spring-cloud-zuul-eureka-integration</module>
<module>spring-cloud-contract</module>
<module>spring-cloud-kubernetes</module>
<module>spring-cloud-open-service-broker</module>
<module>spring-cloud-archaius</module>
<module>spring-cloud-functions</module>
<module>spring-cloud-vault</module>

View File

@ -0,0 +1 @@
### Relevant Articles:

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>spring-cloud-open-service-broker</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.spring.cloud</groupId>
<artifactId>spring-cloud</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<properties>
<spring-cloud-starter-open-service-broker.version>3.1.1.RELEASE</spring-cloud-starter-open-service-broker.version>
<spring-boot-starter-web.version>2.2.7.RELEASE</spring-boot-starter-web.version>
<reactor-test.version>3.3.5.RELEASE</reactor-test.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-open-service-broker</artifactId>
<version>${spring-cloud-starter-open-service-broker.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot-starter-web.version}</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<version>${reactor-test.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,13 @@
package com.baeldung.spring.cloud.openservicebroker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ServiceBrokerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceBrokerApplication.class, args);
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.spring.cloud.openservicebroker.config;
import org.springframework.cloud.servicebroker.model.catalog.Catalog;
import org.springframework.cloud.servicebroker.model.catalog.Plan;
import org.springframework.cloud.servicebroker.model.catalog.ServiceDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CatalogConfiguration {
@Bean
public Catalog catalog() {
Plan mailFreePlan = Plan.builder()
.id("fd81196c-a414-43e5-bd81-1dbb082a3c55")
.name("mail-free-plan")
.description("Mail Service Free Plan")
.free(true)
.build();
ServiceDefinition serviceDefinition = ServiceDefinition.builder()
.id("b92c0ca7-c162-4029-b567-0d92978c0a97")
.name("mail-service")
.description("Mail Service")
.bindable(true)
.tags("mail", "service")
.plans(mailFreePlan)
.build();
return Catalog.builder()
.serviceDefinitions(serviceDefinition)
.build();
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.spring.cloud.openservicebroker.mail;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MailController {
@GetMapping("/mail-dashboard/{mailSystemId}")
public String dashboard(@PathVariable("mailSystemId") String mailSystemId) {
return "Mail Dashboard - " + mailSystemId;
}
@GetMapping("/mail-system/{mailSystemId}")
public String mailSystem(@PathVariable("mailSystemId") String mailSystemId) {
return "Mail System - " + mailSystemId;
}
}

View File

@ -0,0 +1,95 @@
package com.baeldung.spring.cloud.openservicebroker.mail;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@Service
public class MailService {
public static final String URI_KEY = "uri";
public static final String USERNAME_KEY = "username";
public static final String PASSWORD_KEY = "password";
private final String mailDashboardBaseURL;
private final String mailSystemBaseURL;
private Map<String, MailServiceInstance> mailServices = new HashMap<>();
private Map<String, MailServiceBinding> mailServiceBindings = new HashMap<>();
public MailService(@Value("${mail.system.dashboard.base-url}") String mailDashboardBaseURL,
@Value("${mail.system.base-url}") String mailSystemBaseURL) {
this.mailDashboardBaseURL = mailDashboardBaseURL;
this.mailSystemBaseURL = mailSystemBaseURL;
}
public Mono<MailServiceInstance> createServiceInstance(String instanceId, String serviceDefinitionId, String planId) {
MailServiceInstance mailServiceInstance = new MailServiceInstance(
instanceId, serviceDefinitionId, planId, mailDashboardBaseURL + instanceId);
mailServices.put(instanceId, mailServiceInstance);
return Mono.just(mailServiceInstance);
}
public Mono<Boolean> serviceInstanceExists(String instanceId) {
return Mono.just(mailServices.containsKey(instanceId));
}
public Mono<MailServiceInstance> getServiceInstance(String instanceId) {
if (mailServices.containsKey(instanceId)) {
return Mono.just(mailServices.get(instanceId));
}
return Mono.empty();
}
public Mono<Void> deleteServiceInstance(String instanceId) {
mailServices.remove(instanceId);
mailServiceBindings.remove(instanceId);
return Mono.empty();
}
public Mono<MailServiceBinding> createServiceBinding(String instanceId, String bindingId) {
return this.serviceInstanceExists(instanceId)
.flatMap(exists -> {
if (exists) {
MailServiceBinding mailServiceBinding =
new MailServiceBinding(bindingId, buildCredentials(instanceId, bindingId));
mailServiceBindings.put(instanceId, mailServiceBinding);
return Mono.just(mailServiceBinding);
} else {
return Mono.empty();
}
});
}
public Mono<Boolean> serviceBindingExists(String instanceId, String bindingId) {
return Mono.just(mailServiceBindings.containsKey(instanceId) &&
mailServiceBindings.get(instanceId).getBindingId().equalsIgnoreCase(bindingId));
}
public Mono<MailServiceBinding> getServiceBinding(String instanceId, String bindingId) {
if (mailServiceBindings.containsKey(instanceId) &&
mailServiceBindings.get(instanceId).getBindingId().equalsIgnoreCase(bindingId)) {
return Mono.just(mailServiceBindings.get(instanceId));
}
return Mono.empty();
}
public Mono<Void> deleteServiceBinding(String instanceId) {
mailServiceBindings.remove(instanceId);
return Mono.empty();
}
private Map<String, Object> buildCredentials(String instanceId, String bindingId) {
Map<String, Object> credentials = new HashMap<>();
credentials.put(URI_KEY, mailSystemBaseURL + instanceId);
credentials.put(USERNAME_KEY, bindingId);
credentials.put(PASSWORD_KEY, UUID.randomUUID().toString());
return credentials;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.spring.cloud.openservicebroker.mail;
import java.util.Map;
public class MailServiceBinding {
private String bindingId;
private Map<String, Object> credentials;
public MailServiceBinding(String bindingId, Map<String, Object> credentials) {
this.bindingId = bindingId;
this.credentials = credentials;
}
public String getBindingId() {
return bindingId;
}
public Map<String, Object> getCredentials() {
return credentials;
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.spring.cloud.openservicebroker.mail;
public class MailServiceInstance {
private String instanceId;
private String serviceDefinitionId;
private String planId;
private String dashboardUrl;
public MailServiceInstance(String instanceId, String serviceDefinitionId, String planId, String dashboardUrl) {
this.instanceId = instanceId;
this.serviceDefinitionId = serviceDefinitionId;
this.planId = planId;
this.dashboardUrl = dashboardUrl;
}
public String getInstanceId() {
return instanceId;
}
public String getServiceDefinitionId() {
return serviceDefinitionId;
}
public String getPlanId() {
return planId;
}
public String getDashboardUrl() {
return dashboardUrl;
}
}

View File

@ -0,0 +1,77 @@
package com.baeldung.spring.cloud.openservicebroker.services;
import com.baeldung.spring.cloud.openservicebroker.mail.MailService;
import org.springframework.cloud.servicebroker.exception.ServiceInstanceBindingDoesNotExistException;
import org.springframework.cloud.servicebroker.exception.ServiceInstanceDoesNotExistException;
import org.springframework.cloud.servicebroker.model.binding.CreateServiceInstanceAppBindingResponse;
import org.springframework.cloud.servicebroker.model.binding.CreateServiceInstanceBindingRequest;
import org.springframework.cloud.servicebroker.model.binding.CreateServiceInstanceBindingResponse;
import org.springframework.cloud.servicebroker.model.binding.DeleteServiceInstanceBindingRequest;
import org.springframework.cloud.servicebroker.model.binding.DeleteServiceInstanceBindingResponse;
import org.springframework.cloud.servicebroker.model.binding.GetServiceInstanceAppBindingResponse;
import org.springframework.cloud.servicebroker.model.binding.GetServiceInstanceBindingRequest;
import org.springframework.cloud.servicebroker.model.binding.GetServiceInstanceBindingResponse;
import org.springframework.cloud.servicebroker.service.ServiceInstanceBindingService;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
@Service
public class MailServiceInstanceBindingService implements ServiceInstanceBindingService {
private final MailService mailService;
public MailServiceInstanceBindingService(MailService mailService) {
this.mailService = mailService;
}
@Override
public Mono<CreateServiceInstanceBindingResponse> createServiceInstanceBinding(
CreateServiceInstanceBindingRequest request) {
return Mono.just(CreateServiceInstanceAppBindingResponse.builder())
.flatMap(responseBuilder -> mailService.serviceBindingExists(
request.getServiceInstanceId(), request.getBindingId())
.flatMap(exists -> {
if (exists) {
return mailService.getServiceBinding(
request.getServiceInstanceId(), request.getBindingId())
.flatMap(serviceBinding -> Mono.just(responseBuilder
.bindingExisted(true)
.credentials(serviceBinding.getCredentials())
.build()));
} else {
return mailService.createServiceBinding(
request.getServiceInstanceId(), request.getBindingId())
.switchIfEmpty(Mono.error(
new ServiceInstanceDoesNotExistException(
request.getServiceInstanceId())))
.flatMap(mailServiceBinding -> Mono.just(responseBuilder
.bindingExisted(false)
.credentials(mailServiceBinding.getCredentials())
.build()));
}
}));
}
@Override
public Mono<GetServiceInstanceBindingResponse> getServiceInstanceBinding(GetServiceInstanceBindingRequest request) {
return mailService.getServiceBinding(request.getServiceInstanceId(), request.getBindingId())
.switchIfEmpty(Mono.error(new ServiceInstanceBindingDoesNotExistException(request.getBindingId())))
.flatMap(mailServiceBinding -> Mono.just(GetServiceInstanceAppBindingResponse.builder()
.credentials(mailServiceBinding.getCredentials())
.build()));
}
@Override
public Mono<DeleteServiceInstanceBindingResponse> deleteServiceInstanceBinding(
DeleteServiceInstanceBindingRequest request) {
return mailService.serviceBindingExists(request.getServiceInstanceId(), request.getBindingId())
.flatMap(exists -> {
if (exists) {
return mailService.deleteServiceBinding(request.getServiceInstanceId())
.thenReturn(DeleteServiceInstanceBindingResponse.builder().build());
} else {
return Mono.error(new ServiceInstanceBindingDoesNotExistException(request.getBindingId()));
}
});
}
}

View File

@ -0,0 +1,72 @@
package com.baeldung.spring.cloud.openservicebroker.services;
import com.baeldung.spring.cloud.openservicebroker.mail.MailService;
import org.springframework.cloud.servicebroker.exception.ServiceInstanceDoesNotExistException;
import org.springframework.cloud.servicebroker.model.instance.CreateServiceInstanceRequest;
import org.springframework.cloud.servicebroker.model.instance.CreateServiceInstanceResponse;
import org.springframework.cloud.servicebroker.model.instance.DeleteServiceInstanceRequest;
import org.springframework.cloud.servicebroker.model.instance.DeleteServiceInstanceResponse;
import org.springframework.cloud.servicebroker.model.instance.GetServiceInstanceRequest;
import org.springframework.cloud.servicebroker.model.instance.GetServiceInstanceResponse;
import org.springframework.cloud.servicebroker.service.ServiceInstanceService;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
@Service
public class MailServiceInstanceService implements ServiceInstanceService {
private final MailService mailService;
public MailServiceInstanceService(MailService mailService) {
this.mailService = mailService;
}
@Override
public Mono<CreateServiceInstanceResponse> createServiceInstance(CreateServiceInstanceRequest request) {
return Mono.just(request.getServiceInstanceId())
.flatMap(instanceId -> Mono.just(CreateServiceInstanceResponse.builder())
.flatMap(responseBuilder -> mailService.serviceInstanceExists(instanceId)
.flatMap(exists -> {
if (exists) {
return mailService.getServiceInstance(instanceId)
.flatMap(mailServiceInstance -> Mono.just(responseBuilder
.instanceExisted(true)
.dashboardUrl(mailServiceInstance.getDashboardUrl())
.build()));
} else {
return mailService.createServiceInstance(
instanceId, request.getServiceDefinitionId(), request.getPlanId())
.flatMap(mailServiceInstance -> Mono.just(responseBuilder
.instanceExisted(false)
.dashboardUrl(mailServiceInstance.getDashboardUrl())
.build()));
}
})));
}
@Override
public Mono<DeleteServiceInstanceResponse> deleteServiceInstance(DeleteServiceInstanceRequest request) {
return Mono.just(request.getServiceInstanceId())
.flatMap(instanceId -> mailService.serviceInstanceExists(instanceId)
.flatMap(exists -> {
if (exists) {
return mailService.deleteServiceInstance(instanceId)
.thenReturn(DeleteServiceInstanceResponse.builder().build());
} else {
return Mono.error(new ServiceInstanceDoesNotExistException(instanceId));
}
}));
}
@Override
public Mono<GetServiceInstanceResponse> getServiceInstance(GetServiceInstanceRequest request) {
return Mono.just(request.getServiceInstanceId())
.flatMap(instanceId -> mailService.getServiceInstance(instanceId)
.switchIfEmpty(Mono.error(new ServiceInstanceDoesNotExistException(instanceId)))
.flatMap(serviceInstance -> Mono.just(GetServiceInstanceResponse.builder()
.serviceDefinitionId(serviceInstance.getServiceDefinitionId())
.planId(serviceInstance.getPlanId())
.dashboardUrl(serviceInstance.getDashboardUrl())
.build())));
}
}

View File

@ -0,0 +1,10 @@
spring:
cloud:
openservicebroker:
base-path: /broker
mail:
system:
base-url: http://localhost:8080/mail-system/
dashboard:
base-url: http://localhost:8080/mail-dashboard/

View File

@ -0,0 +1,201 @@
package com.baeldung.spring.cloud.openservicebroker.services;
import com.baeldung.spring.cloud.openservicebroker.mail.MailService;
import com.baeldung.spring.cloud.openservicebroker.mail.MailServiceBinding;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.cloud.servicebroker.exception.ServiceInstanceBindingDoesNotExistException;
import org.springframework.cloud.servicebroker.model.binding.CreateServiceInstanceAppBindingResponse;
import org.springframework.cloud.servicebroker.model.binding.CreateServiceInstanceBindingRequest;
import org.springframework.cloud.servicebroker.model.binding.DeleteServiceInstanceBindingRequest;
import org.springframework.cloud.servicebroker.model.binding.GetServiceInstanceAppBindingResponse;
import org.springframework.cloud.servicebroker.model.binding.GetServiceInstanceBindingRequest;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import java.util.HashMap;
import java.util.Map;
import static com.baeldung.spring.cloud.openservicebroker.mail.MailService.PASSWORD_KEY;
import static com.baeldung.spring.cloud.openservicebroker.mail.MailService.URI_KEY;
import static com.baeldung.spring.cloud.openservicebroker.mail.MailService.USERNAME_KEY;
import static java.util.UUID.randomUUID;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
public class MailServiceInstanceBindingServiceUnitTest {
private static final String MAIL_SERVICE_INSTANCE_ID = "test@baeldung.com";
private static final String MAIL_SERVICE_BINDING_ID = "test";
private static final String MAIL_SYSTEM_URL = "http://localhost:8080/mail-system/test@baeldung.com";
@Mock
private MailService mailService;
private MailServiceInstanceBindingService mailServiceInstanceBindingService;
@BeforeEach
public void setUp() {
initMocks(this);
this.mailServiceInstanceBindingService = new MailServiceInstanceBindingService(mailService);
}
@Test
public void givenServiceBindingDoesNotExist_whenCreateServiceBinding_thenNewBindingIsCreated() {
// given service binding does not exist
when(mailService.serviceBindingExists(MAIL_SERVICE_INSTANCE_ID, MAIL_SERVICE_BINDING_ID)).thenReturn(Mono.just(false));
Map<String, Object> credentials = generateCredentials();
MailServiceBinding serviceBinding = new MailServiceBinding(MAIL_SERVICE_BINDING_ID, credentials);
when(mailService.createServiceBinding(MAIL_SERVICE_INSTANCE_ID, MAIL_SERVICE_BINDING_ID))
.thenReturn(Mono.just(serviceBinding));
// when create service binding
CreateServiceInstanceBindingRequest request = CreateServiceInstanceBindingRequest.builder()
.serviceInstanceId(MAIL_SERVICE_INSTANCE_ID)
.bindingId(MAIL_SERVICE_BINDING_ID)
.build();
// then a new service binding is provisioned
StepVerifier.create(mailServiceInstanceBindingService.createServiceInstanceBinding(request))
.consumeNextWith(response -> {
assertTrue(response instanceof CreateServiceInstanceAppBindingResponse);
CreateServiceInstanceAppBindingResponse bindingResponse = (CreateServiceInstanceAppBindingResponse) response;
assertFalse(bindingResponse.isBindingExisted());
validateBindingCredentials(bindingResponse.getCredentials());
})
.verifyComplete();
}
@Test
public void givenServiceBindingExists_whenCreateServiceBinding_thenExistingBindingIsRetrieved() {
// given service binding exists
when(mailService.serviceBindingExists(MAIL_SERVICE_INSTANCE_ID, MAIL_SERVICE_BINDING_ID)).thenReturn(Mono.just(true));
Map<String, Object> credentials = generateCredentials();
MailServiceBinding serviceBinding = new MailServiceBinding(MAIL_SERVICE_BINDING_ID, credentials);
when(mailService.getServiceBinding(MAIL_SERVICE_INSTANCE_ID, MAIL_SERVICE_BINDING_ID))
.thenReturn(Mono.just(serviceBinding));
// when create service binding
CreateServiceInstanceBindingRequest request = CreateServiceInstanceBindingRequest.builder()
.serviceInstanceId(MAIL_SERVICE_INSTANCE_ID)
.bindingId(MAIL_SERVICE_BINDING_ID)
.build();
// then a new service binding is provisioned
StepVerifier.create(mailServiceInstanceBindingService.createServiceInstanceBinding(request))
.consumeNextWith(response -> {
assertTrue(response instanceof CreateServiceInstanceAppBindingResponse);
CreateServiceInstanceAppBindingResponse bindingResponse = (CreateServiceInstanceAppBindingResponse) response;
assertTrue(bindingResponse.isBindingExisted());
validateBindingCredentials(bindingResponse.getCredentials());
})
.verifyComplete();
}
@Test
public void givenServiceBindingDoesNotExist_whenGetServiceBinding_thenException() {
// given service binding does not exist
when(mailService.getServiceBinding(MAIL_SERVICE_INSTANCE_ID, MAIL_SERVICE_BINDING_ID)).thenReturn(Mono.empty());
// when get service binding
GetServiceInstanceBindingRequest request = GetServiceInstanceBindingRequest.builder()
.serviceInstanceId(MAIL_SERVICE_INSTANCE_ID)
.bindingId(MAIL_SERVICE_BINDING_ID)
.build();
// then ServiceInstanceBindingDoesNotExistException is thrown
StepVerifier.create(mailServiceInstanceBindingService.getServiceInstanceBinding(request))
.expectErrorMatches(ex -> ex instanceof ServiceInstanceBindingDoesNotExistException)
.verify();
}
@Test
public void givenServiceBindingExists_whenGetServiceBinding_thenExistingBindingIsRetrieved() {
// given service binding exists
Map<String, Object> credentials = generateCredentials();
MailServiceBinding serviceBinding = new MailServiceBinding(MAIL_SERVICE_BINDING_ID, credentials);
when(mailService.getServiceBinding(MAIL_SERVICE_INSTANCE_ID, MAIL_SERVICE_BINDING_ID))
.thenReturn(Mono.just(serviceBinding));
// when get service binding
GetServiceInstanceBindingRequest request = GetServiceInstanceBindingRequest.builder()
.serviceInstanceId(MAIL_SERVICE_INSTANCE_ID)
.bindingId(MAIL_SERVICE_BINDING_ID)
.build();
// then the existing service binding is retrieved
StepVerifier.create(mailServiceInstanceBindingService.getServiceInstanceBinding(request))
.consumeNextWith(response -> {
assertTrue(response instanceof GetServiceInstanceAppBindingResponse);
GetServiceInstanceAppBindingResponse bindingResponse = (GetServiceInstanceAppBindingResponse) response;
validateBindingCredentials(bindingResponse.getCredentials());
})
.verifyComplete();
}
@Test
public void givenServiceBindingDoesNotExist_whenDeleteServiceBinding_thenException() {
// given service binding does not exist
when(mailService.serviceBindingExists(MAIL_SERVICE_INSTANCE_ID, MAIL_SERVICE_BINDING_ID)).thenReturn(Mono.just(false));
// when delete service binding
DeleteServiceInstanceBindingRequest request = DeleteServiceInstanceBindingRequest.builder()
.serviceInstanceId(MAIL_SERVICE_INSTANCE_ID)
.bindingId(MAIL_SERVICE_BINDING_ID)
.build();
// then ServiceInstanceBindingDoesNotExistException is thrown
StepVerifier.create(mailServiceInstanceBindingService.deleteServiceInstanceBinding(request))
.expectErrorMatches(ex -> ex instanceof ServiceInstanceBindingDoesNotExistException)
.verify();
}
@Test
public void givenServiceBindingExists_whenDeleteServiceBinding_thenExistingBindingIsDeleted() {
// given service binding exists
when(mailService.serviceBindingExists(MAIL_SERVICE_INSTANCE_ID, MAIL_SERVICE_BINDING_ID)).thenReturn(Mono.just(true));
when(mailService.deleteServiceBinding(MAIL_SERVICE_INSTANCE_ID)).thenReturn(Mono.empty());
// when delete service binding
DeleteServiceInstanceBindingRequest request = DeleteServiceInstanceBindingRequest.builder()
.serviceInstanceId(MAIL_SERVICE_INSTANCE_ID)
.bindingId(MAIL_SERVICE_BINDING_ID)
.build();
// then the existing service binding is retrieved
StepVerifier.create(mailServiceInstanceBindingService.deleteServiceInstanceBinding(request))
.consumeNextWith(response -> {
assertFalse(response.isAsync());
assertNull(response.getOperation());
})
.verifyComplete();
}
private void validateBindingCredentials(Map<String, Object> bindingCredentials) {
assertNotNull(bindingCredentials);
assertEquals(3, bindingCredentials.size());
assertTrue(bindingCredentials.containsKey(URI_KEY));
assertTrue(bindingCredentials.containsKey(USERNAME_KEY));
assertTrue(bindingCredentials.containsKey(PASSWORD_KEY));
assertEquals(MAIL_SYSTEM_URL, bindingCredentials.get(URI_KEY));
assertEquals(MAIL_SERVICE_BINDING_ID, bindingCredentials.get(USERNAME_KEY));
assertNotNull(bindingCredentials.get(PASSWORD_KEY));
}
private Map<String, Object> generateCredentials() {
Map<String, Object> credentials = new HashMap<>();
credentials.put(URI_KEY, MAIL_SYSTEM_URL);
credentials.put(USERNAME_KEY, MAIL_SERVICE_BINDING_ID);
credentials.put(PASSWORD_KEY, randomUUID().toString());
return credentials;
}
}

View File

@ -0,0 +1,166 @@
package com.baeldung.spring.cloud.openservicebroker.services;
import com.baeldung.spring.cloud.openservicebroker.mail.MailService;
import com.baeldung.spring.cloud.openservicebroker.mail.MailServiceInstance;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.cloud.servicebroker.exception.ServiceInstanceDoesNotExistException;
import org.springframework.cloud.servicebroker.model.instance.CreateServiceInstanceRequest;
import org.springframework.cloud.servicebroker.model.instance.DeleteServiceInstanceRequest;
import org.springframework.cloud.servicebroker.model.instance.GetServiceInstanceRequest;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
public class MailServiceInstanceServiceUnitTest {
private static final String MAIL_SERVICE_INSTANCE_ID = "test@baeldung.com";
private static final String MAIL_SERVICE_DEFINITION_ID = "mock-service-definition-id";
private static final String MAIL_SERVICE_PLAN_ID = "mock-service-plan-id";
private static final String MAIL_DASHBOARD_URL = "http://localhost:8080/mail-dashboard/test@baeldung.com";
@Mock
private MailService mailService;
private MailServiceInstanceService mailServiceInstanceService;
@BeforeEach
public void setUp() {
initMocks(this);
this.mailServiceInstanceService = new MailServiceInstanceService(mailService);
}
@Test
public void givenServiceInstanceDoesNotExist_whenCreateServiceInstance_thenProvisionNewService() {
// given service instance does not exist
when(mailService.serviceInstanceExists(MAIL_SERVICE_INSTANCE_ID)).thenReturn(Mono.just(false));
MailServiceInstance serviceInstance = new MailServiceInstance(MAIL_SERVICE_INSTANCE_ID, MAIL_SERVICE_DEFINITION_ID,
MAIL_SERVICE_PLAN_ID, MAIL_DASHBOARD_URL);
when(mailService.createServiceInstance(MAIL_SERVICE_INSTANCE_ID, MAIL_SERVICE_DEFINITION_ID, MAIL_SERVICE_PLAN_ID))
.thenReturn(Mono.just(serviceInstance));
// when create service instance
CreateServiceInstanceRequest request = CreateServiceInstanceRequest.builder()
.serviceInstanceId(MAIL_SERVICE_INSTANCE_ID)
.serviceDefinitionId(MAIL_SERVICE_DEFINITION_ID)
.planId(MAIL_SERVICE_PLAN_ID)
.build();
// then a new service instance is provisioned
StepVerifier.create(mailServiceInstanceService.createServiceInstance(request))
.consumeNextWith(response -> {
assertFalse(response.isInstanceExisted());
assertEquals(MAIL_DASHBOARD_URL, response.getDashboardUrl());
})
.verifyComplete();
}
@Test
public void givenServiceInstanceExists_whenCreateServiceInstance_thenExistingServiceInstanceIsRetrieved() {
// given service instance exists
when(mailService.serviceInstanceExists(MAIL_SERVICE_INSTANCE_ID)).thenReturn(Mono.just(true));
MailServiceInstance serviceInstance = new MailServiceInstance(MAIL_SERVICE_INSTANCE_ID, MAIL_SERVICE_DEFINITION_ID,
MAIL_SERVICE_PLAN_ID, MAIL_DASHBOARD_URL);
when(mailService.getServiceInstance(MAIL_SERVICE_INSTANCE_ID)).thenReturn(Mono.just(serviceInstance));
// when create service instance
CreateServiceInstanceRequest request = CreateServiceInstanceRequest.builder()
.serviceInstanceId(MAIL_SERVICE_INSTANCE_ID)
.serviceDefinitionId(MAIL_SERVICE_DEFINITION_ID)
.planId(MAIL_SERVICE_PLAN_ID)
.build();
// then the existing one is retrieved
StepVerifier.create(mailServiceInstanceService.createServiceInstance(request))
.consumeNextWith(response -> {
assertTrue(response.isInstanceExisted());
assertEquals(MAIL_DASHBOARD_URL, response.getDashboardUrl());
})
.verifyComplete();
}
@Test
public void givenServiceInstanceDoesNotExist_whenDeleteServiceInstance_thenException() {
// given service instance does not exist
when(mailService.serviceInstanceExists(MAIL_SERVICE_INSTANCE_ID)).thenReturn(Mono.just(false));
// when delete service instance
DeleteServiceInstanceRequest request = DeleteServiceInstanceRequest.builder()
.serviceInstanceId(MAIL_SERVICE_INSTANCE_ID)
.build();
// then ServiceInstanceDoesNotExistException is thrown
StepVerifier.create(mailServiceInstanceService.deleteServiceInstance(request))
.expectErrorMatches(ex -> ex instanceof ServiceInstanceDoesNotExistException)
.verify();
}
@Test
public void givenServiceInstanceExists_whenDeleteServiceInstance_thenSuccess() {
// given service instance exists
when(mailService.serviceInstanceExists(MAIL_SERVICE_INSTANCE_ID)).thenReturn(Mono.just(true));
// when delete service instance
when(mailService.deleteServiceInstance(MAIL_SERVICE_INSTANCE_ID)).thenReturn(Mono.empty());
DeleteServiceInstanceRequest request = DeleteServiceInstanceRequest.builder()
.serviceInstanceId(MAIL_SERVICE_INSTANCE_ID)
.build();
// then success
StepVerifier.create(mailServiceInstanceService.deleteServiceInstance(request))
.consumeNextWith(response -> {
assertFalse(response.isAsync());
assertNull(response.getOperation());
})
.verifyComplete();
}
@Test
public void givenServiceInstanceDoesNotExist_whenGetServiceInstance_thenException() {
// given service instance does not exist
when(mailService.getServiceInstance(MAIL_SERVICE_INSTANCE_ID)).thenReturn(Mono.empty());
// when get service instance
GetServiceInstanceRequest request = GetServiceInstanceRequest.builder()
.serviceInstanceId(MAIL_SERVICE_INSTANCE_ID)
.build();
// then ServiceInstanceDoesNotExistException is thrown
StepVerifier.create(mailServiceInstanceService.getServiceInstance(request))
.expectErrorMatches(ex -> ex instanceof ServiceInstanceDoesNotExistException)
.verify();
}
@Test
public void givenServiceInstanceExists_whenGetServiceInstance_thenExistingServiceInstanceIsRetrieved() {
// given service instance exists
MailServiceInstance serviceInstance = new MailServiceInstance(MAIL_SERVICE_INSTANCE_ID, MAIL_SERVICE_DEFINITION_ID,
MAIL_SERVICE_PLAN_ID, MAIL_DASHBOARD_URL);
when(mailService.getServiceInstance(MAIL_SERVICE_INSTANCE_ID)).thenReturn(Mono.just(serviceInstance));
// when get service instance
GetServiceInstanceRequest request = GetServiceInstanceRequest.builder()
.serviceInstanceId(MAIL_SERVICE_INSTANCE_ID)
.build();
// then the existing service instance is retrieved
StepVerifier.create(mailServiceInstanceService.getServiceInstance(request))
.consumeNextWith(response -> {
assertEquals(MAIL_SERVICE_DEFINITION_ID, response.getServiceDefinitionId());
assertEquals(MAIL_SERVICE_PLAN_ID, response.getPlanId());
assertEquals(MAIL_DASHBOARD_URL, response.getDashboardUrl());
})
.verifyComplete();
}
}