Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete assembler DSL use cases #18

Merged
merged 1 commit into from
Apr 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* 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.assembler.dsl;

import com.google.common.collect.Lists;
import org.assertj.core.api.Assertions;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.seedstack.business.api.interfaces.assembler.FluentAssembler;
import org.seedstack.business.assembler.fixtures.book.BookDto;
import org.seedstack.business.assembler.fixtures.book.BookId;
import org.seedstack.business.assembler.fixtures.book.StoredBook;
import org.seedstack.seed.it.SeedITRunner;

import javax.inject.Inject;
import java.util.Date;
import java.util.List;

/**
* @author Pierre Thirouin <pierre.thirouin@ext.mpsa.com>
*/
@RunWith(SeedITRunner.class)
public class AggToDtoIT {

public static final String ALEXANDRE_DUMAS = "Alexandre Dumas";
public static final String THE_THREE_MUSKETEERS = "The Three Musketeers";
public static final Date PUBLISH_DATE = new Date();
public static final String THE_COUNT_OF_MONTE_CRISTO = "The Count of Monte Cristo";

@Inject
private FluentAssembler fluently;

@Test
public void test_Assemble_Aggregate_With_Value_Object_Id_To_Dto() {
StoredBook book = new StoredBook(new BookId(THE_THREE_MUSKETEERS, ALEXANDRE_DUMAS));
book.setEditor("unknown");
book.setPublishDate(PUBLISH_DATE);
BookDto dto = fluently.assemble().aggregate(book).to(BookDto.class);

Assertions.assertThat(dto.getAuthor()).isEqualTo(ALEXANDRE_DUMAS);
Assertions.assertThat(dto.getTitle()).isEqualTo(THE_THREE_MUSKETEERS);
Assertions.assertThat(dto.getPublishDate()).isEqualTo(PUBLISH_DATE);
}

@Test
@Ignore
public void test_Assemble_Aggregate_To_Dto_null_case() {
BookDto to = fluently.assemble().aggregate(null).to(BookDto.class);
Assertions.assertThat(to).isNull();
}

@Test
public void test_Assemble_Aggregates_To_Dtos() {
StoredBook book = new StoredBook(new BookId(THE_THREE_MUSKETEERS, ALEXANDRE_DUMAS));
book.setEditor("unknown");
book.setPublishDate(PUBLISH_DATE);

StoredBook book2 = new StoredBook(new BookId(THE_COUNT_OF_MONTE_CRISTO, ALEXANDRE_DUMAS));
book2.setEditor("other editor");
book2.setPublishDate(PUBLISH_DATE);

List<BookDto> dtos = fluently.assemble().aggregates(Lists.newArrayList(book, book2)).to(BookDto.class);

Assertions.assertThat(dtos).isNotNull();
Assertions.assertThat(dtos).isNotEmpty();
BookDto dto = dtos.get(0);
Assertions.assertThat(dto.getAuthor()).isEqualTo(ALEXANDRE_DUMAS);
Assertions.assertThat(dto.getTitle()).isEqualTo(THE_THREE_MUSKETEERS);
Assertions.assertThat(dto.getPublishDate()).isEqualTo(PUBLISH_DATE);
BookDto dto2 = dtos.get(1);
Assertions.assertThat(dto2.getAuthor()).isEqualTo(ALEXANDRE_DUMAS);
Assertions.assertThat(dto2.getTitle()).isEqualTo(THE_COUNT_OF_MONTE_CRISTO);
Assertions.assertThat(dto2.getPublishDate()).isEqualTo(PUBLISH_DATE);
}

@Test
public void test_Assemble_Aggregates_To_Dtos_null_case() {
List<BookDto> to = fluently.assemble().aggregates(null).to(BookDto.class);
Assertions.assertThat(to).isNotNull();
Assertions.assertThat(to).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import org.seedstack.business.api.interfaces.assembler.dsl.AggregateNotFoundException;
import org.seedstack.business.api.interfaces.assembler.dsl.Assemble;
import org.seedstack.business.api.interfaces.assembler.dsl.AssembleSecurely;
import org.seedstack.business.core.interfaces.assembler.dsl.fixture.Order;
import org.seedstack.business.core.interfaces.assembler.dsl.fixture.OrderDto;
import org.seedstack.business.core.interfaces.assembler.dsl.fixture.OrderFactory;
import org.seedstack.business.core.interfaces.assembler.dsl.fixture.OrderRepositoryInternal;
import org.seedstack.business.core.interfaces.assembler.dsl.fixture.customer.Order;
import org.seedstack.business.core.interfaces.assembler.dsl.fixture.customer.OrderDto;
import org.seedstack.business.core.interfaces.assembler.dsl.fixture.customer.OrderFactory;
import org.seedstack.business.core.interfaces.assembler.dsl.fixture.customer.OrderRepositoryInternal;
import org.seedstack.seed.it.SeedITRunner;

import javax.inject.Inject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.seedstack.business.api.domain.Repository;
import org.seedstack.business.api.interfaces.assembler.FluentAssembler;
import org.seedstack.business.api.interfaces.assembler.dsl.AggregateNotFoundException;
import org.seedstack.business.core.interfaces.assembler.dsl.fixture.*;
import org.seedstack.business.core.interfaces.assembler.dsl.fixture.customer.*;
import org.seedstack.seed.it.SeedITRunner;

import javax.inject.Inject;
Expand Down Expand Up @@ -48,7 +48,7 @@ public class AssemblerDslWithTupleIT {
public void testAssembleFromFactory() {
Recipe recipe = new Recipe("customer1", "luke", "order1", "light saber");

Pair<Order, Customer> orderCustomerPair = fluently.assemble().dto(recipe).<Pair<Order, Customer>>to(Order.class, Customer.class).fromFactory();
Pair<Order, Customer> orderCustomerPair = fluently.assemble().dto(recipe).to(Order.class, Customer.class).fromFactory();

Assertions.assertThat(orderCustomerPair.getValue0()).isNotNull();
Assertions.assertThat(orderCustomerPair.getValue0().getEntityId()).isEqualTo("order1");
Expand All @@ -71,7 +71,7 @@ public void testAssembleFromRepository() {

Pair<Order, Customer> orderCustomerPair = null;
try {
orderCustomerPair = fluently.assemble().dto(recipe).<Pair<Order, Customer>>to(Order.class, Customer.class).fromRepository().orFail();
orderCustomerPair = fluently.assemble().dto(recipe).to(Order.class, Customer.class).fromRepository().orFail();
} catch (AggregateNotFoundException e) {
fail();
}
Expand All @@ -86,8 +86,9 @@ public void testAssembleFromRepository() {
@Test
@Ignore
public void testAssembleFromRepositoryOrFail() {
Recipe recipe = new Recipe("customer1", "luke", "order1", "light saber");
try {
fluently.assemble().dto(new OrderDto("1", "light saber")).to(Order.class).fromRepository().orFail();
fluently.assemble().dto(recipe).to(Order.class, Customer.class).fromRepository().orFail();
fail();
} catch (AggregateNotFoundException e) {
Assertions.assertThat(e).isNotNull();
Expand All @@ -97,15 +98,16 @@ public void testAssembleFromRepositoryOrFail() {
@Test
@Ignore
public void testAssembleFromRepositoryOrFactory() {
OrderDto dto = new OrderDto("1", "light saber", PRICE);
Recipe recipe = new Recipe("customer1", "luke", "order1", "light saber");

Order aggregateRoot = fluently.assemble().dto(dto).to(Order.class).fromRepository().thenFromFactory();
Pair<Order, Customer> orderCustomerPair = fluently.assemble().dto(recipe).to(Order.class, Customer.class).fromRepository().thenFromFactory();

Assertions.assertThat(aggregateRoot).isNotNull();
Assertions.assertThat(aggregateRoot.getEntityId()).isEqualTo("1");
Assertions.assertThat(aggregateRoot.getProduct()).isEqualTo("light saber");
// the customer name is not part of the factory parameters, but so it should be set by the assembler
Assertions.assertThat(aggregateRoot.getPrice()).isEqualTo(PRICE);
Assertions.assertThat(orderCustomerPair.getValue0()).isNotNull();
Assertions.assertThat(orderCustomerPair.getValue0().getEntityId()).isEqualTo("order1");
Assertions.assertThat(orderCustomerPair.getValue0().getProduct()).isEqualTo("light saber");
// the customer name is not part of the factory parameters, so it is set by the assembler
Assertions.assertThat(orderCustomerPair.getValue1().getEntityId()).isEqualTo("customer1");
Assertions.assertThat(orderCustomerPair.getValue1().getName()).isEqualTo("luke");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package org.seedstack.business.assembler.dsl;

import com.google.common.collect.Lists;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -22,10 +23,13 @@
import org.seedstack.business.core.interfaces.AutomaticAssembler;
import org.seedstack.business.core.interfaces.DefaultAssembler;
import org.seedstack.business.core.interfaces.assembler.dsl.InternalRegistry;
import org.seedstack.business.core.interfaces.assembler.dsl.fixture.AutoAssembler;
import org.seedstack.business.core.interfaces.assembler.dsl.fixture.customer.AutoAssembler;
import org.seedstack.business.core.interfaces.assembler.dsl.fixture.customer.Customer;
import org.seedstack.business.core.interfaces.assembler.dsl.fixture.customer.Recipe;
import org.seedstack.seed.it.SeedITRunner;

import javax.inject.Inject;
import java.util.List;

/**
* Tests the DSL internal registry.
Expand Down Expand Up @@ -74,10 +78,17 @@ public void testAssemblerOfWithDefaultAssembler() {
Assertions.assertThat(assembler.getClass()).isEqualTo(expectedAutomaticAssembler.getClass());
}

@Test
public void testAssemblerOfTuple() {
List<?> aggregateRootTuple = Lists.newArrayList(org.seedstack.business.core.interfaces.assembler.dsl.fixture.customer.Order.class, Customer.class);
Assembler<?, ?> assembler = registry.tupleAssemblerOf((List<Class<? extends AggregateRoot<?>>>) aggregateRootTuple, Recipe.class);
Assertions.assertThat(assembler).isNotNull();
}

@Test
public void testAssemblerOfWithAutomaticAssembler() {
Assembler<?, ?> assembler = registry.assemblerOf(org.seedstack.business.core.interfaces.assembler.dsl.fixture.Order.class
, org.seedstack.business.core.interfaces.assembler.dsl.fixture.OrderDto.class);
Assembler<?, ?> assembler = registry.assemblerOf(org.seedstack.business.core.interfaces.assembler.dsl.fixture.customer.Order.class
, org.seedstack.business.core.interfaces.assembler.dsl.fixture.customer.OrderDto.class);
Assertions.assertThat(assembler).isNotNull();
Assertions.assertThat(assembler).isInstanceOf(AutomaticAssembler.class);
Assertions.assertThat(assembler).isNotInstanceOf(DefaultAssembler.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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.assembler.fixtures.book;

import org.seedstack.business.api.interfaces.assembler.BaseAssembler;

/**
* @author Pierre Thirouin <pierre.thirouin@ext.mpsa.com>
*/
public class BookAssembler extends BaseAssembler<StoredBook, BookDto> {

@Override
protected void doAssembleDtoFromAggregate(BookDto targetDto, StoredBook sourceAggregate) {
targetDto.setTitle(sourceAggregate.getEntityId().getTitle());
targetDto.setAuthor(sourceAggregate.getEntityId().getAuthor());
targetDto.setPublishDate(sourceAggregate.getPublishDate());
targetDto.setEditor(sourceAggregate.getEditor());
}

@Override
protected void doMergeAggregateWithDto(StoredBook targetAggregate, BookDto sourceDto) {
targetAggregate.setPublishDate(sourceDto.getPublishDate());
targetAggregate.setEditor(sourceDto.getEditor());
// identity never change
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* 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.assembler.fixtures.book;

import java.util.Date;

/**
* @author Pierre Thirouin <pierre.thirouin@ext.mpsa.com>
*/
public class BookDto {

private String title;

private String author;

private Date publishDate;

private String editor;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public Date getPublishDate() {
return publishDate;
}

public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}

public String getEditor() {
return editor;
}

public void setEditor(String editor) {
this.editor = editor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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.assembler.fixtures.book;

import org.seedstack.business.api.domain.base.BaseValueObject;

/**
* @author Pierre Thirouin <pierre.thirouin@ext.mpsa.com>
*/
public class BookId extends BaseValueObject {

private String title;

private String author;

public BookId(String title, String author) {
this.title = title;
this.author = author;
}

public String getTitle() {
return title;
}

public String getAuthor() {
return author;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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.assembler.fixtures.book;

import org.seedstack.business.api.domain.base.BaseAggregateRoot;

import java.util.Date;

/**
* @author Pierre Thirouin <pierre.thirouin@ext.mpsa.com>
*/
public class StoredBook extends BaseAggregateRoot<BookId> {

private BookId bookId;

private Date publishDate;

private String editor;

public StoredBook(BookId bookId) {
this.bookId = bookId;
}

@Override
public BookId getEntityId() {
return bookId;
}

public Date getPublishDate() {
return publishDate;
}

public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}

public String getEditor() {
return editor;
}

public void setEditor(String editor) {
this.editor = editor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected A doLoad(K id) {
return null;
}


@Override
protected void doDelete(K id) {

Expand Down
Loading