From 0e87790146aff0ab2b3234a736db18844d48d4c3 Mon Sep 17 00:00:00 2001 From: Thierry BOUVET Date: Wed, 30 Sep 2015 11:49:28 +0200 Subject: [PATCH] Create a DomainRegistry to access to domain objects (repository, factory, service, policy) --- core/pom.xml | 6 + .../internal/registry/DomainRegistyIT.java | 162 +++++++ .../registry/fixtures/domain/Client.java | 26 ++ .../registry/fixtures/domain/Composite.java | 18 + .../registry/fixtures/domain/Product.java | 18 + .../fixtures/policy/PolicyQualifier.java | 26 ++ .../fixtures/policy/RebatePolicy.java | 24 ++ .../fixtures/policy/RebatePolicyInternal.java | 26 ++ .../RebatePolicyInternalWithQualifier.java | 27 ++ .../registry/fixtures/service/MyService.java | 21 + .../fixtures/service/MyServiceInternal.java | 19 + .../MyServiceInternalWithQualiferNamed.java | 22 + .../fixtures/service/RebateService.java | 21 + .../service/RebateServiceInternal.java | 21 + .../RebateServiceInternalWithQualifier.java | 22 + ...ServiceInternalWithQualifierComposite.java | 21 + ...bateServiceInternalWithQualifierNamed.java | 25 ++ .../fixtures/service/ServiceQualifier.java | 27 ++ .../registry/repository/ClientRepository.java | 47 ++ .../registry/repository/JpaQualifier.java | 27 ++ .../business/internal/BusinessModule.java | 7 +- .../internal/registry/DomainRegistryImpl.java | 210 +++++++++ .../registry/DomainRegistryImplTest.java | 407 ++++++++++++++++++ .../business/api/domain/DomainRegistry.java | 215 +++++++++ 24 files changed, 1443 insertions(+), 2 deletions(-) create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/DomainRegistyIT.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/fixtures/domain/Client.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/fixtures/domain/Composite.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/fixtures/domain/Product.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/fixtures/policy/PolicyQualifier.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/fixtures/policy/RebatePolicy.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/fixtures/policy/RebatePolicyInternal.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/fixtures/policy/RebatePolicyInternalWithQualifier.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/MyService.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/MyServiceInternal.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/MyServiceInternalWithQualiferNamed.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateService.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateServiceInternal.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateServiceInternalWithQualifier.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateServiceInternalWithQualifierComposite.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateServiceInternalWithQualifierNamed.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/ServiceQualifier.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/repository/ClientRepository.java create mode 100644 core/src/it/java/org/seedstack/business/internal/registry/repository/JpaQualifier.java create mode 100644 core/src/main/java/org/seedstack/business/internal/registry/DomainRegistryImpl.java create mode 100644 core/src/test/java/org/seedstack/business/internal/registry/DomainRegistryImplTest.java create mode 100644 specs/src/main/java/org/seedstack/business/api/domain/DomainRegistry.java diff --git a/core/pom.xml b/core/pom.xml index 1c87bdbe..0256405e 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -48,6 +48,12 @@ 0.7.3 + + org.jmockit + jmockit + 1.19 + test + org.easytesting fest-reflect diff --git a/core/src/it/java/org/seedstack/business/internal/registry/DomainRegistyIT.java b/core/src/it/java/org/seedstack/business/internal/registry/DomainRegistyIT.java new file mode 100644 index 00000000..8f895e52 --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/DomainRegistyIT.java @@ -0,0 +1,162 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry; + +import javax.inject.Inject; + +import org.assertj.core.api.Assertions; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.seedstack.business.api.Service; +import org.seedstack.business.api.domain.DomainPolicy; +import org.seedstack.business.api.domain.DomainRegistry; +import org.seedstack.business.api.domain.Factory; +import org.seedstack.business.api.domain.Repository; +import org.seedstack.business.internal.registry.fixtures.domain.Client; +import org.seedstack.business.internal.registry.fixtures.domain.Composite; +import org.seedstack.business.internal.registry.fixtures.domain.Product; +import org.seedstack.business.internal.registry.fixtures.policy.PolicyQualifier; +import org.seedstack.business.internal.registry.fixtures.policy.RebatePolicy; +import org.seedstack.business.internal.registry.fixtures.policy.RebatePolicyInternalWithQualifier; +import org.seedstack.business.internal.registry.fixtures.service.MyService; +import org.seedstack.business.internal.registry.fixtures.service.MyServiceInternal; +import org.seedstack.business.internal.registry.fixtures.service.MyServiceInternalWithQualiferNamed; +import org.seedstack.business.internal.registry.fixtures.service.RebateService; +import org.seedstack.business.internal.registry.fixtures.service.RebateServiceInternal; +import org.seedstack.business.internal.registry.fixtures.service.RebateServiceInternalWithQualifierComposite; +import org.seedstack.business.internal.registry.fixtures.service.RebateServiceInternalWithQualifierNamed; +import org.seedstack.business.internal.registry.fixtures.service.ServiceQualifier; +import org.seedstack.business.internal.registry.repository.ClientRepository; +import org.seedstack.business.internal.registry.repository.JpaQualifier; +import org.seedstack.seed.core.api.TypeOf; +import org.seedstack.seed.it.SeedITRunner; + + +/** + * Integration test for {@link DomainRegistry}. + * @author thierry.bouvet@mpsa.com + * + */ +@RunWith(SeedITRunner.class) +public class DomainRegistyIT { + + @Inject + DomainRegistry domainRegistry; + + /** + * Test to get a {@link Service} from the {@link DomainRegistry}. + */ + @Test + public void testServiceBase() { + MyService service = this.domainRegistry.getService(MyService.class); + Assertions.assertThat(service).isInstanceOf(MyServiceInternal.class); + } + + /** + * Test to get a {@link Service} from the {@link DomainRegistry}. + */ + @Test + public void testService() { + TypeOf> type = new TypeOf>(){}; + + RebateService service = this.domainRegistry.getService(type); + Assertions.assertThat(service).isInstanceOf(RebateServiceInternal.class); + } + + /** + * Test to get a {@link Service} from the {@link DomainRegistry}. + */ + @Test + public void testServiceWithStringQualifierFromTypeOf() { + TypeOf> type = new TypeOf>(){}; + + RebateService service = this.domainRegistry.getService(type,"Dummy"); + Assertions.assertThat(service).isInstanceOf(RebateServiceInternalWithQualifierNamed.class); + } + + /** + * Test to get a {@link Service} from the {@link DomainRegistry}. + */ + @Test + public void testServiceWithStringQualifier() { + MyService service = this.domainRegistry.getService(MyService.class,"Dummy"); + Assertions.assertThat(service).isInstanceOf(MyServiceInternalWithQualiferNamed.class); + } + + /** + * Test to get a {@link Service} with a qualifier from the {@link DomainRegistry}. + */ + @Test + public void testServiceWithQualifierAndComposite() { + TypeOf>> type = new TypeOf>>(){}; + RebateService> service = this.domainRegistry.getService(type,ServiceQualifier.class); + Assertions.assertThat(service).isInstanceOf(RebateServiceInternalWithQualifierComposite.class); + + } + + /** + * Test to get a {@link DomainPolicy} from the {@link DomainRegistry}. + */ + @Test + public void testPolicy() { + RebatePolicy policy = this.domainRegistry.getPolicy(RebatePolicy.class); + final int rebateExpected = 10; + Assertions.assertThat(policy.calculateRebate(new Product(),10000)).isEqualTo(rebateExpected); + } + + /** + * Test to get a {@link DomainPolicy} with a qualifier from the {@link DomainRegistry}. + */ + @Test + public void testPolicyWithQualifier() { + RebatePolicy policy = this.domainRegistry.getPolicy(RebatePolicy.class,PolicyQualifier.class); + Assertions.assertThat(policy).isInstanceOf(RebatePolicyInternalWithQualifier.class); + } + + /** + * Test to get a {@link Repository} from the {@link DomainRegistry}. + */ + @Test + public void testRepository() { + Repository repository = this.domainRegistry.getRepository(Client.class, Long.class, JpaQualifier.class); + Assertions.assertThat(repository).isInstanceOf(ClientRepository.class); + } + + /** + * Test to get a {@link Repository} from the {@link DomainRegistry}. + */ + @Test + public void testRepositoryFromTypeOf() { + TypeOf> typeOf = new TypeOf>(){}; + Repository repository = this.domainRegistry.getRepository(typeOf, JpaQualifier.class); + Assertions.assertThat(repository).isInstanceOf(ClientRepository.class); + } + + /** + * Test to get a {@link Factory} from the {@link DomainRegistry}. + */ + @Test + public void testFactory() { + Factory factory = domainRegistry.getFactory(Client.class); + Assertions.assertThat(factory).isInstanceOf(Factory.class); + } + + /** + * Test to get a {@link Factory} from the {@link DomainRegistry}. + */ + @Test + public void testFactoryFromTypeOf() { + TypeOf> typeOf = new TypeOf>(){}; + + Factory factory = domainRegistry.getFactory(typeOf); + Assertions.assertThat(factory).isInstanceOf(Factory.class); + } + +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/fixtures/domain/Client.java b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/domain/Client.java new file mode 100644 index 00000000..f61447c6 --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/domain/Client.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.fixtures.domain; + +import org.seedstack.business.api.domain.BaseAggregateRoot; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */ +public class Client extends BaseAggregateRoot{ + + @Override + public Long getEntityId() { + return 1L; + } + +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/fixtures/domain/Composite.java b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/domain/Composite.java new file mode 100644 index 00000000..19b8642a --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/domain/Composite.java @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.fixtures.domain; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */public class Composite { + +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/fixtures/domain/Product.java b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/domain/Product.java new file mode 100644 index 00000000..0cff5af3 --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/domain/Product.java @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.fixtures.domain; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */public class Product { + +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/fixtures/policy/PolicyQualifier.java b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/policy/PolicyQualifier.java new file mode 100644 index 00000000..c4f2519b --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/policy/PolicyQualifier.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.fixtures.policy; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import javax.inject.Qualifier; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */@Documented +@Retention(RetentionPolicy.RUNTIME) +@Qualifier +public @interface PolicyQualifier { +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/fixtures/policy/RebatePolicy.java b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/policy/RebatePolicy.java new file mode 100644 index 00000000..34497228 --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/policy/RebatePolicy.java @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.fixtures.policy; + +import org.seedstack.business.api.domain.DomainPolicy; +import org.seedstack.business.internal.registry.fixtures.domain.Product; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */ +@DomainPolicy +public interface RebatePolicy { + + public float calculateRebate(Product product, float quantity); +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/fixtures/policy/RebatePolicyInternal.java b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/policy/RebatePolicyInternal.java new file mode 100644 index 00000000..06bb1fc4 --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/policy/RebatePolicyInternal.java @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.fixtures.policy; + +import org.seedstack.business.internal.registry.fixtures.domain.Product; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */ +public class RebatePolicyInternal implements RebatePolicy{ + + @Override + public float calculateRebate(Product product, float quantity) { + return quantity / 1000; + } + +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/fixtures/policy/RebatePolicyInternalWithQualifier.java b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/policy/RebatePolicyInternalWithQualifier.java new file mode 100644 index 00000000..3460e3d3 --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/policy/RebatePolicyInternalWithQualifier.java @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.fixtures.policy; + +import org.seedstack.business.internal.registry.fixtures.domain.Product; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */ +@PolicyQualifier +public class RebatePolicyInternalWithQualifier implements RebatePolicy{ + + @Override + public float calculateRebate(Product product, float quantity) { + return quantity / 10; + } + +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/MyService.java b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/MyService.java new file mode 100644 index 00000000..13827a88 --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/MyService.java @@ -0,0 +1,21 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.fixtures.service; + +import org.seedstack.business.api.Service; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */ +@Service +public interface MyService { +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/MyServiceInternal.java b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/MyServiceInternal.java new file mode 100644 index 00000000..ebda3ba6 --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/MyServiceInternal.java @@ -0,0 +1,19 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.fixtures.service; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */ +public class MyServiceInternal implements MyService { + +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/MyServiceInternalWithQualiferNamed.java b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/MyServiceInternalWithQualiferNamed.java new file mode 100644 index 00000000..546d21d7 --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/MyServiceInternalWithQualiferNamed.java @@ -0,0 +1,22 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.fixtures.service; + +import javax.inject.Named; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */ +@Named("Dummy") +public class MyServiceInternalWithQualiferNamed implements MyService { + +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateService.java b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateService.java new file mode 100644 index 00000000..1a300673 --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateService.java @@ -0,0 +1,21 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.fixtures.service; + +import org.seedstack.business.api.Service; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */ +@Service +public interface RebateService { +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateServiceInternal.java b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateServiceInternal.java new file mode 100644 index 00000000..693a184c --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateServiceInternal.java @@ -0,0 +1,21 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.fixtures.service; + +import org.seedstack.business.internal.registry.fixtures.domain.Client; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */ +public class RebateServiceInternal implements RebateService{ + +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateServiceInternalWithQualifier.java b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateServiceInternalWithQualifier.java new file mode 100644 index 00000000..090cacb8 --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateServiceInternalWithQualifier.java @@ -0,0 +1,22 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.fixtures.service; + +import org.seedstack.business.internal.registry.fixtures.domain.Client; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */ +@ServiceQualifier +public class RebateServiceInternalWithQualifier implements RebateService{ + +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateServiceInternalWithQualifierComposite.java b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateServiceInternalWithQualifierComposite.java new file mode 100644 index 00000000..fd907d88 --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateServiceInternalWithQualifierComposite.java @@ -0,0 +1,21 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.fixtures.service; + +import org.seedstack.business.internal.registry.fixtures.domain.Composite; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */ +@ServiceQualifier +public class RebateServiceInternalWithQualifierComposite implements RebateService>{ +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateServiceInternalWithQualifierNamed.java b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateServiceInternalWithQualifierNamed.java new file mode 100644 index 00000000..9f409cc7 --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/RebateServiceInternalWithQualifierNamed.java @@ -0,0 +1,25 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.fixtures.service; + + +import javax.inject.Named; + +import org.seedstack.business.internal.registry.fixtures.domain.Client; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */ +@Named("Dummy") +public class RebateServiceInternalWithQualifierNamed implements RebateService{ + +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/ServiceQualifier.java b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/ServiceQualifier.java new file mode 100644 index 00000000..f5a1f084 --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/fixtures/service/ServiceQualifier.java @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.fixtures.service; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import javax.inject.Qualifier; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Qualifier +public @interface ServiceQualifier { +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/repository/ClientRepository.java b/core/src/it/java/org/seedstack/business/internal/registry/repository/ClientRepository.java new file mode 100644 index 00000000..578e06ba --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/repository/ClientRepository.java @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.repository; + +import org.seedstack.business.api.domain.AggregateRoot; +import org.seedstack.business.api.domain.BaseRepository; +import org.seedstack.business.spi.GenericImplementation; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */ +@GenericImplementation +@JpaQualifier +public class ClientRepository, K> extends BaseRepository{ + + @Override + protected A doLoad(K id) { + return null; + } + + @Override + protected void doDelete(K id) { + } + + @Override + protected void doDelete(A aggregate) { + } + + @Override + protected void doPersist(A aggregate) { + } + + @Override + protected A doSave(A aggregate) { + return null; + } + +} diff --git a/core/src/it/java/org/seedstack/business/internal/registry/repository/JpaQualifier.java b/core/src/it/java/org/seedstack/business/internal/registry/repository/JpaQualifier.java new file mode 100644 index 00000000..e3e35409 --- /dev/null +++ b/core/src/it/java/org/seedstack/business/internal/registry/repository/JpaQualifier.java @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry.repository; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import javax.inject.Qualifier; + +/** + * Dummy class. + * @author thierry.bouvet@mpsa.com + * + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Qualifier +public @interface JpaQualifier { +} diff --git a/core/src/main/java/org/seedstack/business/internal/BusinessModule.java b/core/src/main/java/org/seedstack/business/internal/BusinessModule.java index a9120eb6..86f16e99 100644 --- a/core/src/main/java/org/seedstack/business/internal/BusinessModule.java +++ b/core/src/main/java/org/seedstack/business/internal/BusinessModule.java @@ -11,10 +11,13 @@ import com.google.inject.AbstractModule; import com.google.inject.Key; + +import org.seedstack.business.api.domain.DomainRegistry; import org.seedstack.business.api.interfaces.assembler.FluentAssembler; import org.seedstack.business.internal.assembler.dsl.FluentAssemblerImpl; import org.seedstack.business.internal.assembler.dsl.InternalRegistry; import org.seedstack.business.internal.assembler.dsl.InternalRegistryInternal; +import org.seedstack.business.internal.registry.DomainRegistryImpl; import org.seedstack.business.internal.strategy.api.BindingStrategy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,13 +62,13 @@ public BusinessModule(Collection> assemblersClasses, Map, Class< this.bindingStrategies = bindingStrategies; } - @SuppressWarnings("deprecation") @Override protected void configure() { bind(FluentAssembler.class).to(FluentAssemblerImpl.class); bind(InternalRegistry.class).to(InternalRegistryInternal.class); + bind(DomainRegistry.class).to(DomainRegistryImpl.class); - for (Entry, Class> binding : bindings.entrySet()) { + for (Entry, Class> binding : bindings.entrySet()) { logger.trace("Binding {} to {}.", binding.getKey(), binding.getValue().getSimpleName()); bind(binding.getKey()).to((Class) binding.getValue()); } diff --git a/core/src/main/java/org/seedstack/business/internal/registry/DomainRegistryImpl.java b/core/src/main/java/org/seedstack/business/internal/registry/DomainRegistryImpl.java new file mode 100644 index 00000000..4cd5fc27 --- /dev/null +++ b/core/src/main/java/org/seedstack/business/internal/registry/DomainRegistryImpl.java @@ -0,0 +1,210 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import javax.inject.Inject; +import javax.inject.Qualifier; + +import org.seedstack.business.api.Producible; +import org.seedstack.business.api.domain.AggregateRoot; +import org.seedstack.business.api.domain.DomainObject; +import org.seedstack.business.api.domain.DomainRegistry; +import org.seedstack.business.api.domain.Factory; +import org.seedstack.business.api.domain.Repository; +import org.seedstack.seed.core.api.TypeOf; + +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.name.Names; +import com.google.inject.util.Types; + +/** + * Registry to access to all domain objects (repository, factory, service, policy). + * + * @author thierry.bouvet@mpsa.com + * + */ +public class DomainRegistryImpl implements DomainRegistry { + + @Inject + private Injector injector; + + @Override + public , A extends AggregateRoot, K> T getRepository(TypeOf typeOf, + Class qualifier) { + return getInstance(getKey(typeOf.getType(), qualifier)); + } + + @Override + public , A extends AggregateRoot, K> T getRepository(TypeOf typeOf, + String qualifier) { + return getInstance(getKey(typeOf.getType(), qualifier)); + } + + @Override + public , K> Repository getRepository(Class aggregateRoot, Class key, + Class qualifier) { + return getInstance(getKey(getType(Repository.class,aggregateRoot, key),qualifier)); + } + + @Override + public , K> Repository getRepository(Class aggregateRoot, Class key, + String qualifier) { + return getInstance(getKey(getType(Repository.class,aggregateRoot, key),qualifier)); + } + + @Override + public ,A extends DomainObject & Producible> T getFactory(TypeOf typeOf) { + return getInstance(getKey(typeOf.getType())); + } + + @Override + public ,A extends DomainObject & Producible> T getFactory(TypeOf typeOf, + Class qualifier) { + return getInstance(getKey(typeOf.getType(), qualifier)); + } + + @Override + public , A extends DomainObject & Producible> T getFactory(TypeOf typeOf, String qualifier) { + return getInstance(getKey(typeOf.getType(), qualifier)); + } + + @Override + public Factory getFactory(Class aggregateRoot) { + return getInstance(getKey(getType(Factory.class, aggregateRoot))); + } + + @Override + public Factory getFactory(Class aggregateRoot, + Class qualifier) { + return getInstance(getKey(getType(Factory.class, aggregateRoot), qualifier)); + } + + @Override + public Factory getFactory(Class aggregateRoot, String qualifier) { + return getInstance(getKey(getType(Factory.class, aggregateRoot), qualifier)); + } + + @Override + public T getService(TypeOf typeOf) { + return getInstance(getKey(typeOf.getType())); + } + + @Override + public T getService(TypeOf typeOf, Class qualifier) { + return getInstance(getKey(typeOf.getType(), qualifier)); + } + + @Override + public T getService(TypeOf typeOf, String qualifier) { + return getInstance(getKey(typeOf.getType(), qualifier)); + } + + @Override + public T getService(Class rawType) { + return getInstance(getKey(getType(rawType))); + } + + @Override + public T getService(Class rawType, Class qualifier) { + return getInstance(getKey(rawType,qualifier)); + } + + + @Override + public T getService(Class rawType, String qualifier) { + return getInstance(getKey(getType(rawType),qualifier)); + } + + @Override + public T getPolicy(Class rawType) { + return getInstance(getKey(getType(rawType))); + } + + @Override + public T getPolicy(Class rawType, Class qualifier) { + return getInstance(getKey(rawType,qualifier)); + } + + @Override + public T getPolicy(Class rawType, String qualifier) { + return getInstance(getKey(getType(rawType),qualifier)); + } + + @Override + public T getPolicy(TypeOf typeOf) { + return getInstance(getKey(typeOf.getType())); + } + + @Override + public T getPolicy(TypeOf typeOf, Class qualifier) { + return getInstance(getKey(typeOf.getType(), qualifier)); + } + + @Override + public T getPolicy(TypeOf typeOf, String qualifier) { + return getInstance(getKey(typeOf.getType(), qualifier)); + } + + /** + * Get an instance for a defined {@link Key}. + * @param key key to find in {@link Injector} + * @return an instance bound to the {@link Key}. + */ + @SuppressWarnings("unchecked") + private T getInstance(Key key) { + return (T) injector.getInstance(key); + } + + /** + * Get a {@link Key} for a defined class and a qualifier. + * @param rawType class + * @param qualifier Optional Key {@link Qualifier}. + * @return the {@link Key}. + */ + private Key getKey(Type type, Class qualifier) { + return Key.get(type,qualifier); + } + + /** + * Get a {@link Key} for a defined class and a qualifier. + * @param rawType class + * @return the {@link Key}. + */ + private Key getKey(Type type, String qualifier) { + return Key.get(type, Names.named(qualifier)); + } + + /** + * Get a {@link Key} for a defined class and a qualifier. + * @param rawType class + * @param qualifier Optional Key {@link Qualifier}. + * @return the {@link Key}. + */ + private Key getKey(Type type) { + return Key.get(type); + } + + /** + * Get a {@link Type} for a defined class. + * @param rawType class + * @param typeArguments parameterized types + * @return the {@link Type} + */ + private Type getType(Type rawType, Type... typeArguments) { + if (typeArguments.length == 0) { + return rawType; + } + return Types.newParameterizedType(rawType, typeArguments); + } +} diff --git a/core/src/test/java/org/seedstack/business/internal/registry/DomainRegistryImplTest.java b/core/src/test/java/org/seedstack/business/internal/registry/DomainRegistryImplTest.java new file mode 100644 index 00000000..4f23e01d --- /dev/null +++ b/core/src/test/java/org/seedstack/business/internal/registry/DomainRegistryImplTest.java @@ -0,0 +1,407 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.internal.registry; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.assertj.core.api.Assertions; +import org.junit.Test; +import org.seedstack.business.api.domain.AggregateRoot; +import org.seedstack.business.api.domain.DomainElement; +import org.seedstack.business.api.domain.DomainRegistry; +import org.seedstack.business.api.domain.Factory; +import org.seedstack.business.api.domain.Repository; +import org.seedstack.seed.core.api.TypeOf; + +import com.google.inject.BindingAnnotation; +import com.google.inject.Injector; +import com.google.inject.Key; + +import mockit.Deencapsulation; +import mockit.Mocked; +import mockit.StrictExpectations; + +/** + * Unit test for {@link DomainRegistryImpl} + * @author thierry.bouvet@mpsa.com + * + */ +public class DomainRegistryImplTest { + + @Mocked private Injector injector; + + @DomainElement + @Retention(RetentionPolicy.RUNTIME) + @Target({ ElementType.TYPE}) + @BindingAnnotation + private @interface MockedAnnotation { + } + + private interface MockedService {}; + + private interface MockedServiceParameterized {}; + + private interface MockedPolicy {}; + + private interface MockedPolicyParameterized {}; + + + /** + * Create a {@link DomainRegistry} with a mocker {@link Injector}. + * @return a new {@link DomainRegistry} + */ + private DomainRegistry createDomainRegistry() { + DomainRegistry domainRegistry = new DomainRegistryImpl(); + Deencapsulation.setField(domainRegistry, "injector", injector); + return domainRegistry; + } + + @SuppressWarnings({ "unchecked" }) + @Test + public void testGetRepositoryTypeOfOfTClassOfQextendsAnnotation(final @Mocked Repository, Long> repository) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key, Long>>)any); + result=repository; + } + }; + + Assertions.assertThat(domainRegistry.getRepository(new TypeOf, Long>>() { + }, MockedAnnotation.class)).isEqualTo(repository); + } + + @SuppressWarnings({ "unchecked" }) + @Test + public void testGetRepositoryTypeOfOfTString(final @Mocked Repository, Long> repository) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key, Long>>)any); + result=repository; + } + }; + + Assertions.assertThat(domainRegistry.getRepository(new TypeOf, Long>>() { + }, "dummyAnnotation")).isEqualTo(repository); + } + + @SuppressWarnings("unchecked") + @Test + public void testGetRepositoryClassOfAClassOfKClassOfQextendsAnnotation(final @Mocked Repository, Long> repository) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key, Long>>)any); + result=repository; + } + }; + + Assertions.assertThat(domainRegistry.getRepository(AggregateRoot.class, Long.class,MockedAnnotation.class)).isEqualTo(repository); + } + + @SuppressWarnings("unchecked") + @Test + public void testGetRepositoryClassOfAClassOfKString(final @Mocked Repository, Long> repository) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key, Long>>)any); + result=repository; + } + }; + + Assertions.assertThat(domainRegistry.getRepository(AggregateRoot.class, Long.class,"dummyAnnotation")).isEqualTo(repository); + } + + @SuppressWarnings({ "unchecked" }) + @Test + public void testGetFactoryTypeOfOfT(final @Mocked Factory factory) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key>>)any); + result=factory; + } + }; + + Assertions.assertThat(domainRegistry.getFactory(new TypeOf>>() { + })).isEqualTo(factory); + } + + @SuppressWarnings({ "unchecked" }) + @Test + public void testGetFactoryTypeOfOfTClassOfQextendsAnnotation(final @Mocked Factory factory) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key>>)any); + result=factory; + } + }; + + Assertions.assertThat(domainRegistry.getFactory(new TypeOf>>() { + }, MockedAnnotation.class)).isEqualTo(factory); + } + + @SuppressWarnings({ "unchecked" }) + @Test + public void testGetFactoryTypeOfOfTString(final @Mocked Factory factory) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key>>)any); + result=factory; + } + }; + + Assertions.assertThat(domainRegistry.getFactory(new TypeOf>>() { + }, "dummyAnnotation")).isEqualTo(factory); + } + + @SuppressWarnings("unchecked") + @Test + public void testGetFactoryClassOfT(final @Mocked Factory factory) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key>>)any); + result=factory; + } + }; + + Assertions.assertThat(domainRegistry.getFactory(AggregateRoot.class)).isEqualTo(factory); + } + + @SuppressWarnings("unchecked") + @Test + public void testGetFactoryClassOfTClassOfQextendsAnnotation(final @Mocked Factory factory) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key>>)any); + result=factory; + } + }; + + Assertions.assertThat(domainRegistry.getFactory(AggregateRoot.class,MockedAnnotation.class)).isEqualTo(factory); + } + + @SuppressWarnings("unchecked") + @Test + public void testGetFactoryClassOfTString(final @Mocked Factory factory) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key>>)any); + result=factory; + } + }; + + Assertions.assertThat(domainRegistry.getFactory(AggregateRoot.class,"dummyAnnotation")).isEqualTo(factory); + } + + @SuppressWarnings({ "unchecked" }) + @Test + public void testGetServiceTypeOfOfT(final @Mocked MockedServiceParameterized service) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key>)any); + result=service; + } + }; + + Assertions.assertThat(domainRegistry.getService(new TypeOf>() {})).isEqualTo(service); + } + + @SuppressWarnings({ "unchecked" }) + @Test + public void testGetServiceTypeOfOfTClassOfQextendsAnnotation(final @Mocked MockedServiceParameterized service) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key>)any); + result=service; + } + }; + + Assertions.assertThat(domainRegistry.getService(new TypeOf>() {},MockedAnnotation.class)).isEqualTo(service); + } + + @SuppressWarnings({ "unchecked" }) + @Test + public void testGetServiceTypeOfOfTString(final @Mocked MockedServiceParameterized service) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key>)any); + result=service; + } + }; + + Assertions.assertThat(domainRegistry.getService(new TypeOf>() {},"dummyAnnotation")).isEqualTo(service); + } + + @SuppressWarnings("unchecked") + @Test + public void testGetServiceClassOfT(final @Mocked MockedService service) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key)any); + result=service; + } + }; + + Assertions.assertThat(domainRegistry.getService(MockedService.class)).isEqualTo(service); + } + + @SuppressWarnings("unchecked") + @Test + public void testGetServiceClassOfTClassOfQextendsAnnotation(final @Mocked MockedService service) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key)any); + result=service; + } + }; + + Assertions.assertThat(domainRegistry.getService(MockedService.class,MockedAnnotation.class)).isEqualTo(service); + } + + @SuppressWarnings("unchecked") + @Test + public void testGetServiceClassOfTString(final @Mocked MockedService service) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key)any); + result=service; + } + }; + + Assertions.assertThat(domainRegistry.getService(MockedService.class,"dummyAnnotation")).isEqualTo(service); + } + + @SuppressWarnings("unchecked") + @Test + public void testGetPolicyClassOfT(final @Mocked MockedPolicy policy) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key)any); + result=policy; + } + }; + + Assertions.assertThat(domainRegistry.getPolicy(MockedPolicy.class)).isEqualTo(policy); + } + + @SuppressWarnings("unchecked") + @Test + public void testGetPolicyClassOfTClassOfQextendsAnnotation(final @Mocked MockedPolicy policy) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key)any); + result=policy; + } + }; + + Assertions.assertThat(domainRegistry.getPolicy(MockedPolicy.class,MockedAnnotation.class)).isEqualTo(policy); + } + + @SuppressWarnings("unchecked") + @Test + public void testGetPolicyClassOfTString(final @Mocked MockedPolicy policy) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key)any); + result=policy; + } + }; + + Assertions.assertThat(domainRegistry.getPolicy(MockedPolicy.class,"dummyAnnotation")).isEqualTo(policy); + } + + @SuppressWarnings({ "unchecked" }) + @Test + public void testGetPolicyTypeOfOfT(final @Mocked MockedPolicyParameterized policy) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key>)any); + result=policy; + } + }; + + Assertions.assertThat(domainRegistry.getPolicy(new TypeOf>() { + })).isEqualTo(policy); + } + + @SuppressWarnings({ "unchecked" }) + @Test + public void testGetPolicyTypeOfOfTClassOfQextendsAnnotation(final @Mocked MockedPolicyParameterized policy) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key>)any); + result=policy; + } + }; + + Assertions.assertThat(domainRegistry.getPolicy(new TypeOf>() { + },MockedAnnotation.class)).isEqualTo(policy); + } + + @SuppressWarnings({ "unchecked" }) + @Test + public void testGetPolicyTypeOfOfTString(final @Mocked MockedPolicyParameterized policy) { + DomainRegistry domainRegistry = createDomainRegistry(); + + new StrictExpectations() { + { + injector.getInstance((Key>)any); + result=policy; + } + }; + + Assertions.assertThat(domainRegistry.getPolicy(new TypeOf>() { + },"dummyAnnotation")).isEqualTo(policy); + } + +} diff --git a/specs/src/main/java/org/seedstack/business/api/domain/DomainRegistry.java b/specs/src/main/java/org/seedstack/business/api/domain/DomainRegistry.java new file mode 100644 index 00000000..7d9dd038 --- /dev/null +++ b/specs/src/main/java/org/seedstack/business/api/domain/DomainRegistry.java @@ -0,0 +1,215 @@ +/** + * Copyright (c) 2013-2015 by The SeedStack authors. All rights reserved. + * + * This file is part of SeedStack, An enterprise-oriented full development stack. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.seedstack.business.api.domain; + +import java.lang.annotation.Annotation; + +import org.seedstack.business.api.Producible; +import org.seedstack.business.api.Service; +import org.seedstack.seed.core.api.TypeOf; + +/** + * Registry to access to all domain objects. + * + *
+ * To use it, just inject it :
+ * 
+ * {@code @Inject}
+ * DomainRegistry domainRegistry
+ * 
+ * + *
+ * Example for a class without generic parameter:
+ * 
+ * 	   MyPolicy policy = domainRegistry.getPolicy(MyPolicy.class,"qualifier");
+ * 
+ * 
+ * Example for a class with generic parameter:
+ * 
+ * 	   AnotherPolicy<MyClient<Long>> policy = domainRegistry.getPolicy(new TypeOf<AnotherPolicy<MyClient<Long>>>(){},"qualifier");
+ * 
+ * 
+ * @author thierry.bouvet@mpsa.com + * + */ +public interface DomainRegistry { + + /** + * Get a {@link Repository} from the domain. + * @param typeOf the {@link TypeOf} to define all generic pattern. + * @param qualifier repository qualifier + * @return a {@link Repository} found in the domain. + */ + ,A extends AggregateRoot,K> T getRepository(TypeOf typeOf, Class qualifier); + + /** + * Get a {@link Repository} from the domain. + * @param typeOf the {@link TypeOf} to define all generic pattern. + * @param qualifier repository qualifier + * @return a {@link Repository} found in the domain. + */ + ,A extends AggregateRoot,K> T getRepository(TypeOf typeOf, String qualifier); + + /** + * Get the {@link Repository} for an aggregate root and a qualifier. + * @param aggregateRoot the aggregate root linked to the repository. + * @param key the aggregate root's key. + * @param qualifier repository qualifier (example: JPA, ...). + * @return an instance of the {@link Repository} + */ +
, K> Repository getRepository(Class aggregateRoot, Class key, Class qualifier); + + /** + * Get the {@link Repository} for an aggregate root and a qualifier. + * @param aggregateRoot the aggregate root linked to the repository. + * @param key the aggregate root's key. + * @param qualifier repository qualifier (example: JPA, ...). + * @return an instance of the {@link Repository} + */ + , K> Repository getRepository(Class aggregateRoot, Class key, String qualifier); + + /** + * Get the {@link Factory} for an aggregate root. + * @param aggregateRoot the aggregate root linked to the factoy. + * @return an instance of the {@link Factory} + */ + Factory getFactory(Class aggregateRoot); + + /** + * Get the {@link Factory} with a qualifier for an aggregate root. + * @param aggregateRoot the aggregate root linked to the factoy. + * @param qualifier factory qualifier. + * @return an instance of the {@link Factory} + */ + Factory getFactory(Class aggregateRoot, Class qualifier); + + /** + * Get the {@link Factory} with a qualifier for an aggregate root. + * @param aggregateRoot the aggregate root linked to the factoy. + * @param qualifier factory qualifier. + * @return an instance of the {@link Factory} + */ + Factory getFactory(Class aggregateRoot, String qualifier); + + /** + * Get a {@link Factory} from the domain. + * @param typeOf the {@link TypeOf} to define all generic pattern. + * @return a {@link Factory} found in the domain. + */ + ,A extends DomainObject & Producible> T getFactory(TypeOf typeOf); + + /** + * Get a {@link Factory} from the domain. + * @param typeOf the {@link TypeOf} to define all generic pattern. + * @param qualifier factory qualifier + * @return a {@link Factory} found in the domain. + */ + ,A extends DomainObject & Producible> T getFactory(TypeOf typeOf, Class qualifier); + + /** + * Get a {@link Factory} from the domain. + * @param typeOf the {@link TypeOf} to define all generic pattern. + * @param qualifier factory qualifier + * @return a {@link Factory} found in the domain. + */ + ,A extends DomainObject & Producible> T getFactory(TypeOf typeOf, String qualifier); + + /** + * Get a {@link Service} from the domain. + * @param rawType the service class. + * @return a {@link Service} found in the domain. + */ + T getService(Class rawType); + + /** + * Get a {@link Service} with a qualifier from the domain. + * @param rawType the service class. + * @param qualifier service qualifier + * @return a {@link Service} found in the domain. + */ + T getService(Class rawType, Class qualifier); + + /** + * Get a {@link Service} with a qualifier from the domain. + * @param rawType the service class. + * @param qualifier service qualifier + * @return a {@link Service} found in the domain. + */ + T getService(Class rawType, String qualifier); + + /** + * Get a {@link Service} from the domain. + * @param typeOf the {@link TypeOf} to define all generic pattern. + * @return a {@link Service} found in the domain. + */ + T getService(TypeOf typeOf); + + /** + * Get a {@link Service} from the domain. + * @param typeOf the {@link TypeOf} to define all generic pattern. + * @param qualifier service qualifier + * @return a {@link Service} found in the domain. + */ + T getService(TypeOf typeOf, Class qualifier); + + /** + * Get a {@link Service} from the domain. + * @param typeOf the {@link TypeOf} to define all generic pattern. + * @param qualifier service qualifier + * @return a {@link Service} found in the domain. + */ + T getService(TypeOf typeOf, String qualifier); + + /** + * Get a {@link DomainPolicy} from the domain. + * @param rawType the policy class. + * @return a {@link DomainPolicy} found in the domain. + */ + T getPolicy(Class rawType); + + /** + * Get a {@link DomainPolicy} with a qualifier from the domain. + * @param rawType the policy class. + * @param qualifier policy qualifier + * @return a {@link DomainPolicy} found in the domain. + */ + T getPolicy(Class rawType, Class qualifier); + + /** + * Get a {@link DomainPolicy} with a qualifier from the domain. + * @param rawType the policy class. + * @param qualifier policy qualifier + * @return a {@link DomainPolicy} found in the domain. + */ + T getPolicy(Class rawType, String qualifier); + + /** + * Get a {@link DomainPolicy} from the domain. + * @param typeOf the {@link TypeOf} to define all generic pattern. + * @return a {@link DomainPolicy} found in the domain. + */ + T getPolicy(TypeOf typeOf); + + /** + * Get a {@link DomainPolicy} from the domain. + * @param typeOf the {@link TypeOf} to define all generic pattern. + * @param qualifier policy qualifier + * @return a {@link DomainPolicy} found in the domain. + */ + T getPolicy(TypeOf typeOf, Class qualifier); + + /** + * Get a {@link DomainPolicy} from the domain. + * @param typeOf the {@link TypeOf} to define all generic pattern. + * @param qualifier policy qualifier + * @return a {@link DomainPolicy} found in the domain. + */ + T getPolicy(TypeOf typeOf, String qualifier); +}