EnquirySyncService implements SyncService<Enquiry> { @Override public Enquiry sync(Enquiry record, User currentUser) throws IOException, JSONException, HttpException { String syncPath = getSyncPath(record); GenericSyncService<Enquiry> syncService = new GenericSyncService<Enquiry>(mediaSyncHelper, enquiryHttpDao, enquiryRepository); return syncService.sync(record, syncPath); } @Inject  EnquirySyncService(RapidFtrApplication rapidFtrApplication, EntityHttpDao<Enquiry> enquiryHttpDao, EnquiryRepository enquiryRepository); @Override Enquiry sync(Enquiry record, User currentUser); @Override Enquiry getRecord(String url); @Override List<String> getIdsToDownload(); @Override void setMedia(Enquiry enquiry); @Override int getNotificationId(); @Override String getNotificationTitle(); @Override void setLastSyncedAt(Enquiry enquiry); String getSyncPath(Enquiry enquiry); static final String ENQUIRIES_API_PATH; static final String ENQUIRIES_API_PARAMETER; }
@Test public void shouldNotSendUnnecessaryParamsDuringSync() throws Exception { String response = "{\"_id\" : \"couch_id\", \"photo_keys\":[], \"audio_attachments\":\"[]\",\"unique_identifier\":\"78223s4h1e468f5200edc\"}"; Enquiry record = new Enquiry(response); Enquiry recordSpy = spy(record); getFakeHttpLayer().setDefaultHttpResponse(200, response); EnquirySyncService enquirySyncService = new EnquirySyncService(mockContext(), enquiryHttpDao, enquiryRepository); enquirySyncService.sync(recordSpy, user); verify(recordSpy).remove("photo_keys"); verify(recordSpy).remove("audio_attachments"); verify(recordSpy).remove("synced"); }
@Test public void shouldUpdateEnquiryWhenItIsNotNew() throws Exception { String response = "{\"_id\" : \"couch_id\", \"child_name\":\"subhas\",\"unique_identifier\":\"78223s4h1e468f5200edc\"}"; Enquiry enquiry = spy(new Enquiry(response, "createdBy")); enquiry.put(Enquiry.FIELD_INTERNAL_ID, "id"); doReturn(false).when(enquiry).isNew(); BasicHttpResponse httpResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); httpResponse.setEntity(new StringEntity(response, ContentType.APPLICATION_JSON)); getFakeHttpLayer().addHttpResponseRule("PUT", "http: Enquiry returnedEnquiry = new EnquirySyncService(mockContext(), enquiryHttpDao, enquiryRepository).sync(enquiry, user); verify(enquiryRepository).createOrUpdateWithoutHistory(returnedEnquiry); }
@Test public void shouldRemoveHistoriesAfterSuccessfulSync() throws Exception { String response = "{\"_id\" : \"couch_id\", \"child_name\":\"subhas\",\"unique_identifier\":\"78223s4h1e468f5200edc\"}"; Enquiry enquirySpy = spy(new Enquiry(response, "createdBy")); enquirySpy.put(Enquiry.FIELD_INTERNAL_ID, "id"); doReturn(false).when(enquirySpy).isNew(); enquiryHttpDao = mock(EntityHttpDao.class); doReturn(enquirySpy).when(enquiryHttpDao).update(any(Enquiry.class), any(String.class), any(Map.class)); new EnquirySyncService(mockContext(), enquiryHttpDao, enquiryRepository).sync(enquirySpy, user); verify(enquirySpy).remove(History.HISTORIES); }
@Test(expected = SyncFailedException.class) public void shouldNotRemoveHistoriesAfterFailedSync() throws Exception { String response = "{\"_id\" : \"couch_id\", \"child_name\":\"subhas\",\"unique_identifier\":\"78223s4h1e468f5200edc\"}"; Enquiry enquirySpy = spy(new Enquiry(response, "createdBy")); doReturn(false).when(enquirySpy).isNew(); enquiryHttpDao = mock(EntityHttpDao.class); doThrow(new HTTPException(404)).when(enquiryHttpDao).update(any(Enquiry.class), any(String.class), any(Map.class)); new EnquirySyncService(mockContext(), enquiryHttpDao, enquiryRepository).sync(enquirySpy, user); verify(enquirySpy, never()).remove(History.HISTORIES); }
@Test public void shouldCreateEnquiryWhenItIsNew() throws Exception { String response = "{\"_id\" : \"couch_id\", \"child_name\":\"subhas\",\"unique_identifier\":\"78223s4h1e468f5200edc\"}"; Enquiry enquiry = spy(new Enquiry(response, "createdBy")); doReturn(true).when(enquiry).isNew(); BasicHttpResponse httpResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); httpResponse.setEntity(new StringEntity(response, ContentType.APPLICATION_JSON)); getFakeHttpLayer().addHttpResponseRule("POST", "http: Enquiry returnedEnquiry = new EnquirySyncService(mockContext(), enquiryHttpDao, enquiryRepository).sync(enquiry, user); verify(enquiryRepository).createOrUpdateWithoutHistory(returnedEnquiry); }
@Test public void shouldUpdateEnquiryAttributesAfterSync() throws Exception { String response = "{\"_id\" : \"couch_id\", \"child_name\":\"subhas\",\"unique_identifier\":\"78223s4h1e468f5200edc\", \"last_updated_at\":\"2014-04-12\"}"; Enquiry enquiry = spy(new Enquiry(response, "createdBy")); enquiry.put(Enquiry.FIELD_INTERNAL_ID, "id"); doReturn(false).when(enquiry).isNew(); BasicHttpResponse httpResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); httpResponse.setEntity(new StringEntity(response, ContentType.APPLICATION_JSON)); getFakeHttpLayer().addHttpResponseRule("PUT", "http: Enquiry returnedEnquiry = new EnquirySyncService(mockContext(), enquiryHttpDao, enquiryRepository).sync(enquiry, user); verify(enquiryRepository).createOrUpdateWithoutHistory(returnedEnquiry); assertNotNull(returnedEnquiry.getLastUpdatedAt()); assertThat(returnedEnquiry.isSynced(), CoreMatchers.is(true)); }
@Test(expected = SyncFailedException.class) public void shouldHandleSyncFailuresAndReturnEnquiry() throws Exception { String response = "{\"_id\" : \"couch_id\", \"child_name\":\"subhas\",\"unique_identifier\":\"78223s4h1e468f5200edc\"}"; Enquiry enquiry = spy(new Enquiry(response, "createdBy")); doReturn(true).when(enquiry).isNew(); BasicHttpResponse httpResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "ERROR"); httpResponse.setEntity(new StringEntity(response, ContentType.APPLICATION_JSON)); getFakeHttpLayer().addHttpResponseRule("POST", "http: new EnquirySyncService(mockContext(), enquiryHttpDao, enquiryRepository).sync(enquiry, user); assertFalse(enquiry.isSynced()); assertNull(enquiry.getLastUpdatedAt()); }