Skip to content

Commit

Permalink
Merge pull request #667 from KyleAure/657-more-QBN-fixes
Browse files Browse the repository at this point in the history
Add test case for custom entity types and fix query methods for validation
  • Loading branch information
KyleAure authored Apr 16, 2024
2 parents a37cac5 + e01c2bf commit 76b999e
Show file tree
Hide file tree
Showing 16 changed files with 497 additions and 164 deletions.
50 changes: 50 additions & 0 deletions tck/src/main/java/ee/jakarta/tck/data/common/cdi/AddressBook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package ee.jakarta.tck.data.common.cdi;

import static jakarta.data.repository.By.ID;

import java.util.List;
import java.util.UUID;

import jakarta.data.repository.By;
import jakarta.data.repository.DataRepository;
import jakarta.data.repository.Delete;
import jakarta.data.repository.Find;
import jakarta.data.repository.Repository;

/**
* An AddressBook repository for testing.
*
* Uses the AddressRecord with the {@code @EntityDefining} annotation {@code TCKEntity}
* to ensure the mock Jakarta Data provider from this TCK implements this repository interface.
*
* @see ee.jakarta.tck.data.common.cdi.DirectoryRepository
*/
@Repository
public interface AddressBook extends DataRepository<AddressRecord, UUID> {

public static final String ADDRESS_PROVIDER = "ADDRESS_PROVIDER";

@Find
List<AddressRecord> findById(List<UUID> ids);

@Delete
void deleteById(@By(ID) UUID id);

@PutTCKLifecyleMethod
AddressRecord putAddress(AddressRecord address);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package ee.jakarta.tck.data.common.cdi;

import java.util.UUID;

/**
* A test entity that will be persisted to a repository.
* Uses the custom {@code @TCKEntity} annotation.
*
* @see ee.jakarta.tck.data.common.cdi.TCKEntity
*/
@TCKEntity
public record AddressRecord(UUID id, int house, String street, String city, String state, long zipCode) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package ee.jakarta.tck.data.common.cdi;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
* An implementation of the AddressBook repository interface.
* Implementation is backed by an in-memory HashMap.
*/
public class AddressRepository implements AddressBook {

private Map<UUID, AddressRecord> data = new HashMap<UUID, AddressRecord>();

public AddressRepository() {
}

@Override
public List<AddressRecord> findById(List<UUID> ids) {
return data.values()
.stream()
.filter(a -> ids.contains(a.id()))
.collect(Collectors.toList());
}

@Override
public void deleteById(UUID id) {
data.remove(id);
}

@Override
public AddressRecord putAddress(AddressRecord address) {
return data.put(address.id(), address);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
/**
* A Directory repository for testing.
*
* Uses the 'provider' attribute to ensure the mock Jakarta Data provider
* from the TCK implements this repository interface.
*
* @see ee.jakarta.tck.data.common.cdi.DirectoryRepository
*/
@Repository(provider = Directory.PERSON_PROVIDER)
Expand Down
9 changes: 5 additions & 4 deletions tck/src/main/java/ee/jakarta/tck/data/common/cdi/Person.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,11 +17,12 @@

/**
* A test entity that will be persisted to a repository.
* Uses the custom {@code @PersonEntity} annotation.
*
* @see ee.jakarta.tck.data.common.cdi.PersonEntity
* Is annotated with both persistence and nosql {@code @Entity}
* to ensure the provider attribute of the repository is honored.
*/
@PersonEntity
@jakarta.nosql.Entity
@jakarta.persistence.Entity
public class Person {
public long id;
public String firstName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package ee.jakarta.tck.data.common.cdi;

import static java.lang.annotation.ElementType.METHOD;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023,2024 Contributors to the Eclipse Foundation
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -24,10 +24,12 @@

/**
* A custom entity annotation for testing.
*
* Only supported by the mock Jakarta Data provider implemented by this TCK.
*/
@EntityDefining
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface PersonEntity {
public @interface TCKEntity {

}
35 changes: 30 additions & 5 deletions tck/src/main/java/ee/jakarta/tck/data/core/cdi/ExtensionTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;

import ee.jakarta.tck.data.common.cdi.AddressBook;
import ee.jakarta.tck.data.common.cdi.AddressRecord;
import ee.jakarta.tck.data.common.cdi.Directory;
import ee.jakarta.tck.data.common.cdi.Person;
import ee.jakarta.tck.data.core.cdi.provider.DirectoryBuildCompatibleExtension;
import ee.jakarta.tck.data.core.cdi.provider.BuildCompatibleExtensionImpl;
import ee.jakarta.tck.data.framework.junit.anno.AnyEntity;
import ee.jakarta.tck.data.framework.junit.anno.Assertion;
import ee.jakarta.tck.data.framework.junit.anno.CDI;
Expand All @@ -42,8 +45,8 @@ public class ExtensionTests {
@Deployment
public static WebArchive createDeployment() {
JavaArchive provider = ShrinkWrap.create(JavaArchive.class)
.addPackage(DirectoryBuildCompatibleExtension.class.getPackage())
.addAsServiceProvider(BuildCompatibleExtension.class, DirectoryBuildCompatibleExtension.class);
.addPackage(BuildCompatibleExtensionImpl.class.getPackage())
.addAsServiceProvider(BuildCompatibleExtension.class, BuildCompatibleExtensionImpl.class);


return ShrinkWrap.create(WebArchive.class)
Expand All @@ -56,16 +59,23 @@ public static WebArchive createDeployment() {
@Inject
Directory directory;

@Assertion(id = "133", strategy = "Verifies ability for a CDI BuildCompatibleExtension to handle custom entity annotations")
@Inject
AddressBook addressBook;

@Assertion(id = "133", strategy = "Verifies ability for a CDI BuildCompatibleExtension to handle "
+ "EntityDefining annotations and repository provider attributes.")
public void testDataProviderWithBuildCompatibleExtension() {
List<Person> result = directory.findByIdInOrderByAgeDesc(List.of(04L, 05L, 011L));
List<String> firstNames = result.stream().map(p -> p.firstName).collect(Collectors.toList());
List<String> lastNames = result.stream().map(p -> p.lastName).collect(Collectors.toList());
assertEquals(List.of("Olivia", "Lauren", "Victor"), firstNames);
assertEquals(List.of("Skinner", "Powell", "Gibson"), lastNames);


}

@Assertion(id = "640", strategy = "Verifies that another Jakarta Data Provider does not attempt to implement the Dictonary repository")
@Assertion(id = "640", strategy = "Verifies that another Jakarta Data Provider does not attempt to "
+ "implement the Dictonary repository based on provider attribute.")
public void testDataRepositoryHonorsProviderAttribute() {
long id = 013L;
Person original = new Person(id, "Mark", "Pearson", 46);
Expand All @@ -78,4 +88,19 @@ public void testDataRepositoryHonorsProviderAttribute() {
directory.deleteById(id);
}
}

@Assertion(id = "640", strategy = "Verifies that another Jakarta Data Provider does not attempt to "
+ "implement the Address repository based on the EntityDefining annotation.")
public void testDataRepositoryHonorsEntityDefiningAnnotation() {
UUID id = UUID.randomUUID();
AddressRecord original = new AddressRecord(id, 1057, "1st Street NW", "Rochester", "MN", 55901);
AddressRecord updated = new AddressRecord(id, 1057, "1st Street NW", "Rochester", "MN", 55902);

try {
assertEquals(null, addressBook.putAddress(original));
assertEquals(original, addressBook.putAddress(updated));
} finally {
addressBook.deleteById(id);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -17,27 +17,25 @@

import java.util.logging.Logger;

import ee.jakarta.tck.data.common.cdi.Directory;
import ee.jakarta.tck.data.common.cdi.DirectoryRepository;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.build.compatible.spi.Parameters;
import jakarta.enterprise.inject.build.compatible.spi.SyntheticBeanCreator;

/**
* Creates beans for repositories for which the entity class has the PersonEntity annotation.
*/
public class PersonBeanCreator implements SyntheticBeanCreator<Object> {
public class BeanCreator implements SyntheticBeanCreator<Object> {

private static final Logger log = Logger.getLogger(PersonBeanCreator.class.getCanonicalName());
private static final Logger log = Logger.getLogger(BeanCreator.class.getCanonicalName());

@Override
public Object create(Instance<Object> instance, Parameters parameters) {
String provider = parameters.get("provider", String.class);
if (provider == Directory.PERSON_PROVIDER) {
log.info("Creating repository for " + instance + ", provider: " + provider);
return new DirectoryRepository();
} else {
log.info("Bean creator does not support creating " + instance + " for provider " + provider);
Class<?> provider = parameters.get("impl", Class.class);

try {
return provider.getConstructor().newInstance();
} catch (Exception e) {
log.warning("Error while constructing implementation of repository: " + e.getMessage());
return null;
}
}
Expand Down
Loading

0 comments on commit 76b999e

Please sign in to comment.