Skip to content

Commit 0b6c997

Browse files
committed
Polishing.
Reformat code, replace known unsupported constructor with UnsupportedOperationException. See #4432 Original pull request: #4439
1 parent edeb423 commit 0b6c997

File tree

5 files changed

+66
-127
lines changed

5 files changed

+66
-127
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/encryption/MongoEncryptionConverter.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.bson.BsonDocument;
2727
import org.bson.BsonValue;
2828
import org.bson.Document;
29-
import org.bson.conversions.Bson;
3029
import org.bson.types.Binary;
3130
import org.springframework.core.CollectionFactory;
3231
import org.springframework.data.mongodb.core.convert.MongoConversionContext;
@@ -96,7 +95,7 @@ public Object decrypt(Object encryptedValue, EncryptionContext context) {
9695
if (!persistentProperty.isEntity()) {
9796
Collection<Object> collection = CollectionFactory.createCollection(persistentProperty.getType(), size);
9897
iterable.forEach(it -> {
99-
if(it instanceof BsonValue bsonValue) {
98+
if (it instanceof BsonValue bsonValue) {
10099
collection.add(BsonUtils.toJavaType(bsonValue));
101100
} else {
102101
collection.add(context.read(it, persistentProperty.getActualType()));
@@ -107,7 +106,7 @@ public Object decrypt(Object encryptedValue, EncryptionContext context) {
107106
} else {
108107
Collection<Object> collection = CollectionFactory.createCollection(persistentProperty.getType(), size);
109108
iterable.forEach(it -> {
110-
if(it instanceof BsonValue bsonValue) {
109+
if (it instanceof BsonValue bsonValue) {
111110
collection.add(context.read(BsonUtils.toJavaType(bsonValue), persistentProperty.getActualType()));
112111
} else {
113112
collection.add(context.read(it, persistentProperty.getActualType()));
@@ -118,14 +117,14 @@ public Object decrypt(Object encryptedValue, EncryptionContext context) {
118117
}
119118

120119
if (!persistentProperty.isEntity() && persistentProperty.isMap()) {
121-
if(persistentProperty.getType() != Document.class) {
122-
if(decryptedValue instanceof BsonValue bsonValue) {
120+
if (persistentProperty.getType() != Document.class) {
121+
if (decryptedValue instanceof BsonValue bsonValue) {
123122
return new LinkedHashMap<>((Document) BsonUtils.toJavaType(bsonValue));
124123
}
125-
if(decryptedValue instanceof Document document) {
124+
if (decryptedValue instanceof Document document) {
126125
return new LinkedHashMap<>(document);
127126
}
128-
if(decryptedValue instanceof Map map) {
127+
if (decryptedValue instanceof Map map) {
129128
return map;
130129
}
131130
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/BsonUtils.java

+25-38
Original file line numberDiff line numberDiff line change
@@ -286,36 +286,22 @@ static void removeFrom(Bson bson, String key) {
286286
*/
287287
public static Object toJavaType(BsonValue value) {
288288

289-
switch (value.getBsonType()) {
290-
case INT32:
291-
return value.asInt32().getValue();
292-
case INT64:
293-
return value.asInt64().getValue();
294-
case STRING:
295-
return value.asString().getValue();
296-
case DECIMAL128:
297-
return value.asDecimal128().doubleValue();
298-
case DOUBLE:
299-
return value.asDouble().getValue();
300-
case BOOLEAN:
301-
return value.asBoolean().getValue();
302-
case OBJECT_ID:
303-
return value.asObjectId().getValue();
304-
case DB_POINTER:
305-
return new DBRef(value.asDBPointer().getNamespace(), value.asDBPointer().getId());
306-
case BINARY:
307-
return value.asBinary().getData();
308-
case DATE_TIME:
309-
return new Date(value.asDateTime().getValue());
310-
case SYMBOL:
311-
return value.asSymbol().getSymbol();
312-
case ARRAY:
313-
return value.asArray().toArray();
314-
case DOCUMENT:
315-
return Document.parse(value.asDocument().toJson());
316-
default:
317-
return value;
318-
}
289+
return switch (value.getBsonType()) {
290+
case INT32 -> value.asInt32().getValue();
291+
case INT64 -> value.asInt64().getValue();
292+
case STRING -> value.asString().getValue();
293+
case DECIMAL128 -> value.asDecimal128().doubleValue();
294+
case DOUBLE -> value.asDouble().getValue();
295+
case BOOLEAN -> value.asBoolean().getValue();
296+
case OBJECT_ID -> value.asObjectId().getValue();
297+
case DB_POINTER -> new DBRef(value.asDBPointer().getNamespace(), value.asDBPointer().getId());
298+
case BINARY -> value.asBinary().getData();
299+
case DATE_TIME -> new Date(value.asDateTime().getValue());
300+
case SYMBOL -> value.asSymbol().getSymbol();
301+
case ARRAY -> value.asArray().toArray();
302+
case DOCUMENT -> Document.parse(value.asDocument().toJson());
303+
default -> value;
304+
};
319305
}
320306

321307
/**
@@ -364,26 +350,26 @@ public static BsonValue simpleToBsonValue(Object source) {
364350
return new BsonDouble(floatValue);
365351
}
366352

367-
if(source instanceof Binary binary) {
353+
if (source instanceof Binary binary) {
368354
return new BsonBinary(binary.getType(), binary.getData());
369355
}
370356

371-
if(source instanceof Temporal) {
357+
if (source instanceof Temporal) {
372358
if (source instanceof Instant value) {
373359
return new BsonDateTime(value.toEpochMilli());
374360
}
375361
if (source instanceof LocalDateTime value) {
376362
return new BsonDateTime(value.toInstant(ZoneOffset.UTC).toEpochMilli());
377363
}
378-
if(source instanceof LocalDate value) {
364+
if (source instanceof LocalDate value) {
379365
return new BsonDateTime(value.atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli());
380366
}
381-
if(source instanceof LocalTime value) {
367+
if (source instanceof LocalTime value) {
382368
return new BsonDateTime(value.atDate(LocalDate.ofEpochDay(0L)).toInstant(ZoneOffset.UTC).toEpochMilli());
383369
}
384370
}
385371

386-
if(source instanceof Date date) {
372+
if (source instanceof Date date) {
387373
new BsonDateTime(date.getTime());
388374
}
389375

@@ -393,7 +379,7 @@ public static BsonValue simpleToBsonValue(Object source) {
393379

394380
/**
395381
* Merge the given {@link Document documents} into on in the given order. Keys contained within multiple documents are
396-
* overwritten by their follow ups.
382+
* overwritten by their follow-ups.
397383
*
398384
* @param documents must not be {@literal null}. Can be empty.
399385
* @return the document containing all key value pairs.
@@ -694,7 +680,7 @@ private static String toJson(@Nullable Object value) {
694680

695681
if (value instanceof Collection<?> collection) {
696682
return toString(collection);
697-
} else if (value instanceof Map<?,?> map) {
683+
} else if (value instanceof Map<?, ?> map) {
698684
return toString(map);
699685
} else if (ObjectUtils.isArray(value)) {
700686
return toString(Arrays.asList(ObjectUtils.toObjectArray(value)));
@@ -716,8 +702,9 @@ private static String serializeValue(@Nullable Object value) {
716702

717703
private static String toString(Map<?, ?> source) {
718704

705+
// Avoid String.format for performance
719706
return iterableToDelimitedString(source.entrySet(), "{ ", " }",
720-
entry -> String.format("\"%s\" : %s", entry.getKey(), toJson(entry.getValue())));
707+
entry -> "\"" + entry.getKey() + "\" : " + toJson(entry.getValue()));
721708
}
722709

723710
private static String toString(Collection<?> source) {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/encryption/AbstractEncryptionTestBase.java

+19-28
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.time.Month;
2626
import java.util.Arrays;
2727
import java.util.Collections;
28-
import java.util.HashMap;
2928
import java.util.List;
3029
import java.util.Map;
3130
import java.util.Objects;
@@ -34,14 +33,6 @@
3433
import java.util.function.Function;
3534
import java.util.function.Supplier;
3635

37-
import com.mongodb.ClientEncryptionSettings;
38-
import com.mongodb.ConnectionString;
39-
import com.mongodb.MongoClientSettings;
40-
import com.mongodb.client.MongoCollection;
41-
import com.mongodb.client.model.Filters;
42-
import com.mongodb.client.model.IndexOptions;
43-
import com.mongodb.client.model.Indexes;
44-
import com.mongodb.client.vault.ClientEncryptions;
4536
import org.assertj.core.api.Assertions;
4637
import org.bson.BsonBinary;
4738
import org.bson.Document;
@@ -61,13 +52,21 @@
6152
import org.springframework.data.mongodb.core.convert.encryption.MongoEncryptionConverter;
6253
import org.springframework.data.mongodb.core.mapping.ExplicitEncrypted;
6354
import org.springframework.data.mongodb.core.query.Update;
55+
import org.springframework.data.util.Lazy;
6456

57+
import com.mongodb.ClientEncryptionSettings;
58+
import com.mongodb.ConnectionString;
59+
import com.mongodb.MongoClientSettings;
6560
import com.mongodb.MongoNamespace;
6661
import com.mongodb.client.MongoClient;
6762
import com.mongodb.client.MongoClients;
63+
import com.mongodb.client.MongoCollection;
64+
import com.mongodb.client.model.Filters;
65+
import com.mongodb.client.model.IndexOptions;
66+
import com.mongodb.client.model.Indexes;
6867
import com.mongodb.client.model.vault.DataKeyOptions;
6968
import com.mongodb.client.vault.ClientEncryption;
70-
import org.springframework.data.util.Lazy;
69+
import com.mongodb.client.vault.ClientEncryptions;
7170

7271
/**
7372
* @author Christoph Strobl
@@ -87,7 +86,7 @@ void encryptAndDecryptSimpleValue() {
8786

8887
verifyThat(source) //
8988
.identifiedBy(Person::getId) //
90-
.wasSavedMatching(it -> assertThat(it.get("ssn")).isInstanceOf(Binary.class)) //
89+
.wasSavedMatching(it -> assertThat(it.get("ssn")).isInstanceOf(Binary.class)) //
9190
.loadedIsEqualToSource();
9291
}
9392

@@ -119,7 +118,7 @@ void encryptAndDecryptComplexValue() {
119118

120119
verifyThat(source) //
121120
.identifiedBy(Person::getId) //
122-
.wasSavedMatching(it -> assertThat(it.get("address")).isInstanceOf(Binary.class)) //
121+
.wasSavedMatching(it -> assertThat(it.get("address")).isInstanceOf(Binary.class)) //
123122
.loadedIsEqualToSource();
124123
}
125124

@@ -483,24 +482,17 @@ ClientEncryptionSettings encryptionSettings(MongoClient mongoClient) {
483482
MongoCollection<Document> collection = mongoClient.getDatabase(getDatabaseName()).getCollection("test");
484483
collection.drop(); // Clear old data
485484

486-
final byte[] localMasterKey = new byte[96];
485+
byte[] localMasterKey = new byte[96];
487486
new SecureRandom().nextBytes(localMasterKey);
488-
Map<String, Map<String, Object>> kmsProviders = new HashMap<>() {
489-
{
490-
put("local", new HashMap<>() {
491-
{
492-
put("key", localMasterKey);
493-
}
494-
});
495-
}
496-
};
487+
Map<String, Map<String, Object>> kmsProviders = Map.of("local", Map.of("key", localMasterKey));
497488

498489
// Create the ClientEncryption instance
499-
ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder()
490+
return ClientEncryptionSettings.builder() //
500491
.keyVaultMongoClientSettings(
501-
MongoClientSettings.builder().applyConnectionString(new ConnectionString("mongodb://localhost")).build())
502-
.keyVaultNamespace(keyVaultNamespace.getFullName()).kmsProviders(kmsProviders).build();
503-
return clientEncryptionSettings;
492+
MongoClientSettings.builder().applyConnectionString(new ConnectionString("mongodb://localhost")).build()) //
493+
.keyVaultNamespace(keyVaultNamespace.getFullName()) //
494+
.kmsProviders(kmsProviders) //
495+
.build();
504496
}
505497
}
506498

@@ -693,8 +685,7 @@ public String toString() {
693685
+ ", wallet=" + this.getWallet() + ", address=" + this.getAddress() + ", encryptedZip="
694686
+ this.getEncryptedZip() + ", listOfString=" + this.getListOfString() + ", listOfComplex="
695687
+ this.getListOfComplex() + ", viaAltKeyNameField=" + this.getViaAltKeyNameField() + ", mapOfString="
696-
+ this.getMapOfString() + ", mapOfComplex=" + this.getMapOfComplex()
697-
+ ", today=" + this.getToday() + ")";
688+
+ this.getMapOfString() + ", mapOfComplex=" + this.getMapOfComplex() + ", today=" + this.getToday() + ")";
698689
}
699690
}
700691

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/encryption/BypassAutoEncryptionTest.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ protected void configureClientSettings(Builder builder) {
6868
ClientEncryptionSettings clientEncryptionSettings = encryptionSettings(mongoClient);
6969
mongoClient.close();
7070

71-
builder.autoEncryptionSettings(
72-
AutoEncryptionSettings.builder().kmsProviders(clientEncryptionSettings.getKmsProviders())
73-
.keyVaultNamespace(clientEncryptionSettings.getKeyVaultNamespace()).bypassAutoEncryption(true).build());
71+
builder.autoEncryptionSettings(AutoEncryptionSettings.builder() //
72+
.kmsProviders(clientEncryptionSettings.getKmsProviders()) //
73+
.keyVaultNamespace(clientEncryptionSettings.getKeyVaultNamespace()) //
74+
.bypassAutoEncryption(true).build());
7475
}
7576

7677
@Override
@@ -81,6 +82,7 @@ protected void configureConverters(MongoConverterConfigurationAdapter converterC
8182
}
8283

8384
@Bean
85+
@Override
8486
MongoEncryptionConverter encryptingConverter(MongoClientEncryption mongoClientEncryption) {
8587

8688
Lazy<BsonBinary> dataKey = Lazy.of(() -> mongoClientEncryption.getClientEncryption().createDataKey("local",
@@ -91,6 +93,7 @@ MongoEncryptionConverter encryptingConverter(MongoClientEncryption mongoClientEn
9193
}
9294

9395
@Bean
96+
@Override
9497
CachingMongoClientEncryption clientEncryption(ClientEncryptionSettings encryptionSettings) {
9598
return new CachingMongoClientEncryption(() -> ClientEncryptions.create(encryptionSettings));
9699
}

0 commit comments

Comments
 (0)