Skip to content

Commit

Permalink
bring back mapping tests
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr committed Sep 1, 2024
1 parent fc8710a commit 9d6fa12
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 org.springframework.data.aerospike.mapping;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.data.aerospike.sample.Person;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

/**
* @author Peter Milne
* @author Jean Mercier
*/
public class AerospikeMappingContextTest {

@Test
public void setFieldNamingStrategy() {
AerospikeMappingContext context = new AerospikeMappingContext();
context.setApplicationContext(mock(ApplicationContext.class));
context.setFieldNamingStrategy(null);

AerospikePersistentEntity<?> entity = context.getRequiredPersistentEntity(Person.class);

assertThat(entity.getPersistentProperty("firstName").getField().getName()).isEqualTo("firstName");
}

@Test
public void createPersistentEntityTypeInformationOfT() {
AerospikeMappingContext context = new AerospikeMappingContext();
context.setApplicationContext(mock(ApplicationContext.class));
context.setFieldNamingStrategy(null);

AerospikePersistentEntity<?> entity = context.getRequiredPersistentEntity(Person.class);

assertThat(entity.getTypeInformation().getType().getSimpleName()).isEqualTo(Person.class.getSimpleName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* 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 org.springframework.data.aerospike.mapping;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.aerospike.BaseBlockingIntegrationTests;

import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.springframework.data.aerospike.mapping.BasicAerospikePersistentEntity.DEFAULT_EXPIRATION;
import static org.springframework.data.aerospike.sample.SampleClasses.*;

public class AerospikePersistentEntityTest extends BaseBlockingIntegrationTests {

@Autowired
protected AerospikeMappingContext context;

@Test
public void shouldReturnExpirationForDocumentWithExpiration() {
BasicAerospikePersistentEntity<?> persistentEntity =
context.getRequiredPersistentEntity(DocumentWithExpiration.class);

assertThat(persistentEntity.getExpiration()).isEqualTo(EXPIRATION_ONE_SECOND);
}

@Test
public void shouldReturnExpirationForDocumentWithExpirationExpression() {
BasicAerospikePersistentEntity<?> persistentEntity =
context.getRequiredPersistentEntity(DocumentWithExpirationExpression.class);

assertThat(persistentEntity.getExpiration()).isEqualTo(EXPIRATION_ONE_SECOND);
}

@Test
public void shouldReturnExpirationForDocumentWithExpirationUnit() {
BasicAerospikePersistentEntity<?> persistentEntity =
context.getRequiredPersistentEntity(DocumentWithExpirationUnit.class);

assertThat(persistentEntity.getExpiration()).isEqualTo((int) TimeUnit.MINUTES.toSeconds(1));
}

@Test
public void shouldReturnZeroForDocumentWithoutExpiration() {
BasicAerospikePersistentEntity<?> persistentEntity =
context.getRequiredPersistentEntity(DocumentWithoutExpiration.class);

assertThat(persistentEntity.getExpiration()).isEqualTo(DEFAULT_EXPIRATION);
}

@Test
public void shouldReturnNeverExpireForDocumentWithNeverExpireAndWithoutExpirationUnit() {
BasicAerospikePersistentEntity<?> persistentEntity =
context.getRequiredPersistentEntity(DocumentWithNeverExpireAndWithoutExpirationUnit.class);

assertThat(persistentEntity.getExpiration()).isEqualTo(EXPIRATION_NEVER_EXPIRE);
}

@Test
public void shouldReturnNeverExpireForDocumentWithNeverExpireAndExpirationUnit() {
BasicAerospikePersistentEntity<?> persistentEntity =
context.getRequiredPersistentEntity(DocumentWithNeverExpireAndExpirationUnit.class);

assertThat(persistentEntity.getExpiration()).isEqualTo(EXPIRATION_NEVER_EXPIRE);
}

@Test
public void shouldReturnZeroForDocumentWithoutAnnotation() {
BasicAerospikePersistentEntity<?> persistentEntity =
context.getRequiredPersistentEntity(DocumentWithoutAnnotation.class);

assertThat(persistentEntity.getExpiration()).isEqualTo(DEFAULT_EXPIRATION);
}

@Test
public void shouldFailForDocumentWithExpirationAndExpression() {
BasicAerospikePersistentEntity<?> persistentEntity =
context.getRequiredPersistentEntity(DocumentWithExpirationAndExpression.class);

assertThatThrownBy(persistentEntity::getExpiration)
.isInstanceOf(IllegalStateException.class)
.hasMessage("Both 'expiration' and 'expirationExpression' are set");
}

@Test
public void shouldGetExpirationProperty() {
BasicAerospikePersistentEntity<?> persistentEntity =
context.getRequiredPersistentEntity(DocumentWithExpirationAnnotation.class);
AerospikePersistentProperty expirationProperty = persistentEntity.getExpirationProperty();

assertThat(expirationProperty).isNotNull();
assertThat(expirationProperty.isExpirationProperty()).isTrue();
assertThat(expirationProperty.isExpirationSpecifiedAsUnixTime()).isFalse();
}

@Test
public void shouldGetExpirationPropertySpecifiedAsUnixTime() {
BasicAerospikePersistentEntity<?> persistentEntity =
context.getRequiredPersistentEntity(DocumentWithUnixTimeExpiration.class);
AerospikePersistentProperty expirationProperty = persistentEntity.getExpirationProperty();

assertThat(expirationProperty).isNotNull();
assertThat(expirationProperty.isExpirationProperty()).isTrue();
assertThat(expirationProperty.isExpirationSpecifiedAsUnixTime()).isTrue();
}

@Test
public void shouldFailForNonExpirationProperty() {
BasicAerospikePersistentEntity<?> persistentEntity =
context.getRequiredPersistentEntity(DocumentWithUnixTimeExpiration.class);
AerospikePersistentProperty expirationProperty = persistentEntity.getIdProperty();

assertThatThrownBy(expirationProperty::isExpirationSpecifiedAsUnixTime)
.isInstanceOf(IllegalStateException.class)
.hasMessage("Property id is not expiration property");
}

@Test
public void shouldResolvePlaceholdersInCollection() {
BasicAerospikePersistentEntity<?> persistentEntity =
context.getRequiredPersistentEntity(DocumentWithExpressionInCollection.class);

assertThat(persistentEntity.getSetName()).isEqualTo(DocumentWithExpressionInCollection.COLLECTION_PREFIX +
"service1");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 org.springframework.data.aerospike.mapping;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.aerospike.sample.SampleClasses.DocumentWithExpressionInCollection;
import org.springframework.data.aerospike.sample.SampleClasses.DocumentWithoutCollection;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@ExtendWith(MockitoExtension.class)
public class BasicAerospikePersistentEntityTest {

private final AerospikeMappingContext context = new AerospikeMappingContext();

@Test
public void shouldReturnSimpleClassNameIfCollectionNotSpecified() {
BasicAerospikePersistentEntity<?> entity = context.getRequiredPersistentEntity(DocumentWithoutCollection.class);

assertThat(entity.getSetName()).isEqualTo(DocumentWithoutCollection.class.getSimpleName());
}

@Test
public void shouldFailIfEnvironmentNull() {
BasicAerospikePersistentEntity<?> entity =
context.getRequiredPersistentEntity(DocumentWithExpressionInCollection.class);

assertThatThrownBy(entity::getSetName)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Environment must be set to use 'collection'");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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 org.springframework.data.aerospike.mapping;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.data.aerospike.sample.Person;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

/**
* @author Peter Milne
* @author Jean Mercier
*/
public class CachingAerospikePersistentPropertyTest {

AerospikeMappingContext context;

@BeforeEach
public void setUp() {
context = new AerospikeMappingContext();
context.setApplicationContext(mock(ApplicationContext.class));
}

@Test
public void isTransient() {
AerospikePersistentEntity<?> entity = context.getRequiredPersistentEntity(Person.class);

assertThat(entity.getIdProperty().isTransient()).isFalse();
}

@Test
public void isAssociation() {
AerospikePersistentEntity<?> entity = context.getRequiredPersistentEntity(Person.class);

assertThat(entity.getIdProperty().isAssociation()).isFalse();
}

@Test
public void usePropertyAccess() {
AerospikePersistentEntity<?> entity = context.getRequiredPersistentEntity(Person.class);

assertThat(entity.getIdProperty().usePropertyAccess()).isFalse();
}

@Test
public void isIdProperty() {
AerospikePersistentEntity<?> entity = context.getRequiredPersistentEntity(Person.class);

assertThat(entity.getIdProperty().isIdProperty()).isTrue();
}

@Test
public void getFieldName() {
AerospikePersistentEntity<?> entity = context.getRequiredPersistentEntity(Person.class);

assertThat(entity.getIdProperty().getName()).isEqualTo("id");
}
}

0 comments on commit 9d6fa12

Please sign in to comment.