Skip to content

Commit

Permalink
Use secure XStreamSerializer instance
Browse files Browse the repository at this point in the history
Whilst touching the MongoTokenStoreTest, a secure XStreamSerializer
should be added. Use this in the token store test, as well as in other
test cases.

#49
  • Loading branch information
smcvb committed Oct 19, 2021
1 parent d7453e6 commit 6f4893c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2020. Axon Framework
* Copyright (c) 2010-2021. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,12 +19,12 @@
import com.mongodb.client.FindIterable;
import org.axonframework.extensions.mongo.MongoTemplate;
import org.axonframework.extensions.mongo.util.MongoTemplateFactory;
import org.axonframework.extensions.mongo.utils.TestSerializer;
import org.axonframework.modelling.saga.AssociationValue;
import org.axonframework.modelling.saga.AssociationValues;
import org.axonframework.modelling.saga.AssociationValuesImpl;
import org.axonframework.modelling.saga.repository.SagaStore;
import org.axonframework.serialization.Serializer;
import org.axonframework.serialization.xml.XStreamSerializer;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.junit.jupiter.api.*;
Expand Down Expand Up @@ -62,6 +62,7 @@ void setUp() {
mongoTemplate.sagaCollection().drop();
testSubject = MongoSagaStore.builder()
.mongoTemplate(mongoTemplate)
.serializer(TestSerializer.xStreamSerializer())
.build();
}

Expand Down Expand Up @@ -201,7 +202,7 @@ void testLoadSaga_AssociationValueRemoved() {
MyTestSaga saga = new MyTestSaga();
AssociationValue associationValue = new AssociationValue("key", "value");
SagaEntry<MyTestSaga> testSagaEntry = new SagaEntry<>(
identifier, saga, singleton(associationValue), XStreamSerializer.defaultSerializer()
identifier, saga, singleton(associationValue), TestSerializer.xStreamSerializer()
);
mongoTemplate.sagaCollection().insertOne(testSagaEntry.asDocument());

Expand All @@ -217,7 +218,7 @@ identifier, saga, singleton(associationValue), XStreamSerializer.defaultSerializ
void testSaveSaga() {
String identifier = UUID.randomUUID().toString();
MyTestSaga saga = new MyTestSaga();
Serializer serializer = XStreamSerializer.defaultSerializer();
Serializer serializer = TestSerializer.xStreamSerializer();
mongoTemplate.sagaCollection()
.insertOne(new SagaEntry<>(identifier, saga, emptySet(), serializer).asDocument());
SagaStore.Entry<MyTestSaga> loaded = testSubject.loadSaga(MyTestSaga.class, identifier);
Expand All @@ -243,7 +244,7 @@ void testEndSaga() {
MyTestSaga saga = new MyTestSaga();
AssociationValue associationValue = new AssociationValue("key", "value");
SagaEntry<MyTestSaga> testSagaEntry = new SagaEntry<>(
identifier, saga, singleton(associationValue), XStreamSerializer.defaultSerializer()
identifier, saga, singleton(associationValue), TestSerializer.xStreamSerializer()
);
mongoTemplate.sagaCollection().insertOne(testSagaEntry.asDocument());
testSubject.deleteSaga(MyTestSaga.class, identifier, singleton(associationValue));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2020. Axon Framework
* Copyright (c) 2010-2021. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,7 @@
import org.axonframework.eventhandling.TrackingToken;
import org.axonframework.extensions.mongo.MongoTemplate;
import org.axonframework.extensions.mongo.util.MongoTemplateFactory;
import org.axonframework.extensions.mongo.utils.TestSerializer;
import org.junit.jupiter.api.*;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
Expand Down Expand Up @@ -114,8 +115,11 @@ public void testCreateTokenAtTimeBeforeFirstEvent() {

@Override
protected MongoEventStorageEngine createEngine(UnaryOperator<MongoEventStorageEngine.Builder> customization) {
MongoEventStorageEngine.Builder engineBuilder = MongoEventStorageEngine.builder()
.mongoTemplate(mongoTemplate);
MongoEventStorageEngine.Builder engineBuilder =
MongoEventStorageEngine.builder()
.snapshotSerializer(TestSerializer.xStreamSerializer())
.eventSerializer(TestSerializer.xStreamSerializer())
.mongoTemplate(mongoTemplate);
return customization.apply(engineBuilder).build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2020. Axon Framework
* Copyright (c) 2010-2021. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,18 +20,17 @@
import com.mongodb.client.MongoCollection;
import org.axonframework.extensions.mongo.MongoTemplate;
import org.axonframework.extensions.mongo.util.MongoTemplateFactory;
import org.axonframework.extensions.mongo.utils.TestSerializer;
import org.axonframework.serialization.Serializer;
import org.axonframework.serialization.xml.XStreamSerializer;
import org.bson.Document;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.time.Duration;
import java.time.temporal.TemporalAmount;

import static org.junit.jupiter.api.Assertions.*;

/**
Expand All @@ -57,7 +56,7 @@ void setUp() {
trackingTokensCollection = mongoTemplate.trackingTokensCollection();
trackingTokensCollection.drop();
String testOwner = "testOwner";
Serializer serializer = XStreamSerializer.defaultSerializer();
Serializer serializer = TestSerializer.xStreamSerializer();
MongoTokenStore.Builder tokenStoreBuilder = MongoTokenStore.builder()
.mongoTemplate(mongoTemplate)
.serializer(serializer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.axonframework.eventhandling.tokenstore.UnableToInitializeTokenException;
import org.axonframework.extensions.mongo.MongoTemplate;
import org.axonframework.extensions.mongo.util.MongoTemplateFactory;
import org.axonframework.extensions.mongo.utils.TestSerializer;
import org.axonframework.serialization.Serializer;
import org.axonframework.serialization.json.JacksonSerializer;
import org.axonframework.serialization.xml.XStreamSerializer;
Expand Down Expand Up @@ -94,7 +95,7 @@ void setUp() {
trackingTokensCollection = mongoTemplate.trackingTokensCollection();
trackingTokensCollection.drop();

serializer = XStreamSerializer.defaultSerializer();
serializer = TestSerializer.xStreamSerializer();
MongoTokenStore.Builder tokenStoreBuilder = MongoTokenStore.builder()
.mongoTemplate(mongoTemplate)
.serializer(serializer)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2010-2021. Axon Framework
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.axonframework.extensions.mongo.utils;

import com.thoughtworks.xstream.XStream;
import org.axonframework.serialization.xml.CompactDriver;
import org.axonframework.serialization.xml.XStreamSerializer;

/**
* Utility providing {@link org.axonframework.serialization.Serializer} instances for testing.
*
* @author Steven van Beelen
*/
public abstract class TestSerializer {

private TestSerializer() {
// Test utility class
}

/**
* Return a {@link XStreamSerializer} using a default {@link XStream} instance with a {@link CompactDriver}.
*
* @return a {@link XStreamSerializer} using a default {@link XStream} instance with a {@link CompactDriver}
*/
public static XStreamSerializer xStreamSerializer() {
return XStreamSerializer.builder()
.xStream(new XStream(new CompactDriver()))
.build();
}
}

0 comments on commit 6f4893c

Please sign in to comment.