DocumentService { public DocumentMetadata saveDocument(DocumentMetadata document) { LOG.debug("Saving a document"); if (document != null) { if (document.getDocumentid() != null) { LOG.debug("Performing an update for document: {}", document.getDocumentid()); DocumentMetadata ecDoc = getDocument(document.getDocumentid()); if (ecDoc != null) { Set<EventCode> eventCodes = ecDoc.getEventCodes(); if (eventCodes != null && !eventCodes.isEmpty()) { for (EventCode eventCode : eventCodes) { eventCodeDao.delete(eventCode); eventCode.setEventCodeId(null); } } eventCodes = document.getEventCodes(); if (eventCodes != null && !eventCodes.isEmpty()) { for (EventCode eventCode : eventCodes) { if (eventCode.getEventCodeId() != null) { eventCode.setEventCodeId(null); } } } } } Document doc = document.getDocument(); if (doc != null && doc.getRawData().length > 0) { String documentStr; try { String sHash; documentStr = StringUtil.convertToStringUTF8(doc.getRawData()); sHash = SHA1HashCode.calculateSHA1(documentStr); if (LOG.isDebugEnabled()) { LOG.debug("Successfully created Hash Code for string: {}", sHash); } document.setHash(sHash); } catch (UnsupportedEncodingException uee) { LOG.error("Failed to create SHA-1 Hash code for Data Text. Error:" + uee.getMessage(), uee); } catch (NoSuchAlgorithmException nsae) { LOG.error("Failed to create SHA-1 Hash code for Data Text. Error:" + nsae.getMessage(), nsae); } } else { document.setHash(""); LOG.warn("No SHA-1 Hash Code created because document data was empty."); } } documentDao.save(document); return document; }  DocumentMetadata saveDocument(DocumentMetadata document); void deleteDocument(DocumentMetadata document); DocumentMetadata getDocument(Long documentId); List<DocumentMetadata> getAllDocuments(); List<DocumentMetadata> documentQuery(DocumentQueryParams params); String getNextID();  }
@Test public void testSaveDocument() throws NoSuchAlgorithmException, UnsupportedEncodingException { DocumentService documentService = getDocumentServiceWithMockDaos(); DocumentMetadata doc = new DocumentMetadata(); Document document = new Document(doc); long id = 12345L; String rawDataString = "RAW DATA"; byte[] rawData = rawDataString.getBytes(); document.setRawData(rawData); doc.setDocumentid(id); HashSet<EventCode> eventCodes = new HashSet<>(); eventCodes.add(new EventCode()); doc.setEventCodes(eventCodes); doc.setDocument(document); documentService.saveDocument(doc); when(DOCUMENT_DAO.findById(anyLong())).thenReturn(doc); verify(DOCUMENT_DAO).save(doc); assertEquals(SHA1HashCode.calculateSHA1(rawDataString), doc.getHash()); }