Skip to content

Commit

Permalink
SYNCT-327:Enable disabling of Manual Sync to Parent and Manual Pull t…
Browse files Browse the repository at this point in the history
…o Parent
  • Loading branch information
reagan-meant committed Oct 24, 2019
1 parent 6b97207 commit 3dec202
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
import org.openmrs.module.sync2.api.exceptions.SyncException;
import org.openmrs.module.sync2.api.model.SyncCategory;
import org.openmrs.module.sync2.api.model.configuration.SyncMethodConfiguration;
import org.openmrs.module.sync2.api.service.SyncConfigurationService;
import org.openmrs.module.sync2.client.reader.LocalFeedReader;
import org.openmrs.module.sync2.client.reader.atomfeed.LocalAtomfeedFeedWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("sync2.localFeedReader." + SyncConstants.ATOMFEED_EVENT_HANDLER)
public class LocalAtomfeedFeedReaderImpl extends AbstractAtomfeedFeedReader implements LocalFeedReader {

@Autowired
private SyncConfigurationService syncConfigurationService;

private boolean enabled() {
return syncConfigurationService.getSyncConfiguration().getWhitelist().isEnabled();
}

public LocalAtomfeedFeedReaderImpl() {
super(new LocalAtomfeedFeedWorker());
}
Expand All @@ -27,11 +35,17 @@ protected String getBaseUri() {

@Override
public void readAndPushAllFeeds() {
readAndProcessAllFeeds();
if(enabled()==true) {
readAndProcessAllFeeds();
}
else throw new SyncException("Sync is disabled to push Child data to parent");
}

@Override
public void readAndPushAllFeeds(SyncCategory category) throws SyncException {
readAndProcessFeedsForCategory(category.getCategory());
}
if(enabled()==true) {
readAndProcessFeedByCategory(category.getCategory());
}
else throw new SyncException("Sync is disabled to push Child data to parent");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
import org.openmrs.module.sync2.api.exceptions.SyncException;
import org.openmrs.module.sync2.api.model.SyncCategory;
import org.openmrs.module.sync2.api.model.configuration.SyncMethodConfiguration;
import org.openmrs.module.sync2.api.service.SyncConfigurationService;
import org.openmrs.module.sync2.client.reader.ParentFeedReader;
import org.openmrs.module.sync2.client.reader.atomfeed.ParentAtomfeedFeedWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("sync2.parentFeedReader." + SyncConstants.ATOMFEED_EVENT_HANDLER)
public class ParentAtomfeedFeedReaderImpl extends AbstractAtomfeedFeedReader implements ParentFeedReader {

@Autowired
private SyncConfigurationService syncConfigurationService;

public boolean enabled() {
return syncConfigurationService.getSyncConfiguration().getWhitelist().isEnabled();
}

public ParentAtomfeedFeedReaderImpl() {
super(new ParentAtomfeedFeedWorker());
}
Expand All @@ -27,11 +35,19 @@ protected String getBaseUri() {

@Override
public void pullAndProcessAllFeeds() {

if(enabled()==true) {
readAndProcessAllFeeds();
}
else throw new SyncException("Sync is disabled to pull parent data to Child");

}

@Override
public void pullAndProcessFeeds(SyncCategory category) throws SyncException {
if(enabled()==true) {
readAndProcessFeedByCategory(category.getCategory());
}
else throw new SyncException("Sync is disabled to pull parent data to Child");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.openmrs.module.sync2.api.service.impl.atomfeed;


public class LocalAtomfeedFeedReaderTest {

public void LocalAtomfeedFeedReader_shouldNotSyncIfPushIsDisabled() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.openmrs.module.sync2.api.service.impl.atomfeed;

import static org.powermock.api.mockito.PowerMockito.doReturn;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.openmrs.module.sync2.api.exceptions.SyncException;
import org.openmrs.module.sync2.api.service.SyncConfigurationService;
import org.openmrs.module.sync2.api.service.impl.SyncConfigurationServiceImpl;
import org.openmrs.module.sync2.client.reader.atomfeed.impl.ParentAtomfeedFeedReaderImpl;

@RunWith(MockitoJUnitRunner.class)
public class ParentAtomfeedFeedReaderTest {

@InjectMocks
private ParentAtomfeedFeedReaderImpl parentAtomfeedFeedReaderImpl;

This comment has been minimized.

Copy link
@mhawila

mhawila Oct 28, 2019

Contributor

First use the interface instead of the concrete implementation, therefore private ParentAtomFeedReader parentAtomfeedReader. Second this is the instance in which you want to inject mocks but it is not initialized anywhere. You could create this in the setUp() method or you could simply declare and initialize it here with new. I haven't checked the stack trace but I think this is where the NPE is coming from.


@Mock
private SyncConfigurationService sync2ConfigurationService;

@Before
public void setUp() throws SyncException {
sync2ConfigurationService = new SyncConfigurationServiceImpl();

This comment has been minimized.

Copy link
@mhawila

mhawila Oct 28, 2019

Contributor

You don't need to initialize this because you are already telling the mock framework to mock it for you in the line 24 above.

MockitoAnnotations.initMocks(this);
doReturn("false").when(sync2ConfigurationService).getSyncConfiguration().getWhitelist().isEnabled();
}

@Test
public void ParentAtomfeedFeedReader_shouldNotSyncIfPullIsDisabled() throws SyncException {
parentAtomfeedFeedReaderImpl.pullAndProcessAllFeeds();
}
}

0 comments on commit 3dec202

Please sign in to comment.