Skip to content

Commit

Permalink
rename client databases and switch from 3 to 2 of them (eugenp#3028)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisoberle authored and maibin committed Nov 13, 2017
1 parent 2e5531e commit 25fe85d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public enum ClientDatabase {

ACME_WIDGETS, WIDGETS_ARE_US, WIDGET_DEPOT
CLIENT_A, CLIENT_B

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,30 @@ public ClientService clientService() {
@Bean
public DataSource clientDatasource() {
Map<Object, Object> targetDataSources = new HashMap<>();
DataSource acmeWidgetsDatasource = acmeWidgetsDatasource();
DataSource widgetsAreUsDatasource = widgetsAreUsDatasource();
DataSource widgetsDepotDatasource = widgetsDepotDatasource();
targetDataSources.put(ClientDatabase.ACME_WIDGETS, acmeWidgetsDatasource);
targetDataSources.put(ClientDatabase.WIDGETS_ARE_US, widgetsAreUsDatasource);
targetDataSources.put(ClientDatabase.WIDGET_DEPOT, widgetsDepotDatasource);
DataSource clientADatasource = clientADatasource();
DataSource clientBDatasource = clientBDatasource();
targetDataSources.put(ClientDatabase.CLIENT_A, clientADatasource);
targetDataSources.put(ClientDatabase.CLIENT_B, clientBDatasource);

ClientDataSourceRouter clientRoutingDatasource = new ClientDataSourceRouter();
clientRoutingDatasource.setTargetDataSources(targetDataSources);
clientRoutingDatasource.setDefaultTargetDataSource(acmeWidgetsDatasource);
clientRoutingDatasource.setDefaultTargetDataSource(clientADatasource);
return clientRoutingDatasource;
}

private DataSource acmeWidgetsDatasource() {
private DataSource clientADatasource() {
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase embeddedDb = dbBuilder.setType(EmbeddedDatabaseType.H2)
.setName("ACMEWIDGETS")
.setName("CLIENT_A")
.addScript("classpath:dsrouting-db.sql")
.build();
return embeddedDb;
}

private DataSource widgetsAreUsDatasource() {
private DataSource clientBDatasource() {
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase embeddedDb = dbBuilder.setType(EmbeddedDatabaseType.H2)
.setName("WIDGETSAREUS")
.addScript("classpath:dsrouting-db.sql")
.build();
return embeddedDb;
}

private DataSource widgetsDepotDatasource() {
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase embeddedDb = dbBuilder.setType(EmbeddedDatabaseType.H2)
.setName("WIDGETDEPOT")
.setName("CLIENT_B")
.addScript("classpath:dsrouting-db.sql")
.build();
return embeddedDb;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,30 @@ public class DataSourceRoutingTests {

@Before
public void setup() {
final String SQL_ACME_WIDGETS = "insert into client (id, name) values (1, 'ACME WIDGETS')";
final String SQL_WIDGETS_ARE_US = "insert into client (id, name) values (2, 'WIDGETS ARE US')";
final String SQL_WIDGET_DEPOT = "insert into client (id, name) values (3, 'WIDGET DEPOT')";
final String SQL_CLIENT_A = "insert into client (id, name) values (1, 'CLIENT A')";
final String SQL_CLIENT_B = "insert into client (id, name) values (2, 'CLIENT B')";

JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(routingDatasource);

ClientDatabaseContextHolder.set(ClientDatabase.ACME_WIDGETS);
jdbcTemplate.execute(SQL_ACME_WIDGETS);
ClientDatabaseContextHolder.set(ClientDatabase.CLIENT_A);
jdbcTemplate.execute(SQL_CLIENT_A);
ClientDatabaseContextHolder.clear();

ClientDatabaseContextHolder.set(ClientDatabase.WIDGETS_ARE_US);
jdbcTemplate.execute(SQL_WIDGETS_ARE_US);
ClientDatabaseContextHolder.clear();

ClientDatabaseContextHolder.set(ClientDatabase.WIDGET_DEPOT);
jdbcTemplate.execute(SQL_WIDGET_DEPOT);
ClientDatabaseContextHolder.set(ClientDatabase.CLIENT_B);
jdbcTemplate.execute(SQL_CLIENT_B);
ClientDatabaseContextHolder.clear();
}

@Test
public void givenClientDbs_whenContextsSwitch_thenRouteToCorrectDatabase() throws Exception {

// test ACME WIDGETS
String clientName = clientService.getClientName(ClientDatabase.ACME_WIDGETS);
assertEquals(clientName, "ACME WIDGETS");
String clientName = clientService.getClientName(ClientDatabase.CLIENT_A);
assertEquals(clientName, "CLIENT A");

// test WIDGETS_ARE_US
clientName = clientService.getClientName(ClientDatabase.WIDGETS_ARE_US);
assertEquals(clientName, "WIDGETS ARE US");

// test WIDGET_DEPOT
clientName = clientService.getClientName(ClientDatabase.WIDGET_DEPOT);
assertEquals(clientName, "WIDGET DEPOT");
clientName = clientService.getClientName(ClientDatabase.CLIENT_B);
assertEquals(clientName, "CLIENT B");
}
}

0 comments on commit 25fe85d

Please sign in to comment.