2727import org .springframework .beans .factory .config .BeanPostProcessor ;
2828import org .springframework .context .ApplicationListener ;
2929import org .springframework .context .annotation .Bean ;
30+ import org .springframework .context .annotation .Profile ;
3031import org .springframework .context .event .ContextRefreshedEvent ;
3132import org .springframework .core .io .ClassPathResource ;
3233import org .springframework .core .io .Resource ;
3839import org .springframework .data .cassandra .core .cql .session .init .KeyspacePopulator ;
3940import org .springframework .data .cassandra .core .cql .session .init .ResourceKeyspacePopulator ;
4041import org .springframework .data .cassandra .core .cql .session .init .SessionFactoryInitializer ;
42+ import org .springframework .lang .NonNull ;
4143
4244import example .app .crm .model .Customer ;
4345import example .app .crm .repo .CustomerRepository ;
@@ -64,15 +66,30 @@ public abstract class TestCassandraConfiguration {
6466 private static final boolean CONTINUE_ON_ERROR = false ;
6567 private static final boolean IGNORE_FAILED_DROPS = true ;
6668
69+ private static final Customer pieDoe = Customer .newCustomer (16L , "Pie Doe" );
70+
6771 private static final String CQL_SCRIPT_ENCODING = null ;
6872
6973 protected static final int CASSANDRA_DEFAULT_PORT = CqlSessionFactoryBean .DEFAULT_PORT ;
7074
7175 protected static final String CASSANDRA_DATA_CQL = "cassandra-data.cql" ;
7276 protected static final String CASSANDRA_INIT_CQL = "cassandra-init.cql" ;
7377 protected static final String CASSANDRA_SCHEMA_CQL = "cassandra-schema.cql" ;
74- protected static final String LOCAL_DATA_CENTER = "datacenter1 " ;
78+ protected static final String DEBUGGING_PROFILE = "debugging " ;
7579 protected static final String KEYSPACE_NAME = "CustomerService" ;
80+ protected static final String TABLE_NAME = "Customers" ;
81+
82+ protected @ NonNull Resource newCassandraDataCqlScriptResource () {
83+ return new ClassPathResource (CASSANDRA_DATA_CQL );
84+ }
85+
86+ protected @ NonNull Resource newCassandraInitCqlScriptResource () {
87+ return new ClassPathResource (CASSANDRA_INIT_CQL );
88+ }
89+
90+ protected @ NonNull Resource newCassandraSchemaCqlScriptResource () {
91+ return new ClassPathResource (CASSANDRA_SCHEMA_CQL );
92+ }
7693
7794 @ Bean
7895 SessionFactoryInitializer sessionFactoryInitializer (SessionFactory sessionFactory ) {
@@ -87,24 +104,12 @@ SessionFactoryInitializer sessionFactoryInitializer(SessionFactory sessionFactor
87104 return sessionFactoryInitializer ;
88105 }
89106
90- protected Resource newCassandraDataCqlScriptResource () {
91- return new ClassPathResource (CASSANDRA_DATA_CQL );
92- }
93-
94- protected Resource newCassandraInitCqlScriptResource () {
95- return new ClassPathResource (CASSANDRA_INIT_CQL );
96- }
97-
98- protected Resource newCassandraSchemaCqlScriptResource () {
99- return new ClassPathResource (CASSANDRA_SCHEMA_CQL );
100- }
101-
102- protected KeyspacePopulator newKeyspacePopulator (Resource ... cqlScripts ) {
107+ protected @ NonNull KeyspacePopulator newKeyspacePopulator (Resource ... cqlScripts ) {
103108 return new ResourceKeyspacePopulator (CONTINUE_ON_ERROR , IGNORE_FAILED_DROPS , CQL_SCRIPT_ENCODING , cqlScripts );
104- //return cqlScript -> {};
105109 }
106110
107111 @ Bean
112+ @ Profile (DEBUGGING_PROFILE )
108113 BeanPostProcessor cassandraTemplatePostProcessor () {
109114
110115 return new BeanPostProcessor () {
@@ -114,11 +119,12 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
114119
115120 if (bean instanceof CassandraTemplate cassandraTemplate ) {
116121
117- Consumer <CassandraTemplate > cassandraTemplateConsumer = noopCassandraTemplateConsumer ();
118- //.andThen(entityObjectInsertingCassandraTemplateConsumer())
119- //.andThen(entityObjectAssertingCassandraTemplateConsumer());
120- //.andThen(keyspaceNameAssertingCassandraTemplateConsumer())
121- //.andThen(tableNameAssertingCassandraTemplateConsumer());
122+ Consumer <CassandraTemplate > cassandraTemplateConsumer = noopCassandraTemplateConsumer ()
123+ .andThen (insertEntityObjectCassandraTemplateConsumer ())
124+ .andThen (assertEntityCountCassandraTemplateConsumer ())
125+ .andThen (assertEntityObjectCassandraTemplateConsumer ())
126+ .andThen (assertKeyspaceNameCassandraTemplateConsumer ())
127+ .andThen (assertTableNameCassandraTemplateConsumer ());
122128
123129 cassandraTemplateConsumer .accept (cassandraTemplate );
124130 }
@@ -132,35 +138,32 @@ private Consumer<CassandraTemplate> noopCassandraTemplateConsumer() {
132138 return cassandraTemplate -> {};
133139 }
134140
135- private Consumer <CassandraTemplate > entityCountAssertingCassandraTemplateConsumer () {
141+ private Consumer <CassandraTemplate > assertEntityCountCassandraTemplateConsumer () {
136142 return cassandraTemplate -> assertThat (cassandraTemplate .count (Customer .class )).isOne ();
137143 }
138144
139- private Consumer <CassandraTemplate > entityObjectAssertingCassandraTemplateConsumer () {
145+ private Consumer <CassandraTemplate > assertEntityObjectCassandraTemplateConsumer () {
140146
141147 return cassandraTemplate -> {
142148
143- String cql = "SELECT id, name FROM Customers" ;
149+ String cql = "SELECT id, name FROM \" Customers\" " ;
144150
145151 RowMapper <Customer > customerRowMapper = (row , rowNumber ) ->
146152 Customer .newCustomer (row .getLong ("id" ), row .getString ("name" ));
147153
148154 Customer actualCustomer = cassandraTemplate .getCqlOperations ().queryForObject (cql , customerRowMapper );
149- Customer expectedCustomer = Customer .newCustomer (16L , "Pie Doe" );
150155
151- assertThat (actualCustomer ).isEqualTo (expectedCustomer );
152- //assertThat(cassandraTemplate.selectOneById(16L, Customer.class)).isEqualTo(expectedCustomer);
153- //assertThat(cassandraTemplate.query(Customer.class).stream().findFirst().orElse(null))
154- // .isEqualTo(expectedCustomer);
156+ assertThat (actualCustomer ).isEqualTo (pieDoe );
157+ assertThat (cassandraTemplate .selectOneById (16L , Customer .class )).isEqualTo (pieDoe );
158+ assertThat (cassandraTemplate .query (Customer .class ).stream ().findFirst ().orElse (null )).isEqualTo (pieDoe );
155159 };
156160 }
157161
158- // TODO: Why does this work and the CQL data script not work!
159- private Consumer <CassandraTemplate > entityObjectInsertingCassandraTemplateConsumer () {
160- return cassandraTemplate -> cassandraTemplate .insert (Customer .newCustomer (16L , "Pie Doe" ));
162+ private Consumer <CassandraTemplate > insertEntityObjectCassandraTemplateConsumer () {
163+ return cassandraTemplate -> cassandraTemplate .insert (pieDoe );
161164 }
162165
163- private Consumer <CassandraTemplate > keyspaceNameAssertingCassandraTemplateConsumer () {
166+ private Consumer <CassandraTemplate > assertKeyspaceNameCassandraTemplateConsumer () {
164167
165168 return cassandraTemplate -> {
166169
@@ -177,7 +180,7 @@ private Consumer<CassandraTemplate> keyspaceNameAssertingCassandraTemplateConsum
177180 };
178181 }
179182
180- private Consumer <CassandraTemplate > tableNameAssertingCassandraTemplateConsumer () {
183+ private Consumer <CassandraTemplate > assertTableNameCassandraTemplateConsumer () {
181184
182185 return cassandraTemplate -> {
183186
@@ -198,7 +201,10 @@ private Consumer<CassandraTemplate> tableNameAssertingCassandraTemplateConsumer(
198201 }
199202
200203 @ Bean
201- ApplicationListener <ContextRefreshedEvent > populateCassandraDatabaseUsingRepository (CustomerRepository customerRepository ) {
202- return event -> customerRepository .save (Customer .newCustomer (16L , "Pie Doe" ));
204+ @ Profile (DEBUGGING_PROFILE )
205+ ApplicationListener <ContextRefreshedEvent > populateCassandraDatabaseUsingRepository (
206+ CustomerRepository customerRepository ) {
207+
208+ return event -> customerRepository .save (pieDoe );
203209 }
204210}
0 commit comments