Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master #172

Merged
merged 6 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spring-data-mock-build/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<groupId>com.mmnaseri.utils</groupId>
<artifactId>spring-data-mock-build</artifactId>
<version>2.0.0</version>
<version>2.1.0</version>
<packaging>pom</packaging>

<name>Spring Data Mock: Build Aggregator</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mock-sample-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<spring-data-commons.version>2.2.6.RELEASE</spring-data-commons.version>
<spring-data-jpa.version>2.2.6.RELEASE</spring-data-jpa.version>
<persistence-api.version>1.0.2</persistence-api.version>
<spring-data-mock.version>2.0.0</spring-data-mock.version>
<spring-data-mock.version>2.1.0</spring-data-mock.version>
<testng.version>7.1.0</testng.version>
<hamcrest.version>1.3</hamcrest.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mock-sample-mongo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-data-commons.version>2.2.6.RELEASE</spring-data-commons.version>
<spring-data-mongo.version>2.2.6.RELEASE</spring-data-mongo.version>
<spring-data-mock.version>2.0.0</spring-data-mock.version>
<spring-data-mock.version>2.1.0</spring-data-mock.version>
<testng.version>7.1.0</testng.version>
<hamcrest.version>1.3</hamcrest.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mock/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

<groupId>com.mmnaseri.utils</groupId>
<artifactId>spring-data-mock</artifactId>
<version>2.0.0</version>
<version>2.1.0</version>

<name>Spring Data Mock</name>
<description>A framework for mocking Spring Data repositories</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,6 @@ public List<TypeMapping<?>> getMappings(Class<?> repositoryType) {
final Object instance;
try {
instance = implementation.getDeclaredConstructor().newInstance();
} catch (InstantiationException e) {
log.error("Failed to instantiate class " + implementation
+ " because there was an error in the constructor");
throw new RepositoryDefinitionException(repositoryType,
"Failed to instantiate an object of type " + implementation, e);
} catch (IllegalAccessException e) {
log.error("The constructor for the implementation class is not accessible: " + implementation);
throw new RepositoryDefinitionException(repositoryType,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.mmnaseri.utils.spring.data.domain.impl.key;

import org.bson.types.ObjectId;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.HashSet;
import java.util.Set;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public class BsonObjectIdKeyGeneratorTest {

private BsonObjectIdKeyGenerator generator;

@BeforeMethod
public void setUp() {
generator = new BsonObjectIdKeyGenerator();
}

@Test
public void testNotNullValue() {
assertThat(generator.generate(), is(notNullValue()));
}

@Test
public void testUniqueness() {
final Set<ObjectId> set = new HashSet<>();
for (int i = 0; i < 1000; i++) {
final ObjectId id = generator.generate();
assertThat(set, not(contains(id)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.hamcrest.Matchers;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.concurrent.atomic.AtomicLong;

import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -58,10 +59,6 @@ public void testPerformingUpdates() {
assertThat(dataStore.getRequests().get(0).getKey(), Matchers.is(entity.getId()));
}

/**
* This needs to have the root cause logged. See #29
*
*/
@Test(expectedExceptions = DataStoreException.class)
public void testPerformingInsertsWhenNoKeyGeneratorIsPresent() {
final CrudRepositorySupport support = new CrudRepositorySupport();
Expand Down Expand Up @@ -90,4 +87,28 @@ public void testPerformingInsertsWhenAKeyGeneratorIsPresent() {
assertThat(dataStore.getRequests().get(0).getKey(), is(notNullValue()));
}

@Test
public void testInsertingMultipleEntities() {
final CrudRepositorySupport support = new CrudRepositorySupport();
final MemoryDataStore<String, Person> actualDataStore = new MemoryDataStore<>(Person.class);
final SpyingDataStore<String, Person> dataStore = new SpyingDataStore<>(actualDataStore, new AtomicLong());
support.setDataStore(dataStore);
support.setRepositoryMetadata(
new ImmutableRepositoryMetadata(String.class, Person.class, SimplePersonRepository.class, "id"));
support.setKeyGenerator(new UUIDKeyGenerator());

final Person first = new Person();
final Person second = new Person();
final Person third = new Person();

support.insert(Arrays.asList(first, second, third));

assertThat(dataStore.getRequests(), hasSize(3));
assertThat(dataStore.getRequests().get(0).getOperation(), is(Operation.SAVE));
assertThat(dataStore.getRequests().get(0).getEntity(), is(first));
assertThat(dataStore.getRequests().get(1).getOperation(), is(Operation.SAVE));
assertThat(dataStore.getRequests().get(1).getEntity(), is(second));
assertThat(dataStore.getRequests().get(2).getOperation(), is(Operation.SAVE));
assertThat(dataStore.getRequests().get(2).getEntity(), is(third));
}
}