package org.wso2.carbon.identity.application.authentication.framework.handler.request.impl;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext;
import org.wso2.carbon.identity.common.testng.WithCarbonHome;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.testutil.IdentityBaseTest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.RequestParams.LOGOUT;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.RequestParams.TENANT_DOMAIN;
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.RequestParams.TYPE;
import static org.wso2.carbon.identity.core.util.IdentityCoreConstants.TENANT_NAME_FROM_CONTEXT;
import static org.wso2.carbon.utils.multitenancy.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;

/**
 * Unit tests for {@link DefaultRequestCoordinator}.
 */
@WithCarbonHome
public class DefaultRequestCoordinatorTest extends IdentityBaseTest {

    private DefaultRequestCoordinator requestCoordinator;

    @BeforeMethod
    public void setUp() throws Exception {

        requestCoordinator = new DefaultRequestCoordinator();
    }

    @AfterMethod
    public void tearDown() throws Exception {

    }

    @DataProvider(name = "tenantDomainProvider")
    public Object[][] provideTenantDomain() {

        return new Object[][]{
                {null, null, SUPER_TENANT_DOMAIN_NAME},
                {"foo.com", "xyz.com", "foo.com"},
                {null, "xyz.com", "xyz.com"},
        };
    }

    @Test(dataProvider = "tenantDomainProvider")
    public void testTenantDomainInAuthenticationContext(String tenantDomainInThreadLocal,
                                                        String tenantDomainInRequestParam,
                                                        String expected) throws Exception {

        setTenantDomainInThreadLocalContext(tenantDomainInThreadLocal);

        HttpServletRequest request = mock(HttpServletRequest.class);
        when(request.getParameter(TYPE)).thenReturn("oauth");
        when(request.getParameter(LOGOUT)).thenReturn("true");
        when(request.getParameter(TENANT_DOMAIN)).thenReturn(tenantDomainInRequestParam);

        HttpServletResponse response = mock(HttpServletResponse.class);

        AuthenticationContext context = requestCoordinator.initializeFlow(request, response);

        assertEquals(context.getTenantDomain(), expected);
    }

    private void setTenantDomainInThreadLocalContext(String tenantDomainInThreadLocalContext) {

        IdentityUtil.threadLocalProperties.get().put(TENANT_NAME_FROM_CONTEXT, tenantDomainInThreadLocalContext);
    }
}

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

Fix license