Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 15 ■ Spring Data Within Your Spring Application @Test @Ignore public void testMongoDBMigration(){ log.debug("Testing Spring Data MongoDB - Migration (Run only once)..."); assertNotNull(mongoDocumentDAO); assertNotNull(documentFacade); assertNotNull(typeFacade); assertNotNull(mongoTypeDAO); List types = typeFacade.getAllDefinedTypes(); assertNotNull(types); assertEquals(4, types.size()); for(Type type: types){ mongoTypeDAO.save(type); } List documents = documentFacade.getAllDocuments(); assertNotNull(documents); assertEquals(6, documents.size()); } for(Document document : documents){ mongoDocumentDAO.save(document.getDocumentId(), document); } @Test @Ignore public void testMongoDBFind(){ log.debug("Testing Spring Data MongoDB... [ FIND ]"); assertNotNull(mongoDocumentDAO); Document document = mongoDocumentDAO.findById(ID); assertNotNull(document); assertEquals(NAME,document.getName()); log.debug(document.toString()); } @Test @Ignore public void testMongoDBUpdate(){ log.debug("Testing Spring Data MongoDB... [ UPDATE ]"); assertNotNull(mongoDocumentDAO); Document document = new Document(ID,NAME_UPDATED); assertNotNull(document); Document updatedDocument = mongoDocumentDAO.save(ID, document); assertNotNull(updatedDocument); log.debug(updatedDocument.toString()); } 215

Chapter 15 ■ Spring Data Within Your Spring Application } @Test @Ignore public void testMongoDBRemove(){ log.debug("Testing Spring Data MongoDB... [ REMOVE ]"); assertNotNull(mongoDocumentDAO); Document document = mongoDocumentDAO.removeById(ID); assertNotNull(document); Document removedDocument = mongoDocumentDAO.findById(ID); assertNull(removedDocument); } Listing 15-7 shows the complete list of methods. In some cases, you are using the @Ignore of the unit test to test one by one and see the results on the Mongo shell. Also, you can use the @FixMethodOrder(MethodSorters.NAME_ASCENDING) that you used in previous chapters to keep the order of the execution of the test methods; this is because the unit test doesn’t guarantee any order, so maybe you will run into doing a remove before the find method. Now you can add any other property to your domain model without modifying the rest of the implementation, making this more reliable than drastic changes. For example, you can add an author property or a likes property (for how many likes the document has), and only change your Document class, and the rest of the application will be the same. Summary In this chapter, you saw how to use a NoSQL database using the MongoDB and the Spring Data module via the MongoOperations template. The Spring Framework offers more options not only for NoSQL but for graph databases like Neo4J. Of course the JPA (Java Persistence API) is another way to use relational databases. In the next chapter, you will go deeper into messaging and use even more features of the RabbitMQ broker. 216

Chapter 15 ■ <strong>Spring</strong> Data Within Your <strong>Spring</strong> Application<br />

}<br />

@Test<br />

@Ignore<br />

public void testMongoDBRemove(){<br />

log.debug("Testing <strong>Spring</strong> Data MongoDB... [ REMOVE ]");<br />

assertNotNull(mongoDocumentDAO);<br />

Document document = mongoDocumentDAO.removeById(ID);<br />

assertNotNull(document);<br />

Document removedDocument = mongoDocumentDAO.findById(ID);<br />

assertNull(removedDocument);<br />

}<br />

Listing 15-7 shows the complete list of methods. In some cases, you are using the @Ignore of the unit test to test one<br />

by one and see the results on the Mongo shell. Also, you can use the @FixMethodOrder(MethodSorters.NAME_ASCENDING)<br />

that you used in previous chapters to keep the order of the execution of the test methods; this is because the unit test<br />

doesn’t guarantee any order, so maybe you will run into doing a remove before the find method.<br />

Now you can add any other property to your domain model without modifying the rest of the implementation,<br />

making this more reliable than drastic changes. For example, you can add an author property or a likes property<br />

(for how many likes the document has), and only change your Document class, and the rest of the application will be<br />

the same.<br />

Summary<br />

In this chapter, you saw how to use a NoSQL database using the MongoDB and the <strong>Spring</strong> Data module via the<br />

MongoOperations template. The <strong>Spring</strong> <strong>Framework</strong> offers more options not only for NoSQL but for graph databases<br />

like Neo4J. Of course the JPA (Java Persistence API) is another way to use relational databases.<br />

In the next chapter, you will go deeper into messaging and use even more features of the RabbitMQ broker.<br />

216

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!