/*
 *
 *   Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *   WSO2 Inc. licenses this file to you 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.apimgt.keymgt.handlers;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.wso2.carbon.apimgt.api.model.subscription.URLMapping;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO;
import org.wso2.carbon.apimgt.keymgt.APIKeyMgtException;
import org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder;
import org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore;
import org.wso2.carbon.apimgt.keymgt.model.entity.API;
import org.wso2.carbon.apimgt.keymgt.service.TokenValidationContext;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static org.mockito.Matchers.eq;
import static org.wso2.carbon.base.CarbonBaseConstants.CARBON_HOME;


@RunWith(PowerMockRunner.class)
@PrepareForTest({DefaultKeyValidationHandler.class, TokenValidationContext.class, APIKeyValidationInfoDTO.class,
        SubscriptionDataStore.class, SubscriptionDataHolder.class, PrivilegedCarbonContext.class})

public class DefaultKeyValidationHandlerTest extends DefaultKeyValidationHandler {

    private final String USER_NAME = "admin";
    private final String API_CONTEXT = "/apicontext/1.0.0";
    private final String API_NAME = "apiname";
    private final String API_VERSION = "1.0.0";
    private final String DEFAULT_API_VERSION = "_default_1.0.0";
    private final String SUBSCRIBER = "subscriber";
    private final String RESOURCE = "/test";
    private final String TENANT_DOMAIN = "carbon.super";
    private final String HTTP_VERB = "GET";
    private final String APPLICATION_NAME = "foo_PRODUCTION";
    private final String APPLICATION_ID = "1";
    private final String SCOPES = "subscriber";
    private final String ACCESS_TOKEN = "ca19a540f544777860e44e75f605d927";
    private final String TIER = "unlimited";

    @Before
    public void setup() throws Exception {
        System.setProperty(CARBON_HOME, "");
        PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
        PowerMockito.mockStatic(PrivilegedCarbonContext.class);
        PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    }

    @Test
    public void testValidateScopes() throws APIKeyMgtException {

        API api = new API();
        api.setApiId(1);
        api.setApiProvider(USER_NAME);
        api.setApiName(API_NAME);
        api.setApiVersion(API_VERSION);
        api.setContext(API_CONTEXT);
        URLMapping urlMapping = new URLMapping();
        urlMapping.addScope(SCOPES);
        urlMapping.setHttpMethod(HTTP_VERB);
        urlMapping.setUrlPattern(RESOURCE);
        api.addResource(urlMapping);

        Map<String, API> apiMap = new HashMap<>();
        String key = API_CONTEXT + ":" + API_VERSION;
        apiMap.put(key, api);

        APIKeyValidationInfoDTO dto = new APIKeyValidationInfoDTO();
        dto.setSubscriber(SUBSCRIBER);
        dto.setApplicationName(APPLICATION_NAME);
        dto.setApplicationId(APPLICATION_ID);
        dto.setApplicationTier(TIER);
        Set<String> scopeSet = new HashSet<>();
        scopeSet.add(SCOPES);
        dto.setScopes(scopeSet);
        dto.setSubscriberTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
        dto.setUserType(APIConstants.ACCESS_TOKEN_USER_TYPE_APPLICATION);

        // TokenValidationContext for not default API
        TokenValidationContext param1 = new TokenValidationContext();
        param1.setValidationInfoDTO(dto);
        param1.setContext(API_CONTEXT);
        param1.setVersion(API_VERSION);
        param1.setAccessToken(ACCESS_TOKEN);
        param1.setMatchingResource(RESOURCE);
        param1.setHttpVerb(HTTP_VERB);

        // TokenValidationContext for default API version
        TokenValidationContext param2 = new TokenValidationContext();
        param2.setValidationInfoDTO(dto);
        param2.setContext(API_CONTEXT);
        param2.setVersion(DEFAULT_API_VERSION);
        param2.setAccessToken(ACCESS_TOKEN);
        param2.setMatchingResource(RESOURCE);
        param2.setHttpVerb(HTTP_VERB);


        SubscriptionDataHolder subscriptionDataHolder = Mockito.mock(SubscriptionDataHolder.class);
        SubscriptionDataStore tenantSubscriptionStore = Mockito.mock(SubscriptionDataStore.class);
        PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);

        PowerMockito.mockStatic(SubscriptionDataHolder.class);
        Mockito.when(SubscriptionDataHolder.getInstance()).thenReturn(subscriptionDataHolder);

        PowerMockito.mockStatic(PrivilegedCarbonContext.class);
        Mockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
        Mockito.when(privilegedCarbonContext.getTenantDomain()).thenReturn(TENANT_DOMAIN);

        Mockito.when(subscriptionDataHolder.getTenantSubscriptionStore(eq(TENANT_DOMAIN))).thenReturn(tenantSubscriptionStore);
        Mockito.when(tenantSubscriptionStore.getApiByContextAndVersion(eq(API_CONTEXT), eq(API_VERSION))).thenReturn(api);


        DefaultKeyValidationHandler defaultKeyValidationHandler = new DefaultKeyValidationHandler();

        boolean isScopeValidated = defaultKeyValidationHandler.validateScopes(param1);
        boolean isScopeValidated_default = defaultKeyValidationHandler.validateScopes(param2);

        Assert.assertTrue("Scope validation fails for NOT default API!", isScopeValidated);
        Assert.assertTrue("Scope validation fails for default API!", isScopeValidated_default);

    }
}

@@ -2315,15 +2315,75 @@
  = %22
-apiname
+test_api%22;%0A    private final String DEFAULT_API_NAME = %22default_api
 %22;%0A 
@@ -5269,25 +5269,24 @@
 TTP_VERB);%0A%0A
-%0A
         Subs
@@ -6256,17 +6256,16 @@
 (api);%0A%0A
-%0A
         
@@ -6599,25 +6599,22 @@
 for 
-NOT default API!%22
+API %22+API_NAME
 , is
@@ -6699,10 +6699,27 @@
  API
-!%22
+ %22+DEFAULT_API_NAME
 , is

Add clear assert messages and fix formatting issues