From 74065775467c1f2198d970f4dd9aa6e680aadb46 Mon Sep 17 00:00:00 2001 From: Roumen Georgiev Date: Wed, 2 Oct 2019 14:37:00 +0300 Subject: [PATCH] better test coverage & some cleanup --- .../actions/ActionProfileSwitch.java | 27 +++++++------------ .../actions/ActionProfileSwitchTest.java | 25 ++++++++--------- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitch.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitch.java index 9860fca735..a0af486caf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitch.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitch.java @@ -20,16 +20,13 @@ import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; import info.nightscout.androidaps.plugins.general.automation.elements.InputProfileName; import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement; import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder; -import info.nightscout.androidaps.plugins.profile.ns.NSProfilePlugin; import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.utils.JsonHelper; public class ActionProfileSwitch extends Action { private static Logger log = LoggerFactory.getLogger(L.AUTOMATION); - public InputProfileName inputProfileName = new InputProfileName("-"); + public InputProfileName inputProfileName = new InputProfileName(ProfileFunctions.getInstance().getProfileName()); String profileName = ""; - String profileStoreName; - String profileInterfaceString; public ActionProfileSwitch() { // Prevent action if active profile is already active @@ -50,25 +47,19 @@ public class ActionProfileSwitch extends Action { @Override public void doAction(Callback callback) { - String activeProfileName = ProfileFunctions.getInstance().getProfileName(); - /*log.debug("Current profile is: " + activeProfileName); - log.debug("Wanted profile is: " + profileName); - log.debug("Do they match: " + (profileName.equals(activeProfileName))); -*/ + String activeProfileName = ProfileFunctions.getInstance().getProfileName(); + //Check for uninitialized profileName + if ( profileName.equals("")){ profileName = activeProfileName; } + if (profileName.equals(activeProfileName)) { // Profile is already switched return; } - ProfileInterface profileInterface = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface(); - profileInterfaceString = profileInterface.toString(); - ProfileStore profileStore = NSProfilePlugin.getPlugin().getProfile(); - log.debug("NS profileStore is: " +profileStore.toString()); - if (profileStore == null) { - log.error("ProfileStore is null"); - return; - } - profileStoreName = profileStore.toString(); + ProfileInterface activeProfile = ConfigBuilderPlugin.getPlugin().getActiveProfileInterface(); + if (activeProfile == null) return; + ProfileStore profileStore = activeProfile.getProfile(); + if (profileStore == null) return; if(profileStore.getSpecificProfile(profileName) == null) { log.error("Selected profile does not exist! - "+ profileName); return; diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchTest.java b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchTest.java index 2226aac3d6..e62f573c2b 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchTest.java +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/actions/ActionProfileSwitchTest.java @@ -18,7 +18,6 @@ import info.nightscout.androidaps.R; import info.nightscout.androidaps.db.ProfileSwitch; import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions; -import info.nightscout.androidaps.plugins.general.nsclient.NSUpload; import info.nightscout.androidaps.plugins.profile.ns.NSProfilePlugin; import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; import info.nightscout.androidaps.queue.Callback; @@ -30,11 +29,11 @@ import static org.mockito.ArgumentMatchers.anyString; import static org.powermock.api.mockito.PowerMockito.when; @RunWith(PowerMockRunner.class) -@PrepareForTest({MainApp.class, SP.class, TreatmentsPlugin.class, ProfileFunctions.class, ConfigBuilderPlugin.class}) +@PrepareForTest({MainApp.class, SP.class, TreatmentsPlugin.class, ProfileFunctions.class, ConfigBuilderPlugin.class, NSProfilePlugin.class}) public class ActionProfileSwitchTest { TreatmentsPlugin treatmentsPlugin; ProfileSwitch profileAdded; - private ActionProfileSwitch actionProfileSwitch = new ActionProfileSwitch(); + private ActionProfileSwitch actionProfileSwitch; private String stringJson = "{\"data\":{\"profileToSwitchTo\":\"Test\"},\"type\":\"info.nightscout.androidaps.plugins.general.automation.actions.ActionProfileSwitch\"}"; @Before @@ -48,6 +47,7 @@ public class ActionProfileSwitchTest { AAPSMocker.mockTreatmentService(); AAPSMocker.mockBus(); AAPSMocker.mockDatabaseHelper(); + AAPSMocker.mockProfileFunctions(); treatmentsPlugin = AAPSMocker.mockTreatmentPlugin(); NSProfilePlugin profilePlugin = NSProfilePlugin.getPlugin(); @@ -62,6 +62,7 @@ public class ActionProfileSwitchTest { @Test public void friendlyName() { + actionProfileSwitch = new ActionProfileSwitch(); Assert.assertEquals(R.string.profilename, actionProfileSwitch.friendlyName()); } @@ -75,25 +76,24 @@ public class ActionProfileSwitchTest { @Test public void doAction() { -/* actionProfileSwitch = new ActionProfileSwitch(); - actionProfileSwitch.inputProfileName.setValue("Test"); - actionProfileSwitch.profileName = "Test"; - SP.putString("profile", AAPSMocker.getValidProfileStore().getData().toString()); + PowerMockito.when(ProfileFunctions.getInstance().getProfileName()).thenReturn("Test"); + + actionProfileSwitch = new ActionProfileSwitch(); + actionProfileSwitch.inputProfileName.setValue("someOtherProfile"); + actionProfileSwitch.profileName = "someProfile"; - when(NSProfilePlugin.getPlugin().getProfile()).thenReturn(AAPSMocker.getValidProfileStore()); -// Assert.assertEquals("", actionProfileSwitch.profileStoreName); - Assert.assertEquals("", actionProfileSwitch.profileInterfaceString); -// when(NSProfilePlugin.getPlugin().getProfile()).thenReturn(AAPSMocker.getValidProfileStore()); actionProfileSwitch.doAction(new Callback() { @Override public void run() { + Assert.assertTrue(result.success); } }); - Assert.assertNotEquals(null, profileAdded);*/ + } @Test public void hasDialogTest() { + actionProfileSwitch = new ActionProfileSwitch(); Assert.assertTrue(actionProfileSwitch.hasDialog()); } @@ -114,6 +114,7 @@ public class ActionProfileSwitchTest { @Test public void iconTest() { + actionProfileSwitch = new ActionProfileSwitch(); Assert.assertEquals(Optional.of(R.drawable.icon_actions_profileswitch), actionProfileSwitch.icon()); } } \ No newline at end of file