rename client databases and switch from 3 to 2 of them (#3028)
This commit is contained in:
parent
2e5531edd0
commit
25fe85d849
|
@ -2,6 +2,6 @@ package org.baeldung.dsrouting;
|
||||||
|
|
||||||
public enum ClientDatabase {
|
public enum ClientDatabase {
|
||||||
|
|
||||||
ACME_WIDGETS, WIDGETS_ARE_US, WIDGET_DEPOT
|
CLIENT_A, CLIENT_B
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,41 +22,30 @@ public class DataSourceRoutingTestConfiguration {
|
||||||
@Bean
|
@Bean
|
||||||
public DataSource clientDatasource() {
|
public DataSource clientDatasource() {
|
||||||
Map<Object, Object> targetDataSources = new HashMap<>();
|
Map<Object, Object> targetDataSources = new HashMap<>();
|
||||||
DataSource acmeWidgetsDatasource = acmeWidgetsDatasource();
|
DataSource clientADatasource = clientADatasource();
|
||||||
DataSource widgetsAreUsDatasource = widgetsAreUsDatasource();
|
DataSource clientBDatasource = clientBDatasource();
|
||||||
DataSource widgetsDepotDatasource = widgetsDepotDatasource();
|
targetDataSources.put(ClientDatabase.CLIENT_A, clientADatasource);
|
||||||
targetDataSources.put(ClientDatabase.ACME_WIDGETS, acmeWidgetsDatasource);
|
targetDataSources.put(ClientDatabase.CLIENT_B, clientBDatasource);
|
||||||
targetDataSources.put(ClientDatabase.WIDGETS_ARE_US, widgetsAreUsDatasource);
|
|
||||||
targetDataSources.put(ClientDatabase.WIDGET_DEPOT, widgetsDepotDatasource);
|
|
||||||
|
|
||||||
ClientDataSourceRouter clientRoutingDatasource = new ClientDataSourceRouter();
|
ClientDataSourceRouter clientRoutingDatasource = new ClientDataSourceRouter();
|
||||||
clientRoutingDatasource.setTargetDataSources(targetDataSources);
|
clientRoutingDatasource.setTargetDataSources(targetDataSources);
|
||||||
clientRoutingDatasource.setDefaultTargetDataSource(acmeWidgetsDatasource);
|
clientRoutingDatasource.setDefaultTargetDataSource(clientADatasource);
|
||||||
return clientRoutingDatasource;
|
return clientRoutingDatasource;
|
||||||
}
|
}
|
||||||
|
|
||||||
private DataSource acmeWidgetsDatasource() {
|
private DataSource clientADatasource() {
|
||||||
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
|
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
|
||||||
EmbeddedDatabase embeddedDb = dbBuilder.setType(EmbeddedDatabaseType.H2)
|
EmbeddedDatabase embeddedDb = dbBuilder.setType(EmbeddedDatabaseType.H2)
|
||||||
.setName("ACMEWIDGETS")
|
.setName("CLIENT_A")
|
||||||
.addScript("classpath:dsrouting-db.sql")
|
.addScript("classpath:dsrouting-db.sql")
|
||||||
.build();
|
.build();
|
||||||
return embeddedDb;
|
return embeddedDb;
|
||||||
}
|
}
|
||||||
|
|
||||||
private DataSource widgetsAreUsDatasource() {
|
private DataSource clientBDatasource() {
|
||||||
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
|
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
|
||||||
EmbeddedDatabase embeddedDb = dbBuilder.setType(EmbeddedDatabaseType.H2)
|
EmbeddedDatabase embeddedDb = dbBuilder.setType(EmbeddedDatabaseType.H2)
|
||||||
.setName("WIDGETSAREUS")
|
.setName("CLIENT_B")
|
||||||
.addScript("classpath:dsrouting-db.sql")
|
|
||||||
.build();
|
|
||||||
return embeddedDb;
|
|
||||||
}
|
|
||||||
|
|
||||||
private DataSource widgetsDepotDatasource() {
|
|
||||||
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
|
|
||||||
EmbeddedDatabase embeddedDb = dbBuilder.setType(EmbeddedDatabaseType.H2)
|
|
||||||
.setName("WIDGETDEPOT")
|
|
||||||
.addScript("classpath:dsrouting-db.sql")
|
.addScript("classpath:dsrouting-db.sql")
|
||||||
.build();
|
.build();
|
||||||
return embeddedDb;
|
return embeddedDb;
|
||||||
|
|
|
@ -24,23 +24,18 @@ public class DataSourceRoutingTests {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
final String SQL_ACME_WIDGETS = "insert into client (id, name) values (1, 'ACME WIDGETS')";
|
final String SQL_CLIENT_A = "insert into client (id, name) values (1, 'CLIENT A')";
|
||||||
final String SQL_WIDGETS_ARE_US = "insert into client (id, name) values (2, 'WIDGETS ARE US')";
|
final String SQL_CLIENT_B = "insert into client (id, name) values (2, 'CLIENT B')";
|
||||||
final String SQL_WIDGET_DEPOT = "insert into client (id, name) values (3, 'WIDGET DEPOT')";
|
|
||||||
|
|
||||||
JdbcTemplate jdbcTemplate = new JdbcTemplate();
|
JdbcTemplate jdbcTemplate = new JdbcTemplate();
|
||||||
jdbcTemplate.setDataSource(routingDatasource);
|
jdbcTemplate.setDataSource(routingDatasource);
|
||||||
|
|
||||||
ClientDatabaseContextHolder.set(ClientDatabase.ACME_WIDGETS);
|
ClientDatabaseContextHolder.set(ClientDatabase.CLIENT_A);
|
||||||
jdbcTemplate.execute(SQL_ACME_WIDGETS);
|
jdbcTemplate.execute(SQL_CLIENT_A);
|
||||||
ClientDatabaseContextHolder.clear();
|
ClientDatabaseContextHolder.clear();
|
||||||
|
|
||||||
ClientDatabaseContextHolder.set(ClientDatabase.WIDGETS_ARE_US);
|
ClientDatabaseContextHolder.set(ClientDatabase.CLIENT_B);
|
||||||
jdbcTemplate.execute(SQL_WIDGETS_ARE_US);
|
jdbcTemplate.execute(SQL_CLIENT_B);
|
||||||
ClientDatabaseContextHolder.clear();
|
|
||||||
|
|
||||||
ClientDatabaseContextHolder.set(ClientDatabase.WIDGET_DEPOT);
|
|
||||||
jdbcTemplate.execute(SQL_WIDGET_DEPOT);
|
|
||||||
ClientDatabaseContextHolder.clear();
|
ClientDatabaseContextHolder.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,15 +43,11 @@ public class DataSourceRoutingTests {
|
||||||
public void givenClientDbs_whenContextsSwitch_thenRouteToCorrectDatabase() throws Exception {
|
public void givenClientDbs_whenContextsSwitch_thenRouteToCorrectDatabase() throws Exception {
|
||||||
|
|
||||||
// test ACME WIDGETS
|
// test ACME WIDGETS
|
||||||
String clientName = clientService.getClientName(ClientDatabase.ACME_WIDGETS);
|
String clientName = clientService.getClientName(ClientDatabase.CLIENT_A);
|
||||||
assertEquals(clientName, "ACME WIDGETS");
|
assertEquals(clientName, "CLIENT A");
|
||||||
|
|
||||||
// test WIDGETS_ARE_US
|
// test WIDGETS_ARE_US
|
||||||
clientName = clientService.getClientName(ClientDatabase.WIDGETS_ARE_US);
|
clientName = clientService.getClientName(ClientDatabase.CLIENT_B);
|
||||||
assertEquals(clientName, "WIDGETS ARE US");
|
assertEquals(clientName, "CLIENT B");
|
||||||
|
|
||||||
// test WIDGET_DEPOT
|
|
||||||
clientName = clientService.getClientName(ClientDatabase.WIDGET_DEPOT);
|
|
||||||
assertEquals(clientName, "WIDGET DEPOT");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue