Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
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
- Page 161 and 162: Chapter 11 ■ Integrating Your Spr
- Page 163 and 164: Chapter 11 ■ Integrating Your Spr
- Page 165 and 166: Chapter 11 ■ Integrating Your Spr
- Page 167 and 168: Chapter 12 Exposing a REST API Nowa
- Page 169 and 170: Chapter 12 ■ Exposing a REST API
- Page 171 and 172: Chapter 12 ■ Exposing a REST API
- Page 173 and 174: Chapter 12 ■ Exposing a REST API
- Page 175 and 176: Chapter 12 ■ Exposing a REST API
- Page 177 and 178: Chapter 12 ■ Exposing a REST API
- Page 179 and 180: HTTP-DELETE Chapter 12 ■ Exposing
- Page 181 and 182: Chapter 13 Adding E-mail and Schedu
- Page 183 and 184: Chapter 13 ■ Adding E-mail and Sc
- Page 185 and 186: Chapter 13 ■ Adding E-mail and Sc
- Page 187 and 188: Chapter 13 ■ Adding E-mail and Sc
- Page 189 and 190: Chapter 13 ■ Adding E-mail and Sc
- Page 191 and 192: Part III Spring Framework Advanced
- Page 193 and 194: Chapter 14 ■ Using Dynamic Langua
- Page 195 and 196: Chapter 14 ■ Using Dynamic Langua
- Page 197 and 198: Chapter 14 ■ Using Dynamic Langua
- Page 199 and 200: Chapter 14 ■ Using Dynamic Langua
- Page 201 and 202: Chapter 15 ■ Spring Data Within Y
- Page 203 and 204: Chapter 15 ■ Spring Data Within Y
- Page 205 and 206: Chapter 15 ■ Spring Data Within Y
- Page 207 and 208: Chapter 15 ■ Spring Data Within Y
- Page 209 and 210: Chapter 15 ■ Spring Data Within Y
- Page 211: Chapter 15 ■ Spring Data Within Y
- Page 215 and 216: Chapter 16 ■ Messaging with Your
- Page 217 and 218: Chapter 16 ■ Messaging with Your
- Page 219 and 220: Chapter 16 ■ Messaging with Your
- Page 221 and 222: Chapter 16 ■ Messaging with Your
- Page 223 and 224: Chapter 16 ■ Messaging with Your
- Page 225 and 226: Chapter 16 ■ Messaging with Your
- Page 227 and 228: Chapter 17 Be Social and Go Mobile
- Page 229 and 230: Chapter 17 ■ Be Social and Go Mob
- Page 231 and 232: Chapter 17 ■ Be Social and Go Mob
- Page 233 and 234: Chapter 17 ■ Be Social and Go Mob
- Page 235 and 236: Chapter 17 ■ Be Social and Go Mob
- Page 237 and 238: Chapter 17 ■ Be Social and Go Mob
- Page 239 and 240: Chapter 17 ■ Be Social and Go Mob
- Page 241 and 242: Chapter 17 ■ Be Social and Go Mob
- Page 243 and 244: Chapter 17 ■ Be Social and Go Mob
- Page 245 and 246: Chapter 17 ■ Be Social and Go Mob
- Page 247 and 248: Part IV Spring I/O Spring I/O is no
- Page 249 and 250: Chapter 18 ■ Spring and Groovy
- Page 251 and 252: Chapter 18 ■ Spring and Groovy Li
- Page 253 and 254: Chapter 18 ■ Spring and Groovy cl
- Page 255 and 256: Chapter 18 ■ Spring and Groovy If
- Page 257 and 258: Chapter 18 ■ Spring and Groovy Li
- Page 259 and 260: Chapter 19 ■ Spring Boot, Simplif
- Page 261 and 262: Chapter 19 ■ Spring Boot, Simplif
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