This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Writing a Test Case for Multiple User Modes

Running Multiple Instances of a Test Class with Different User Modes

Executing tests in multiple user modes is required to uncover any regression issues which could have been caused from user management changes. The test framework defines different users levels and provides the TestUserMode enum class to refer to user modes easily.

  • Super Tenant (Super Admin)

  • Super Tenant User

  • Tenant Admin

  • Tenant User

Executing the same tests with different user modes is highly recommended. You can run the same test class using the @Factory TestNG annotation. TestNG factory is used to create instances of test classes dynamically. This is useful if you want to run the test class a number of times. For example, if you have a test to deploy a web service and want to run this test multiple times with different user modes, then it’s easy to use the TestNG factory where you create multiple instances of a test class and run the tests. Whereas, dataprovider is used to provide parameters to a test. If you provide a dataprovider to a test, the test will be run taking different sets of values each time.

Shown below is a sample test case which runs with the user modes mentioned above:

public class AARServiceTestCase extends ASIntegrationTest {
        private static final Log log = LogFactory.getLog(AARServiceTestCase.class);
        private TestUserMode userMode;
    
        @Factory(dataProvider = "userModeProvider")
        public AARServiceTestCase(TestUserMode userMode) {
            this.userMode = userMode;
        }
    
        @BeforeClass(alwaysRun = true)
        public void init() throws Exception {
            super.init(userMode);
        }
    
        @Test(groups = "wso2.as", description = "Upload aar service and verify deployment")
        public void testAarServiceUpload() throws Exception {
            AARServiceUploaderClient aarServiceUploaderClient
                    = new AARServiceUploaderClient(backendURL, sessionCookie);
            aarServiceUploaderClient.uploadAARFile("Axis2Service.aar",
                    FrameworkPathUtil.getSystemResourceLocation() + "artifacts" +
                            File.separator + "AS" + File.separator + "aar" + File.separator +
                            "Axis2Service.aar", "");
            String axis2Service = "Axis2Service";
            isServiceDeployed(axis2Service);
            log.info("Axis2Service.aar service uploaded successfully");
        }
    
        @Test(groups = "wso2.as", description = "invoke aar service", dependsOnMethods = "testAarServiceUpload")
        public void invokeService() throws Exception {
            AxisServiceClient axisServiceClient = new AxisServiceClient();
            String endpoint = getServiceUrl("Axis2Service");
            OMElement response = axisServiceClient.sendReceive(createPayLoad(), endpoint, "echoInt");
            log.info("Response : " + response);
            assertTrue(response.toString().contains("<ns:return>25</ns:return>"));
        }
    
        public static OMElement createPayLoad() {
            OMFactory fac = OMAbstractFactory.getOMFactory();
            OMNamespace omNs = fac.createOMNamespace("http://service.carbon.wso2.org", "ns");
            OMElement getOme = fac.createOMElement("echoInt", omNs);
            OMElement getOmeTwo = fac.createOMElement("x", omNs);
            getOmeTwo.setText("25");
            getOme.addChild(getOmeTwo);
            return getOme;
        }
    
        @DataProvider
        private static TestUserMode[][] userModeProvider() {
            return new TestUserMode[][]{
                    new TestUserMode[]{TestUserMode.SUPER_TENANT_ADMIN},
                    new TestUserMode[]{TestUserMode.TENANT_ADMIN},
                    new TestUserMode[]{TestUserMode.SUPER_TENANT_USER},
                    new TestUserMode[]{TestUserMode.TENANT_USER},
            };
        }
    }