Commit 28871709 authored by Matija Obreza's avatar Matija Obreza

Basic Unit tests for core are implemented

parent 9310fd83
......@@ -40,7 +40,7 @@ public class BasicModel implements EntityId, Serializable {
@JsonView({ JsonViews.Update.class, JsonViews.Protected.class })
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false, length = 20)
@Column(name = "id", unique = true, nullable = false)
private Long id = InMemoryIdGenerator.nextId();
/*
......
......@@ -40,19 +40,19 @@ public class ClassPK implements Serializable {
private static final long serialVersionUID = 4850290657706341853L;
/** The id. */
@JsonView(JsonViews.Internal.class)
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false, length = 20)
@JsonView(JsonViews.Internal.class)
protected Long id;
/** The classname. */
@JsonView(JsonViews.Public.class)
@Column(length = 200, updatable = false, unique = true, nullable = false)
@JsonView(JsonViews.Public.class)
protected String classname;
/** The short name. */
@Column(length = 50, unique = true)
@Column(length = DefaultModelConstants.SHORTNAME_LENGTH, unique = true)
private String shortName;
/**
......
......@@ -20,39 +20,12 @@ package org.genesys.blocks.model;
*
* @author Matija Obreza
*/
public interface ModelConstants {
/** The businesscard position len. */
int BUSINESSCARD_POSITION_LEN = 100;
/** The person name len. */
int PERSON_NAME_LEN = 200;
/** The address len. */
int ADDRESS_LEN = 400;
/** The countrycode len. */
int COUNTRYCODE_LEN = 3;
/** The emailaddress len. */
int EMAILADDRESS_LEN = 100;
/** The phonenumber len. */
int PHONENUMBER_LEN = 50;
/** The currency len. */
int CURRENCY_LEN = 3;
/** The custom label len. */
int CUSTOM_LABEL_LEN = 100;
/** The contact detail len. */
int CONTACT_DETAIL_LEN = 200;
/** The subject length. */
int SUBJECT_LENGTH = 200;
public interface DefaultModelConstants {
/** The title length. */
int TITLE_LENGTH = 200;
/** The shortname length. */
int SHORTNAME_LENGTH = 50;
}
......@@ -27,7 +27,7 @@ import com.fasterxml.jackson.annotation.JsonView;
* The Class UuidEntity.
*/
@MappedSuperclass
public abstract class UuidEntity extends AuditedVersionedModel implements IdUUID {
public abstract class UuidModel extends AuditedVersionedModel implements IdUUID {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 2639238742778129554L;
......@@ -41,8 +41,7 @@ public abstract class UuidEntity extends AuditedVersionedModel implements IdUUID
* {@inheritDoc}
*/
@PrePersist
protected void prepersist() {
super.prepersist();
private void prepersistUuidModel() {
if (this.uuid == null) {
this.uuid = UUID.randomUUID();
}
......
......@@ -28,6 +28,8 @@ import com.fasterxml.jackson.annotation.JsonView;
@MappedSuperclass
public abstract class VersionedModel extends BasicModel implements Activatable {
static final boolean DEFAULT_ACTIVE_VALUE = true;
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 6486704708017489425L;
......@@ -39,13 +41,13 @@ public abstract class VersionedModel extends BasicModel implements Activatable {
/** Active by default!. */
@Column(nullable = false)
protected boolean active = true;
protected boolean active = DEFAULT_ACTIVE_VALUE;
/**
* First persisted version is 1
*/
@PrePersist
protected void prepersist() {
private void prepersistVersionedModel() {
version = 1;
}
......
......@@ -40,7 +40,7 @@ public abstract class VersionedModelWithoutId implements Serializable, Activatab
/** Active by default!. */
@Column(nullable = false)
protected boolean active = true;
protected boolean active = VersionedModel.DEFAULT_ACTIVE_VALUE;
/**
* Gets the version.
......
/*
* Copyright 2017 Global Crop Diversity Trust
*
* 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.genesys.blocks.model;
import javax.persistence.Column;
import javax.persistence.Entity;
@Entity
public class UuidEntity extends UuidModel {
private static final long serialVersionUID = 1L;
@Column(nullable = false)
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/*
* Copyright 2017 Global Crop Diversity Trust
*
* 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.genesys.blocks.persistence;
import org.genesys.blocks.model.UuidEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UuidEntityRepository extends JpaRepository<UuidEntity, Long> {
}
......@@ -15,10 +15,16 @@
*/
package org.genesys.blocks.tests;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import org.genesys.blocks.config.ApplicationConfig;
import org.genesys.blocks.persistence.UuidEntityRepository;
import org.genesys.blocks.persistence.VersionedEntityRepository;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
......@@ -35,9 +41,20 @@ import org.springframework.transaction.annotation.Transactional;
public abstract class BaseTest {
protected final Logger LOG = LoggerFactory.getLogger(getClass());
@Autowired
protected VersionedEntityRepository versionedEntityRepository;
@Autowired
protected UuidEntityRepository uuidEntityRepository;
@AfterTransaction
@Transactional
public void cleanup() {
LOG.trace("Deleting all from ...");
versionedEntityRepository.deleteAll();
assertThat(versionedEntityRepository.count(), is(0l));
uuidEntityRepository.deleteAll();
assertThat(uuidEntityRepository.count(), is(0l));
}
}
\ No newline at end of file
/*
* Copyright 2017 Global Crop Diversity Trust
*
* 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.genesys.blocks.tests.model;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import java.util.UUID;
import org.genesys.blocks.model.UuidEntity;
import org.genesys.blocks.tests.ServiceTest;
import org.junit.Test;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class UuidModelTest extends ServiceTest {
private static final String ENTITY_NAME = "Entity";
@Test
public void testConstructor() {
UuidEntity entity = new UuidEntity();
assertThat(entity.getId(), not(nullValue()));
assertThat(entity.getUuid(), is(nullValue()));
assertThat(entity.getVersion(), nullValue());
}
@Test
public void versionIs1OnPersist() {
UuidEntity entity = createEntity();
assertThat(entity.getVersion(), nullValue());
entity = uuidEntityRepository.save(entity);
assertThat(entity.getVersion(), is(1));
}
@Test
public void uuidIsAssignedOnPersist() {
UuidEntity entity = createEntity();
assertThat(entity.getVersion(), nullValue());
entity = uuidEntityRepository.save(entity);
assertThat(entity.getUuid(), not(nullValue()));
}
@Test
public void uuidAssignedIsPersisted() {
UuidEntity entity = createEntity();
UUID uuid=UUID.randomUUID();
entity.setUuid(uuid);
entity = uuidEntityRepository.save(entity);
assertThat(entity.getUuid(), is(uuid));
}
public UuidEntity createEntity() {
UuidEntity entity = new UuidEntity();
entity.setName(ENTITY_NAME);
return entity;
}
}
......@@ -16,6 +16,7 @@
package org.genesys.blocks.tests.model;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
......@@ -27,16 +28,42 @@ import org.springframework.transaction.annotation.Transactional;
@Transactional
public class VersionedModelTest extends ServiceTest {
private static final String ENTITY_NAME = "Entity";
@Test
public void idIsNotNull() {
VersionedEntity entity=new VersionedEntity();
public void testConstructor() {
VersionedEntity entity = new VersionedEntity();
assertThat(entity.getId(), not(nullValue()));
assertThat(entity.getVersion(), nullValue());
assertThat(entity.isActive(), is(true));
}
@Test
public void versionIsNull() {
VersionedEntity entity=new VersionedEntity();
public void versionIs1OnPersist() {
VersionedEntity entity = createEntity();
assertThat(entity.getVersion(), nullValue());
entity = versionedEntityRepository.save(entity);
assertThat(entity.getVersion(), is(1));
}
@Test
public void activeIsOn() {
VersionedEntity entity = versionedEntityRepository.save(createEntity());
assertThat(entity.getVersion(), is(1));
}
@Test
public void activeIsOff() {
VersionedEntity entity = versionedEntityRepository.save(createEntity());
entity.setActive(false);
entity = versionedEntityRepository.save(entity);
assertThat(entity.isActive(), is(false));
}
public VersionedEntity createEntity() {
VersionedEntity entity = new VersionedEntity();
entity.setName(ENTITY_NAME);
return entity;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment