Commit c9d16ce3 authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch '43-test-coverage' into 'main'

Resolve "Test coverage 50%"

Closes #43

See merge request genesys-pgr/application-blocks!92
parents 4278ff8f 57845af8
...@@ -39,6 +39,8 @@ ...@@ -39,6 +39,8 @@
<version>1.1.3</version> <version>1.1.3</version>
<executions> <executions>
<execution> <execution>
<id>generate-source-entities</id>
<phase>generate-sources</phase>
<goals> <goals>
<goal>process</goal> <goal>process</goal>
</goals> </goals>
...@@ -51,6 +53,21 @@ ...@@ -51,6 +53,21 @@
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor> <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration> </configuration>
</execution> </execution>
<execution>
<id>generate-test-entities</id>
<phase>generate-test-sources</phase>
<goals>
<goal>test-process</goal>
</goals>
<configuration>
<includes>
<!-- List packages to be processed -->
<include>org.genesys.blocks.auditlog.model</include>
</includes>
<outputDirectory>target/generated-test-sources/querydsl</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions> </executions>
<dependencies> <dependencies>
<dependency> <dependency>
......
/* /*
* Copyright 2018 Global Crop Diversity Trust * Copyright 2021 Global Crop Diversity Trust
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -13,26 +13,17 @@ ...@@ -13,26 +13,17 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.genesys.blocks.model; package org.genesys.blocks.auditlog.model;
import java.util.concurrent.atomic.AtomicLong; import org.genesys.blocks.model.UuidModel;
import javax.persistence.Entity;
/** /**
* The Class InMemoryIdGenerator. * The Class Other2Entity.
*/ */
public class InMemoryIdGenerator { @Entity
public class Other2Entity extends UuidModel {
/** The Constant id. */ private static final long serialVersionUID = 1L;
private static final AtomicLong id = new AtomicLong(-1l);
/**
* Next id.
*
* @return the long
*/
public static long nextId() {
// LOG.trace("Next random id will be " + (id.get() - 1));
return id.decrementAndGet();
}
} }
/*
* Copyright 2021 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.auditlog.model;
import java.util.List;
import com.querydsl.core.types.Predicate;
import org.genesys.blocks.model.filters.UuidModelFilter;
public class Other2EntityFilter extends UuidModelFilter<Other2EntityFilter, Other2Entity> {
@Override
public List<Predicate> collectPredicates() {
return collectPredicates(QOther2Entity.other2Entity);
}
public List<Predicate> collectPredicates(QOther2Entity other2Entity) {
return super.collectPredicates(other2Entity, other2Entity._super);
}
}
/*
* Copyright 2021 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.auditlog.persistence;
import org.genesys.blocks.auditlog.model.Other2Entity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.stereotype.Repository;
@Repository
public interface Other2EntityRepository extends JpaRepository<Other2Entity, Long>, QuerydslPredicateExecutor<Other2Entity> {
}
/*
* Copyright 2021 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.auditlog.test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import org.genesys.blocks.auditlog.model.Other2Entity;
import org.genesys.blocks.auditlog.model.Other2EntityFilter;
import org.genesys.blocks.auditlog.persistence.Other2EntityRepository;
import org.junit.After;
import org.junit.Test;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
public class Other2EntityTest extends EntityTest<Other2Entity, Other2EntityRepository> {
@After
@Transactional
public void cleanup() {
repository.deleteAll();
assertThat(repository.count(), is(0L));
}
@Test
public void testUuidFilter() {
Other2Entity record = repository.save(makeDefault());
assertThat(record, notNullValue());
assertThat(record.getUuid(), notNullValue());
Other2Entity record2 = repository.save(makeDefault());
assertThat(record2, notNullValue());
assertThat(record2.getUuid(), notNullValue());
assertThat(repository.count(), is(2L));
Other2EntityFilter filter = new Other2EntityFilter();
filter.uuid().add(record2.getUuid());
Page<Other2Entity> page = repository.findAll(filter.buildPredicate(), PageRequest.of(0, 5));
assertThat(page.getTotalElements(), is(1L));
assertThat(page.getContent().get(0), equalTo(record2));
}
@Test
public void testAuditedVersionedModelFilter() {
Other2Entity record = repository.save(makeDefault());
assertThat(record, notNullValue());
assertThat(record.getUuid(), notNullValue());
assertThat(record.getId(), is(greaterThan(0L)));
Calendar instance = Calendar.getInstance();
instance.add(Calendar.MONTH, 10);
Other2Entity record2 = repository.save(makeDefault(UUID.randomUUID(), 2L, instance.getTime()));
assertThat(record2, notNullValue());
assertThat(record2.getUuid(), notNullValue());
assertThat(record2.getId(), is(greaterThan(1L)));
assertThat(repository.count(), is(2L));
Other2EntityFilter filter = new Other2EntityFilter();
filter.createdBy().add(2L);
Page<Other2Entity> page = repository.findAll(filter.buildPredicate(), PageRequest.of(0, 5));
assertThat(page.getTotalElements(), is(1L));
assertThat(page.getContent().get(0), equalTo(record2));
filter = new Other2EntityFilter();
filter.lastModifiedBy().add(1L);
page = repository.findAll(filter.buildPredicate(), PageRequest.of(0, 5));
assertThat(page.getTotalElements(), is(1L));
assertThat(page.getContent().get(0), equalTo(record));
filter = new Other2EntityFilter();
filter.createdDate().ge(instance.getTime());
page = repository.findAll(filter.buildPredicate(), PageRequest.of(0, 5));
assertThat(page.getTotalElements(), is(1L));
assertThat(page.getContent().get(0), equalTo(record2));
filter = new Other2EntityFilter();
filter.lastModifiedDate().ge(instance.getTime());
page = repository.findAll(filter.buildPredicate(), PageRequest.of(0, 5));
assertThat(page.getTotalElements(), is(1L));
assertThat(page.getContent().get(0), equalTo(record2));
}
@Override
protected Other2Entity makeDefault() {
return makeDefault(UUID.randomUUID(), 1L, new Date());
}
private Other2Entity makeDefault(UUID uuid, Long createdBy, Date createdDate) {
Other2Entity other2Entity = new Other2Entity();
other2Entity.setUuid(uuid);
other2Entity.setCreatedBy(createdBy);
other2Entity.setLastModifiedBy(createdBy);
other2Entity.setCreatedDate(createdDate);
other2Entity.setLastModifiedDate(createdDate);
return other2Entity;
}
}
...@@ -37,7 +37,7 @@ public class BasicModel extends EmptyModel { ...@@ -37,7 +37,7 @@ public class BasicModel extends EmptyModel {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false) @Column(name = "id", unique = true, nullable = false)
private Long id = null; // InMemoryIdGenerator.nextId(); private Long id = null;
/* /*
* (non-Javadoc) * (non-Javadoc)
......
/*
* Copyright 2018 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 java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.Version;
import com.fasterxml.jackson.annotation.JsonView;
/**
* The Class VersionedModelWithoutId.
*/
@MappedSuperclass
public abstract class VersionedModelWithoutId implements Serializable, Activatable {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 292945663251323045L;
/** The version. */
@JsonView({ JsonViews.Update.class, JsonViews.Protected.class })
@Version
@Column(nullable = false)
Integer version = null;
/** Active by default!. */
@Column(nullable = false)
protected boolean active = VersionedModel.DEFAULT_ACTIVE_VALUE;
/**
* First persisted version is 1
*/
@PrePersist
private void prepersistVersionedModel() {
version = 1;
}
/**
* Gets the version.
*
* @return the version
*/
public Integer getVersion() {
return version;
}
/**
* Sets the version.
*
* @param version the new version
*/
public void setVersion(final Integer version) {
this.version = version;
}
/*
* (non-Javadoc)
* @see org.genesys.blocks.model.Activatable#isActive()
*/
@Override
public boolean isActive() {
return active;
}
/**
* Sets the active.
*
* @param active the new active
*/
public void setActive(final boolean active) {
this.active = active;
}
/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = (prime * result) + ((version == null) ? 0 : version.hashCode());
return result;
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final VersionedModelWithoutId other = (VersionedModelWithoutId) obj;
if (version == null) {
if (other.version != null)
return false;
} else if (!version.equals(other.version))
return false;
return true;
}
}
/*
* Copyright 2021 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;
import java.util.Date;
@Entity
public class TestingEntity extends BasicModel {
private static final long serialVersionUID = 1L;
@Column
private Date date;
@Column
private Integer number;
public Date getDate() {
return date;
}
public TestingEntity() {
}
public TestingEntity(Date date, Integer number) {
this.date = date;
this.number = number;
}
public void setDate(Date date) {
this.date = date;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
}
/*
* Copyright 2021 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 java.util.List;
import com.querydsl.core.types.Predicate;
import org.genesys.blocks.model.filters.BasicModelFilter;
import org.genesys.blocks.model.filters.DateFilter;
import org.genesys.blocks.model.filters.NumberFilter;
public class TestingEntityFilter extends BasicModelFilter<TestingEntityFilter, TestingEntity> {
public DateFilter date;
public NumberFilter<Integer> number;
@Override
public List<Predicate> collectPredicates() {
return collectPredicates(QTestingEntity.testingEntity);
}
public List<Predicate> collectPredicates(QTestingEntity testingEntity) {
final List<Predicate> predicates = super.collectPredicates(testingEntity);
if (date != null) {
predicates.add(date.buildQuery(testingEntity.date));
}
if (number != null) {
predicates.add(number.buildQuery(testingEntity.number));
}
return predicates;
}
}
/*
* Copyright 2021 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.TestingEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
/**
* The Interface TestingEntityRepository.
*/
public interface TestingEntityRepository extends JpaRepository<TestingEntity, Long>, QuerydslPredicateExecutor<TestingEntity> {
}
...@@ -21,10 +21,12 @@ import static org.hamcrest.MatcherAssert.assertThat; ...@@ -21,10 +21,12 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsInAnyOrder;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.genesys.blocks.model.filters.StringFilter; import org.genesys.blocks.model.filters.StringFilter;
import org.hamcrest.Matchers;
import org.junit.Test; import org.junit.Test;
/** /**
...@@ -72,4 +74,57 @@ public class StringFilterTest { ...@@ -72,4 +74,57 @@ public class StringFilterTest {
assertThat(filter2.sw.size(), is(2)); assertThat(filter2.sw.size(), is(2));
assertThat(Arrays.asList("AAA", "BBB"), containsInAnyOrder(filter2.sw.toArray())); assertThat(Arrays.asList("AAA", "BBB"), containsInAnyOrder(filter2.sw.toArray()));
} }
@Test
public void testEq() {
StringFilter filter = new StringFilter();
filter.eq();
assertThat(filter.eq, is(new HashSet<>()));
StringFilter eq = filter.eq("AAA", "BBB");
assertThat(eq.eq, containsInAnyOrder("AAA", "BBB"));
assertThat(eq, equalTo(filter));
filter.eq("CCC");
assertThat(filter.eq, containsInAnyOrder("AAA", "BBB", "CCC"));
}