/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wso2.carbon.identity.oauth2.listener;

import org.mockito.Mock;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.core.util.IdentityConfigParser;
import org.wso2.carbon.identity.oauth.common.OAuthConstants;
import org.wso2.carbon.identity.oauth2.TestConstants;
import org.wso2.carbon.identity.oauth2.internal.OAuth2ServiceComponentHolder;
import org.wso2.carbon.identity.oauth2.test.utils.CommonTestUtils;
import org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.ResourceImpl;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.stratos.common.beans.TenantInfoBean;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class TenantCreationEventListenerTest extends PowerMockIdentityBaseTest {

    private TenantCreationEventListener tenantCreationEventListener = new TenantCreationEventListener();

    @Mock
    private RegistryService registryService;

    @Mock
    private UserRegistry registry;

    @Mock
    private IdentityConfigParser identityConfigParser;


    @BeforeTest
    public void setUp() throws Exception {
        String carbonHome = getClass().getResource("/").getFile();
        CommonTestUtils.setOAuthServerConfigurationProperties(carbonHome);
        OAuth2ServiceComponentHolder.setRegistryService(registryService);
    }

    @Test
    public void testOnTenantCreate() throws Exception {
        when(OAuth2ServiceComponentHolder.getRegistryService()
                                         .getConfigSystemRegistry(TestConstants.TENANT_ID)).thenReturn(registry);
        Resource resource = new ResourceImpl();
        when(registry.newResource()).thenReturn(resource);
        TenantInfoBean tenantInfoBean = new TenantInfoBean();
        tenantInfoBean.setTenantId(TestConstants.TENANT_ID);
        tenantCreationEventListener.onTenantCreate(tenantInfoBean);
        verify(registry, times(1)).put(OAuthConstants.SCOPE_RESOURCE_PATH, resource);
    }

    @Test
    public void testGetListenerOrder() {
        int listenerOrder = tenantCreationEventListener.getListenerOrder();
        Assert.assertEquals(listenerOrder, 0, "Listener order is different from expected.");
    }

}

@@ -754,20 +754,65 @@
 s.Before
-Test
+Class;%0Aimport org.testng.annotations.BeforeMethod
 ;%0Aimport
@@ -1521,16 +1521,82 @@
 nfoBean;
+%0Aimport org.wso2.carbon.stratos.common.exception.StratosException;
 %0A%0Aimport
@@ -2081,20 +2081,21 @@
  @Before
-Test
+Class
 %0A    pub
@@ -2268,24 +2268,90 @@
 arbonHome);%0A
+    %7D%0A%0A    @BeforeMethod%0A    public void setUpRegistryService() %7B%0A
         OAut
@@ -3176,24 +3176,24 @@
 nerOrder();%0A
-
         Asse
@@ -3280,10 +3280,1344 @@
 %0A    %7D%0A%0A
+    /**%0A     * Unit tests for empty methods%0A     * @throws StratosException%0A     */%0A    @Test%0A    public void testDummyMethod() throws StratosException %7B%0A        TenantInfoBean tenantInfoBean = new TenantInfoBean();%0A        tenantInfoBean.setTenantId(TestConstants.TENANT_ID);%0A        boolean triggeredAllMethods = false;%0A        try %7B%0A            tenantCreationEventListener.onTenantUpdate(tenantInfoBean);%0A            tenantCreationEventListener.onTenantDelete(TestConstants.TENANT_ID);%0A            tenantCreationEventListener%0A                    .onTenantRename(TestConstants.TENANT_ID, TestConstants.TENANT_DOMAIN, TestConstants.TENANT_DOMAIN);%0A            tenantCreationEventListener.onTenantInitialActivation(TestConstants.TENANT_ID);%0A            tenantCreationEventListener.onTenantActivation(TestConstants.TENANT_ID);%0A            tenantCreationEventListener.onTenantDeactivation(TestConstants.TENANT_ID);%0A            tenantCreationEventListener.onSubscriptionPlanChange(TestConstants.TENANT_ID, TestConstants.TENANT_DOMAIN,%0A                                                                 TestConstants.TENANT_DOMAIN);%0A            tenantCreationEventListener.onPreDelete(TestConstants.TENANT_ID);%0A            triggeredAllMethods = true;%0A        %7D finally %7B%0A            Assert.assertTrue(triggeredAllMethods);%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Add dummy methods to creation event listener