committed by
Christoph Strobl
parent
ddad793b88
commit
e39a10d808
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2019 the original author or authors.
|
||||
*
|
||||
* 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 example.springdata.mongodb.immutable;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.mongodb.core.mapping.event.BeforeConvertCallback;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
|
||||
/**
|
||||
* Test configuration to connect to a MongoDB named "test" and using a {@link MongoClient}. Also enables Spring Data
|
||||
* repositories for MongoDB.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@SpringBootApplication
|
||||
class ApplicationConfiguration {
|
||||
|
||||
/**
|
||||
* Callback to update {@link ImmutablePerson}.
|
||||
*
|
||||
* @return a {@link BeforeConvertCallback} for {@link ImmutablePerson}.
|
||||
*/
|
||||
@Bean
|
||||
BeforeConvertCallback<ImmutablePerson> beforeConvertCallback() {
|
||||
|
||||
return (immutablePerson, collection) -> {
|
||||
|
||||
int randomNumber = ThreadLocalRandom.current().nextInt(1, 100);
|
||||
|
||||
return immutablePerson.withRandomNumber(randomNumber);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2019 the original author or authors.
|
||||
*
|
||||
* 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 example.springdata.mongodb.immutable;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.experimental.Wither;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
/**
|
||||
* Immutable object.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@Wither
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class ImmutablePerson {
|
||||
|
||||
private final ObjectId id;
|
||||
|
||||
private final int randomNumber;
|
||||
|
||||
public ImmutablePerson() {
|
||||
this(null, 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2019 the original author or authors.
|
||||
*
|
||||
* 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 example.springdata.mongodb.immutable;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* Integration test for {@link ImmutablePerson} showing features around immutable object support.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class ImmutableEntityIntegrationTest {
|
||||
|
||||
@Autowired MongoOperations operations;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
operations.remove(ImmutablePerson.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case to show that automatically generated ids are assigned to the immutable domain object and how the
|
||||
* {@link ImmutablePerson#getRandomNumber()} gets set via {@link ApplicationConfiguration#beforeConvertCallback()}.
|
||||
*/
|
||||
@Test
|
||||
public void setsRandomNumberOnSave() {
|
||||
|
||||
ImmutablePerson unsaved = new ImmutablePerson();
|
||||
assertThat(unsaved.getRandomNumber()).isZero();
|
||||
|
||||
ImmutablePerson saved = operations.save(unsaved);
|
||||
|
||||
assertThat(saved.getId()).isNotNull();
|
||||
assertThat(saved.getRandomNumber()).isNotZero();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user