Skip to content

Commit

Permalink
Remove PaginatedDbService usage from EncryptedValueTest (#21293)
Browse files Browse the repository at this point in the history
  • Loading branch information
thll authored Jan 9, 2025
1 parent ad934d3 commit 575b3a7
Showing 1 changed file with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.auto.value.AutoValue;
import com.mongodb.client.MongoCollection;
import org.graylog.grn.GRNRegistry;
import org.graylog.testing.mongodb.MongoDBExtension;
import org.graylog.testing.mongodb.MongoDBTestService;
import org.graylog2.bindings.providers.MongoJackObjectMapperProvider;
import org.graylog2.database.MongoConnection;
import org.graylog2.database.PaginatedDbService;
import org.graylog2.database.MongoCollections;
import org.graylog2.database.MongoEntity;
import org.graylog2.database.utils.MongoUtils;
import org.graylog2.jackson.InputConfigurationBeanDeserializerModifier;
import org.graylog2.shared.bindings.providers.ObjectMapperProvider;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -40,6 +42,7 @@
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.graylog2.database.utils.MongoUtils.insertedIdAsString;

@ExtendWith(MongoDBExtension.class)
class EncryptedValueTest {
Expand All @@ -58,7 +61,8 @@ void setUp(MongoDBTestService mongodb) {
InputConfigurationBeanDeserializerModifier.withoutConfig()
).get();

this.dbService = new TestService(mongodb.mongoConnection(), new MongoJackObjectMapperProvider(objectMapper));
this.dbService = new TestService(new MongoCollections(new MongoJackObjectMapperProvider(objectMapper),
mongodb.mongoConnection()));
}

@Test
Expand Down Expand Up @@ -154,7 +158,7 @@ void testWithDatabase() {
.isDeleteValue(false)
.build();

final String savedId = dbService.save(TestDTO.create(value)).id();
final String savedId = dbService.create(TestDTO.create(value));
final TestDTO dto = dbService.get(savedId).orElse(null);

assertThat(dto).isNotNull();
Expand All @@ -167,7 +171,7 @@ void testWithDatabase() {

@Test
void testUnsetWithDatabase() {
final String savedId = dbService.save(TestDTO.create(EncryptedValue.createUnset())).id();
final String savedId = dbService.create(TestDTO.create(EncryptedValue.createUnset()));
final TestDTO dto = dbService.get(savedId).orElse(null);

assertThat(dto).isNotNull();
Expand All @@ -178,19 +182,26 @@ void testUnsetWithDatabase() {
assertThat(dto.passwordValue().salt()).isEmpty();
}

static class TestService extends PaginatedDbService<TestDTO> {
@Override
static class TestService {
private final MongoCollection<TestDTO> collection;
private final MongoUtils<TestDTO> utils;

protected TestService(MongoCollections mongoCollections) {
collection = mongoCollections.collection("test_collection", TestDTO.class);
utils = mongoCollections.utils(collection);
}

public Optional<TestDTO> get(String id) {
return super.get(id);
return utils.getById(id);
}

protected TestService(MongoConnection mongoConnection, MongoJackObjectMapperProvider mapperProvider) {
super(mongoConnection, mapperProvider, TestDTO.class, "test_collection");
public String create(TestDTO testDTO) {
return insertedIdAsString(collection.insertOne(testDTO));
}
}

@AutoValue
static abstract class TestDTO {
static abstract class TestDTO implements MongoEntity {
@Id
@ObjectId
@Nullable
Expand Down

0 comments on commit 575b3a7

Please sign in to comment.