Commit 57845af8 authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza
Browse files

Increase code coverage

- added CustomAclServiceTest
parent 6861409a
......@@ -39,6 +39,8 @@
<version>1.1.3</version>
<executions>
<execution>
<id>generate-source-entities</id>
<phase>generate-sources</phase>
<goals>
<goal>process</goal>
</goals>
......@@ -51,6 +53,21 @@
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</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>
<dependencies>
<dependency>
......
/*
* 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 org.genesys.blocks.model.UuidModel;
import javax.persistence.Entity;
/**
* The Class Other2Entity.
*/
@Entity
public class Other2Entity extends UuidModel {
private static final long serialVersionUID = 1L;
}
/*
* 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;
}
}
/*
* 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;
import static org.hamcrest.Matchers.containsInAnyOrder;
import java.util.Arrays;
import java.util.HashSet;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.genesys.blocks.model.filters.StringFilter;
import org.hamcrest.Matchers;
import org.junit.Test;
/**
......@@ -72,4 +74,57 @@ public class StringFilterTest {
assertThat(filter2.sw.size(), is(2));
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"));
}
@Test
public void testEquals() {
StringFilter filter = new StringFilter();
StringFilter equals = filter.equals();
assertThat(filter.eq, Matchers.nullValue());
assertThat(equals.eq, is(new HashSet<>()));
StringFilter equalsFilter = StringFilter.equals("AAA", "BBB");
assertThat(equalsFilter.eq, containsInAnyOrder("AAA", "BBB"));
}
@Test
public void testContains() {
StringFilter filter = new StringFilter();
filter.contains();
assertThat(filter.contains, is(new HashSet<>()));
StringFilter containsFilter = filter.contains("AAA", "BBB");
assertThat(containsFilter.contains, containsInAnyOrder("AAA", "BBB"));
assertThat(containsFilter, equalTo(filter));
filter.contains("CCC");
assertThat(filter.contains, containsInAnyOrder("AAA", "BBB", "CCC"));
}
@Test
public void testStartsWith() {
StringFilter filter = new StringFilter();
filter.sw();
assertThat(filter.sw, is(new HashSet<>()));
StringFilter swFilter = filter.sw("AAA", "BBB");
assertThat(swFilter.sw, containsInAnyOrder("AAA", "BBB"));
assertThat(swFilter, equalTo(filter));
filter.sw("CCC");
assertThat(filter.sw, containsInAnyOrder("AAA", "BBB", "CCC"));
}
}
......@@ -15,10 +15,9 @@
*/
package org.genesys.blocks.tests.model;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
......@@ -141,6 +140,12 @@ public class CopyableTest {
assertTrue(Collection.class.isAssignableFrom(source.set.getClass()));
assertTrue(Collection.class.isAssignableFrom(source.list.getClass()));
// declare anonymous class
final Copyable<Object> copyable = new Copyable<>() {
};
assertThrows(IllegalArgumentException.class, () -> copyable.apply(source));
assertThrows(IllegalArgumentException.class, () -> copyable.apply(null));
final Child copy = source.copy();
assertThat(Parent.STATIC_INT, is(33));
assertThat(copy.myId, not(source.myId));
......@@ -165,4 +170,58 @@ public class CopyableTest {
assertThat(copy.myList, hasSize(0));
assertThat(copy.myMap.keySet(), hasSize(0));
}
@Test
public void testCopyList() {
final Child source = new Child();
source.defaultInt = 1;
source.protectedInt = 3;
source.publicInt = 4;
source.myInt = 5;
source.myId = 1L;
source.myCreatedBy = 2L;
source.myLastModifiedBy = 3L;
source.myCreatedDate = new Date();
source.myLastModifiedDate = new Date();
source.myNotCopyable = "test string";
source.setPrivateInt(6);
source.myDefaultInt = 7;
source.put("test1");
source.put("test2");
// declare anonymous class
final Copyable<Object> copyable = new Copyable<>() {
};
assertThat(copyable.copy(), nullValue());
// return empty list if a source is null
assertThat(copyable.copyList(null, (children) -> {}).size(), is(0));
final List<Child> copyList = copyable.copyList(List.of(source), (children) -> {});
assertThat(copyList.size(), is(1));
final Child copy = copyList.get(0);
assertThat(Parent.STATIC_INT, is(33));
assertThat(copy.myId, not(source.myId));
assertThat(copy.myCreatedBy, not(source.myCreatedBy));
assertThat(copy.myLastModifiedBy, not(source.myLastModifiedBy));
assertThat(copy.myCreatedDate, not(source.myCreatedDate));
assertThat(copy.myLastModifiedDate, is(source.myLastModifiedDate));
assertThat(copy.myNotCopyable, not(source.myNotCopyable));
assertThat(copy.finalInt, is(42));
assertThat(copy.defaultInt, is(source.defaultInt));
assertThat(copy.protectedInt, is(source.protectedInt));
assertThat(copy.publicInt, is(source.publicInt));
assertThat(copy.finalInt, is(source.finalInt));
assertThat(copy.myInt, is(source.myInt));
assertThat(copy.myFinalInt, is(43));
assertThat(copy.getPrivateInt(), not(is(source.getPrivateInt())));
assertThat(copy.myDefaultInt, is(source.myDefaultInt));
assertThat(copy.set, hasSize(0));
assertThat(copy.list, hasSize(0));
assertThat(copy.map.keySet(), hasSize(0));
assertThat(copy.mySet, hasSize(0));
assertThat(copy.myList, hasSize(0));
assertThat(copy.myMap.keySet(), hasSize(0));
}
}
/*
* 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.tests.model;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import org.genesys.blocks.model.SelfCleaning;
import org.junit.Test;
/**
* The Class SelfCleaningTest.
*/
public class SelfCleaningTest {
private static class TestEntity implements SelfCleaning {
public static final String SOME_CONSTANT = " testValue ";
public String foo;
public String bar;
public final String baz;
public TestEntity(String foo, String bar, String baz) {
this.foo = foo;
this.bar = bar;
this.baz = baz;
}
}