ljugm - boost your java enterprise stack with mongodb

22
Boost your Java Enterprise Stack with MongoDB [email protected] / twitter: @Bernd_Z [email protected] 26. Nov. 2013 @ Leightweight Java User Group Munich

Upload: bernd-zuther

Post on 13-Jan-2015

231 views

Category:

Education


1 download

DESCRIPTION

If you want to use MongoDB in the Java Enterprise World you have to address two questions. The first one is: Should I use the native driver or should I use a persistence framework? Spring is a common framework in many Java applications. It has created a very good object-document-mapping framework with Spring Data for MongoDB. It is easy to integrate into new and existing Java applications. The second question is: How can I ensure my deliverables against error? So you need a testing framework. NoSQLUnit is such a framework that helps you to write tests against a real MongoDB Server or an in-memory clone like Fongo. This talk will cover Spring Data and NoSQLUnit. It will give a basic overview about what things are useful before you can deploy your application, e.g. in a cloud service like MongoSoup.

TRANSCRIPT

Boost your Java Enterprise Stack

with MongoDB

!

!

[email protected] / twitter: @Bernd_Z [email protected]

!26. Nov. 2013 @ Leightweight Java User Group Munich

Continuous Delivery

Standard JEE Stack

Frontend Layer

Service Layer

Repository Layer

MongoDB{ "_id" : ObjectId("51f2ce49c2e6dce83534b73f"), "orderId" : NumberLong(666), "orderItems" : [ { "product" : { "_id" : ObjectId("51f2ce48c2e6dce83534b6ba"), "articleId" : "100", "name" : "Margherita", "urlname" : "margherita", "description" : "our famous tomato sauce and genuine mozzarella", "type" : "PIZZA", "category" : "vegetarian", "price" : 3.8 }, "uuid" : "839f8305-a5e7-408a-96d5-e3d2819d6ad3" } ], "orderDate" : ISODate("2011-09-13T07:24:00Z"), "deliveryAddress" : { "firstname" : "Christan", "lastname" : "Kroemer", "street" : „Lindwurm Straße", "zip" : "80337", "city" : "Munich", "houseNumber" : "97" }, "userId" : "51f2ce48c2e6dce83534b70b" }

Hibernate ORM

mapping of POJOs to relational database tables

@Entity @Table(name = „ORDER") public class Order implements Serializable { ! @Id @GeneratedValue @Column(name="ID") private Long id; ! @OneToOne(cascade = CascadeType.ALL, optional = false, orphanRemoval = false) @JoinColumn(name = "USER_ID", nullable = false) @ForeignKey(name = "ORDER_USER_FK") private User user; ! @OneToMany(mappedBy = "order") @Fetch(FetchMode.JOIN) private List<OrderItem> orderItems;

Spring Data

annotated sample class here

@Document(collection = Order.COLLECTION_NAME) public class Order implements Serializable { public static final String COLLECTION_NAME = "order"; ! @Id private ObjectId id; ! @Indexed(unique = true) private long orderId; ! @DBRef private User user; private List<OrderItem> orderItems; ! @Indexed private Date orderDate = new Date();

Unit Tests

relational in memory DB for testing

- Fast

- Isolated

- Repeatable

- Self-verifying

- Timely

DBUnit<?xml version="1.0" encoding="UTF-8"?> <dataset> <User id="0" title="Mr" firstName="Bernd" lastName="Zuther"/> <User id="1" title="Mr" firstName="Christian" lastName="Kroemer"/> </dataset>

@Test @DatabaseSetup("sampleData.xml") public void testFind() throws Exception { List<User> userList = userService.findByName("Christian"); assertEquals(1, userList.size()); assertEquals("Christian", userList.get(0).getFirstName()); }

Fongo

alternative for the Mongo world TODO: does mongo soup fit in there anywhere?

<dependency> <groupId>com.github.fakemongo</groupId> <artifactId>fongo</artifactId> <version>1.3.2</version> </dependency> !!!!<bean name="fongo" class=“com.github.fakemongo.Fongo"> <constructor-arg value="InMemoryMongo" /> </bean> !<bean id="mongo" factory-bean="fongo" factory-method="getMongo" />

NoSQLUnit@ActiveProfiles("test") @ContextConfiguration(locations = "classpath:com/comsysto/shop/ui/spring-context.xml") @UsingDataSet(locations="initialData.json", loadStrategy=LoadStrategyEnum.CLEAN_INSERT) public class OrderRepositoryImplTest extends AbstractJUnit4SpringContextTests { ! @Autowired private OrderRepository orderRepository; @Autowired private ApplicationContext applicationContext; @Rule public MongoDbRule mongoDbRule = newMongoDbRule().defaultSpringMongoDb("pizza"); ! @Test public void testFindAll() { Sort sortOrder = new Sort(Sort.Direction.ASC, "orderId"); List<Order> retrievedOrders = orderRepository.findAll(10, 0, sortOrder); assertNotNull(retrievedOrders); assertEquals(5, retrievedOrders.size()); } }

Presenter

Continuous Delivery

Infrastructure

Danger Area

Downtime

Flyway

relational in memory DB for testing

Flyway

relational in memory DB for testing

MongoDB - Schemaless

relational in memory DB for testing

{ name : "Bernd", age : 30, interests : "football" } !{ name : "Christian", age : 25 }

MongoDB - Schemaless

relational in memory DB for testing

Build... Test... Live!

https://github.com/comsysto/mongodb-onlineshop/

Questions

Presenter