Skip to content

Commit

Permalink
AF-59: AppFrameworkServiceImplTest becomes plain JUnit. (#43)
Browse files Browse the repository at this point in the history
* AF-59: AppFrameworkServiceImplTest becomes plain JUnit.

* AF-59: Setting up a common instance of AppFrameworkServiceImpl for all test cases.
  • Loading branch information
mks-d authored and dkayiwa committed Oct 28, 2019
1 parent e668b67 commit ec7e41b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 38 deletions.
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/.settings
/.project
/.DS_Store
/.idea

omod/.DS_Store
**/.DS_Store
target

*.iml
/.classpath

**/.classpath
**/.project
**/.settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.openmrs.module.appframework.feature;

/**
* Provides a way to instantiate {@link FeatureToggleProperties} in unit tests.
*/
public class TestFeatureTogglePropertiesFactory {

public static FeatureToggleProperties get() {
return new FeatureToggleProperties();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.File;
import java.util.ArrayList;
Expand All @@ -11,38 +14,39 @@
import java.util.List;
import java.util.Map;

import javax.validation.Validator;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.api.db.hibernate.DbSessionFactory;
import org.openmrs.module.appframework.config.AppFrameworkConfig;
import org.openmrs.module.appframework.context.AppContextModel;
import org.openmrs.module.appframework.domain.AppDescriptor;
import org.openmrs.module.appframework.domain.Extension;
import org.openmrs.module.appframework.domain.ExtensionPoint;
import org.openmrs.module.appframework.feature.FeatureToggleProperties;
import org.openmrs.module.appframework.feature.TestFeatureTogglePropertiesFactory;
import org.openmrs.module.appframework.repository.AllAppDescriptors;
import org.openmrs.module.appframework.repository.AllComponentsState;
import org.openmrs.module.appframework.repository.AllFreeStandingExtensions;
import org.openmrs.test.BaseModuleContextSensitiveTest;
import org.openmrs.test.SkipBaseSetup;
import org.springframework.beans.factory.annotation.Autowired;

@SkipBaseSetup
public class AppFrameworkServiceImplTest extends BaseModuleContextSensitiveTest {

@Autowired
private AppFrameworkService appFrameworkService;

@Autowired
private AllAppDescriptors allAppDescriptors;
public class AppFrameworkServiceImplTest {

private Validator validator = mock(Validator.class);

private AllAppDescriptors allAppDescriptors = new AllAppDescriptors(validator);

@Autowired
private AllFreeStandingExtensions allFreeStandingExtensions;
private AllFreeStandingExtensions allFreeStandingExtensions = new AllFreeStandingExtensions(validator);

private AllComponentsState allComponentsState = new AllComponentsState();

@Autowired
private FeatureToggleProperties featureToggles;
private FeatureToggleProperties featureToggles = TestFeatureTogglePropertiesFactory.get();

@Autowired
private AppFrameworkConfig appFrameworkConfig;
private AppFrameworkConfig appFrameworkConfig = new AppFrameworkConfig();

private AppDescriptor app1;

Expand All @@ -57,7 +61,8 @@ public class AppFrameworkServiceImplTest extends BaseModuleContextSensitiveTest
private Extension ext4;

private Extension ext5;


private AppFrameworkServiceImpl service;

@Before
public void setUp() throws Exception {
Expand Down Expand Up @@ -93,6 +98,15 @@ public void setUp() throws Exception {

// now add some free-standing extension
allFreeStandingExtensions.add(Arrays.asList(ext3, ext4, ext5));

// to go through all the Hibernate stuff
SessionFactory sessionFactory = mock(SessionFactory.class);
Session session = mock(Session.class);
when(session.createCriteria(any(Class.class))).thenReturn(mock(Criteria.class));
when(sessionFactory.getCurrentSession()).thenReturn(session);
allComponentsState.setSessionFactory(new DbSessionFactory(sessionFactory));

service = new AppFrameworkServiceImpl(null, allAppDescriptors, allFreeStandingExtensions, allComponentsState, null, featureToggles, appFrameworkConfig, null);
}

@After
Expand All @@ -103,7 +117,7 @@ public void tearDown() throws Exception {

@Test
public void testGetAllAppsAndIsSortedByOrder() throws Exception {
List<AppDescriptor> allApps = appFrameworkService.getAllApps();
List<AppDescriptor> allApps = service.getAllApps();

assertEquals(2, allApps.size());
assertEquals("app2", allApps.get(0).getId());
Expand All @@ -112,29 +126,27 @@ public void testGetAllAppsAndIsSortedByOrder() throws Exception {

@Test
public void testGetAllEnabledAppsShouldIgnoreAppsToggledOffInFeatureTogglesFile() throws Exception {
List<AppDescriptor> allApps = appFrameworkService.getAllEnabledApps();
List<AppDescriptor> allApps = service.getAllEnabledApps();

assertEquals(1, allApps.size());
assertEquals("app1", allApps.get(0).getId());
}

@Test
public void testGetAllEnabledAppsShouldCorrectlyHandleNegatedFeatureToggles() throws Exception {

// we change these so that app1 should only be enabled if app toggle 1 is not enabled, same for app2
app1.setFeatureToggle("!app1Toggle");
app2.setFeatureToggle("!app2Toggle");

List<AppDescriptor> allApps = appFrameworkService.getAllEnabledApps();
List<AppDescriptor> allApps = service.getAllEnabledApps();

assertEquals(1, allApps.size());
assertEquals("app2", allApps.get(0).getId());
}


@Test
public void testGetAllExtensionsAndIsSortedByOrder() throws Exception {
List<Extension> extensionPoints = appFrameworkService.getAllExtensions("extensionPoint2");
List<Extension> extensionPoints = service.getAllExtensions("extensionPoint2");

assertEquals(2, extensionPoints.size());
assertEquals("ext5", extensionPoints.get(0).getId());
Expand All @@ -144,7 +156,7 @@ public void testGetAllExtensionsAndIsSortedByOrder() throws Exception {

@Test
public void testGetAllEnabledExtensionsShouldIgnoreEnabledToggledOffInFeatureTogglesFile() throws Exception {
List<Extension> extensionPoints = appFrameworkService.getAllEnabledExtensions("extensionPoint2");
List<Extension> extensionPoints = service.getAllEnabledExtensions("extensionPoint2");

assertEquals(2, extensionPoints.size());
assertEquals("ext5", extensionPoints.get(0).getId());
Expand All @@ -153,26 +165,22 @@ public void testGetAllEnabledExtensionsShouldIgnoreEnabledToggledOffInFeatureTog

@Test
public void testGetAllEnabledExtensionsShouldCorrectlyHandleNegatedFeatureToggles() throws Exception {

// invert the feature toggles
ext1.setFeatureToggle("!ext1Toggle");
ext2.setFeatureToggle("!ext2Toggle");
ext3.setFeatureToggle("!ext3Toggle");
ext4.setFeatureToggle("!ext4Toggle");
ext5.setFeatureToggle("!ext5Toggle");

List<Extension> extensionPoints = appFrameworkService.getAllEnabledExtensions("extensionPoint2");
List<Extension> extensionPoints = service.getAllEnabledExtensions("extensionPoint2");

assertEquals(2, extensionPoints.size());
assertEquals("ext4", extensionPoints.get(0).getId());
assertEquals("ext1", extensionPoints.get(1).getId());

}

@Test
public void testCheckRequireExpression() throws Exception {
AppFrameworkServiceImpl service = new AppFrameworkServiceImpl(null, null, null, null, null, null, null, null);

VisitStatus visit = new VisitStatus(true, false);
AppContextModel contextModel = new AppContextModel();
contextModel.put("visit", visit);
Expand All @@ -190,7 +198,6 @@ public void testCheckRequireExpressionWithMapProperty() throws Exception {
obj.put("uuid", "abc-123");
contextModel.put("sessionLocation", obj);

AppFrameworkServiceImpl service = new AppFrameworkServiceImpl(null, null, null, null, null, null, null, null);
assertTrue(service.checkRequireExpression(extensionRequiring("sessionLocation.uuid == 'abc-123'"), contextModel));
}

Expand All @@ -205,7 +212,6 @@ public void testUtilityFunctionForRequireExpressions() throws Exception {
obj.put("tags", tags);
contextModel.put("sessionLocation", obj);

AppFrameworkServiceImpl service = new AppFrameworkServiceImpl(null, null, null, null, null, null, null, null);
assertTrue(service.checkRequireExpression(extensionRequiring("hasMemberWithProperty(sessionLocation.tags, 'display', 'Login Location')"), contextModel));
assertFalse(service.checkRequireExpression(extensionRequiring("hasMemberWithProperty(sessionLocation.tags, 'display', 'Not this tag')"), contextModel));
}
Expand Down

0 comments on commit ec7e41b

Please sign in to comment.