Skip to content

Commit 3288da2

Browse files
authored
springboot 2.4 (#53)
1 parent 0af5599 commit 3288da2

File tree

11 files changed

+50
-96
lines changed

11 files changed

+50
-96
lines changed

geo-data-server/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,22 @@
114114
<artifactId>junit</artifactId>
115115
<scope>test</scope>
116116
</dependency>
117+
<dependency>
118+
<groupId>org.junit.vintage</groupId>
119+
<artifactId>junit-vintage-engine</artifactId>
120+
<scope>test</scope>
121+
</dependency>
117122
<dependency>
118123
<groupId>org.springframework.boot</groupId>
119124
<artifactId>spring-boot-starter-test</artifactId>
120125
<scope>test</scope>
126+
<!-- cassandra driver 4.0 already brings an org.json and vaadin json is only a subset of org.json -->
127+
<exclusions>
128+
<exclusion>
129+
<groupId>com.vaadin.external.google</groupId>
130+
<artifactId>android-json</artifactId>
131+
</exclusion>
132+
</exclusions>
121133
</dependency>
122134
<dependency>
123135
<groupId>org.mockito</groupId>

geo-data-server/src/main/java/org/gridsuite/geodata/server/CassandraConfig.java

Lines changed: 16 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,19 @@
66
*/
77
package org.gridsuite.geodata.server;
88

9-
import com.datastax.driver.core.*;
10-
import com.powsybl.commons.PowsyblException;
11-
import org.gridsuite.geodata.extensions.Coordinate;
9+
import com.datastax.oss.driver.api.core.CqlSession;
1210
import org.gridsuite.geodata.server.repositories.LineRepository;
1311
import org.springframework.beans.factory.annotation.Value;
1412
import org.springframework.context.annotation.Bean;
1513
import org.springframework.context.annotation.Configuration;
1614
import org.springframework.context.annotation.PropertySource;
1715
import org.springframework.core.env.Environment;
1816
import org.springframework.data.cassandra.config.AbstractCassandraConfiguration;
19-
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
20-
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
21-
import org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver;
17+
import org.springframework.data.cassandra.config.CqlSessionFactoryBean;
18+
import org.springframework.data.cassandra.config.SessionFactoryFactoryBean;
19+
import org.springframework.data.cassandra.core.convert.CassandraConverter;
2220
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
2321

24-
import java.nio.ByteBuffer;
25-
2622
/**
2723
* @author Chamseddine Benhamed <chamseddine.benhamed at rte-france.com>
2824
*/
@@ -41,78 +37,20 @@ protected String getKeyspaceName() {
4137
}
4238

4339
@Bean
44-
public CassandraClusterFactoryBean cluster(Environment env) {
45-
CassandraClusterFactoryBean clusterFactory = new CassandraClusterFactoryBean();
46-
clusterFactory.setContactPoints(env.getRequiredProperty("cassandra.contact-points"));
47-
clusterFactory.setPort(Integer.parseInt(env.getRequiredProperty("cassandra.port")));
48-
49-
CodecRegistry codecRegistry = new CodecRegistry();
50-
clusterFactory.setClusterBuilderConfigurer(builder -> {
51-
builder.withCodecRegistry(codecRegistry);
52-
Cluster cluster = builder.build();
53-
KeyspaceMetadata keyspace = cluster.getMetadata().getKeyspace(getKeyspaceName());
54-
if (keyspace == null) {
55-
throw new PowsyblException("Keyspace '" + getKeyspaceName() + "' not found");
56-
}
57-
var coordinateType = keyspace.getUserType("coordinate");
58-
TypeCodec<UDTValue> coordinateTypeCodec = codecRegistry.codecFor(coordinateType);
59-
var coordinateCodec = new CoordinateCodec(coordinateTypeCodec, Coordinate.class);
60-
codecRegistry.register(coordinateCodec);
61-
return builder;
62-
});
63-
return clusterFactory;
40+
public CqlSessionFactoryBean cassandraSession(Environment env) {
41+
var session = new CqlSessionFactoryBean();
42+
session.setContactPoints(env.getRequiredProperty("cassandra.contact-points"));
43+
session.setPort(Integer.parseInt(env.getRequiredProperty("cassandra.port")));
44+
session.setLocalDatacenter(env.getRequiredProperty("cassandra.datacenter"));
45+
session.setKeyspaceName(getKeyspaceName());
46+
return session;
6447
}
6548

6649
@Bean
67-
public CassandraMappingContext cassandraMapping(Cluster cluster, Environment env) {
68-
CassandraMappingContext mappingContext = new CassandraMappingContext();
69-
mappingContext.setUserTypeResolver(new SimpleUserTypeResolver(cluster, getKeyspaceName()));
70-
return mappingContext;
71-
}
72-
73-
static class CoordinateCodec extends TypeCodec<Coordinate> {
74-
75-
private final TypeCodec<UDTValue> innerCodec;
76-
77-
private final UserType userType;
78-
79-
public CoordinateCodec(TypeCodec<UDTValue> innerCodec, Class<Coordinate> javaType) {
80-
super(innerCodec.getCqlType(), javaType);
81-
this.innerCodec = innerCodec;
82-
this.userType = (UserType) innerCodec.getCqlType();
83-
}
84-
85-
@Override
86-
public ByteBuffer serialize(Coordinate value, ProtocolVersion protocolVersion) {
87-
return innerCodec.serialize(toUDTValue(value), protocolVersion);
88-
}
89-
90-
@Override
91-
public Coordinate deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) {
92-
return toCoordinate(innerCodec.deserialize(bytes, protocolVersion));
93-
}
94-
95-
@Override
96-
public Coordinate parse(String value) {
97-
return value == null || value.isEmpty() ? null : toCoordinate(innerCodec.parse(value));
98-
}
99-
100-
@Override
101-
public String format(Coordinate value) {
102-
return value == null ? null : innerCodec.format(toUDTValue(value));
103-
}
104-
105-
protected Coordinate toCoordinate(UDTValue value) {
106-
return value == null ? null : new Coordinate(
107-
value.getDouble("lat"),
108-
value.getDouble("lon")
109-
);
110-
}
111-
112-
protected UDTValue toUDTValue(Coordinate value) {
113-
return value == null ? null : userType.newValue()
114-
.setDouble("lat", value.getLat())
115-
.setDouble("lon", value.getLon());
116-
}
50+
public SessionFactoryFactoryBean cassandraSessionFactory(CqlSession session, CassandraConverter converter) {
51+
var sessionFactory = new SessionFactoryFactoryBean();
52+
sessionFactory.setSession(session);
53+
sessionFactory.setConverter(converter);
54+
return sessionFactory;
11755
}
11856
}

geo-data-server/src/main/java/org/gridsuite/geodata/server/repositories/CoordinateEntity.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
*/
77
package org.gridsuite.geodata.server.repositories;
88

9+
import lombok.*;
910
import org.gridsuite.geodata.extensions.Coordinate;
10-
import lombok.AllArgsConstructor;
11-
import lombok.Builder;
12-
import lombok.Getter;
13-
import lombok.ToString;
1411
import org.springframework.data.cassandra.core.mapping.UserDefinedType;
1512

1613
import java.util.List;
@@ -21,6 +18,7 @@
2118
*/
2219
@UserDefinedType("coordinate")
2320
@AllArgsConstructor
21+
@NoArgsConstructor
2422
@Getter
2523
@Builder
2624
@ToString

geo-data-server/src/main/java/org/gridsuite/geodata/server/repositories/LineCustomRepository.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
*/
77
package org.gridsuite.geodata.server.repositories;
88

9-
import com.datastax.driver.core.ResultSet;
10-
import com.datastax.driver.core.Row;
11-
import com.datastax.driver.core.Session;
9+
import com.datastax.oss.driver.api.core.CqlSession;
10+
import com.datastax.oss.driver.api.core.cql.ResultSet;
11+
import com.datastax.oss.driver.api.core.cql.Row;
12+
import com.datastax.oss.driver.api.core.data.UdtValue;
1213
import org.gridsuite.geodata.extensions.Coordinate;
1314
import org.gridsuite.geodata.server.dto.LineGeoData;
1415
import com.powsybl.iidm.network.Country;
@@ -27,16 +28,17 @@
2728
public class LineCustomRepository {
2829

2930
@Autowired
30-
private Session session;
31+
private CqlSession session;
3132

3233
private static LineGeoData rowToLineGeoData(Row row) {
3334
String id = row.getString("id");
34-
boolean side1 = row.getBool("side1");
35+
boolean side1 = row.getBoolean("side1");
3536
Country country = Country.valueOf(row.getString("country"));
3637
Country otherCountry = Country.valueOf(row.getString("otherCountry"));
3738
String substationStart = row.getString("substationStart");
3839
String substationEnd = row.getString("substationEnd");
39-
List<Coordinate> coordinates = row.getList("coordinates", Coordinate.class);
40+
List<Coordinate> coordinates = row.getList("coordinates", UdtValue.class).stream().map(udtValue ->
41+
new Coordinate(udtValue.getDouble("lat"), udtValue.getDouble("lon"))).collect(Collectors.toList());
4042
return LineGeoData.builder()
4143
.id(id)
4244
.country1(side1 ? country : otherCountry)

geo-data-server/src/main/java/org/gridsuite/geodata/server/repositories/LineEntity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
import org.gridsuite.geodata.server.dto.LineGeoData;
1010
import lombok.*;
1111
import org.springframework.data.cassandra.core.cql.PrimaryKeyType;
12-
import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn;
13-
import org.springframework.data.cassandra.core.mapping.Table;
12+
import org.springframework.data.cassandra.core.mapping.*;
1413

1514
import java.util.List;
1615

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
cassandra.contact-points=localhost
22
cassandra.port=9042
3+
cassandra.datacenter=datacenter1

geo-data-server/src/test/java/org/gridsuite/geodata/server/AbstractEmbeddedCassandraSetup.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
package org.gridsuite.geodata.server;
88

9-
import com.github.nosan.embedded.cassandra.api.connection.ClusterCassandraConnection;
9+
import com.github.nosan.embedded.cassandra.api.connection.CqlSessionCassandraConnection;
1010
import com.github.nosan.embedded.cassandra.api.cql.CqlDataSet;
1111
import org.gridsuite.geodata.test.EmbeddedCassandraFactoryConfig;
1212
import org.junit.Before;
@@ -27,11 +27,11 @@
2727
public abstract class AbstractEmbeddedCassandraSetup {
2828

2929
@Autowired
30-
private ClusterCassandraConnection clusterCassandraConnection;
30+
private CqlSessionCassandraConnection cqlSessionCassandraConnection;
3131

3232
@Before
3333
public void setup() {
34-
CqlDataSet.ofClasspaths("truncate.cql").forEachStatement(clusterCassandraConnection::execute);
34+
CqlDataSet.ofClasspaths("truncate.cql").forEachStatement(cqlSessionCassandraConnection::execute);
3535
}
3636

3737
}

geo-data-server/src/test/java/org/gridsuite/geodata/server/GeoDataControllerTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
package org.gridsuite.geodata.server;
88

9+
import com.datastax.oss.driver.api.core.CqlSession;
910
import com.fasterxml.jackson.databind.ObjectMapper;
1011
import com.powsybl.iidm.network.Country;
1112
import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory;
@@ -55,6 +56,9 @@ public class GeoDataControllerTest {
5556
@MockBean
5657
private CassandraConfig cassandraConfig;
5758

59+
@MockBean
60+
private CqlSession cqlSession;
61+
5862
@MockBean
5963
private NetworkStoreService service;
6064

geo-data-server/src/test/java/org/gridsuite/geodata/server/repositories/LineRepositoryTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public void test() {
4747
assertEquals("LineEntity.LineEntityBuilder(country=FR, id=lineID, side1=false, otherCountry=BE, substationStart$value=sub, substationEnd$value=way, coordinates=[CoordinateEntity(lat=11.0, lon=12.0), CoordinateEntity(lat=13.0, lon=14.1)])", lineEntityBuilder.toString());
4848

4949
repository.save(lineEntityBuilder.build());
50-
5150
List<LineEntity> lines = repository.findAll();
5251

5352
assertEquals(1, lines.size());
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
cassandra.contact-points=localhost
22
cassandra.port=9142
3+
cassandra.datacenter=datacenter1

0 commit comments

Comments
 (0)