package org.wso2.carbon.registry.activities.services;

import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.transport.http.HTTPConstants;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.powermock.api.mockito.PowerMockito;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.registry.common.beans.ActivityBean;
import org.wso2.carbon.registry.core.LogEntry;
import org.wso2.carbon.registry.core.RegistryConstants;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.jdbc.realm.RegistryRealm;
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.user.api.RealmConfiguration;
import org.wso2.carbon.user.core.UserStoreException;
import org.wso2.carbon.user.core.UserStoreManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.Date;

import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;


public class ActivityServiceTest {

    @Before
    public void setup() throws Exception {
        UserRegistry userRegistry = mock(UserRegistry.class);
        when(userRegistry.getUserName()).thenReturn("admin");
        RegistryRealm registryRealm = PowerMockito.mock(RegistryRealm.class);
        UserStoreManager userStoreManager = PowerMockito.mock(UserStoreManager.class);
        when(userStoreManager.getRoleListOfUser("admin"))
                .thenReturn(new String[]{"admin", "internal/everyone", "internal/publisher"});
        when(registryRealm.getUserStoreManager()).thenReturn(userStoreManager);
        RealmConfiguration realmConfiguration = new RealmConfiguration();
        realmConfiguration.setAdminRoleName("admin");
        when(registryRealm.getRealmConfiguration()).thenReturn(realmConfiguration);
        when(userRegistry.getUserRealm()).thenReturn(registryRealm);
        doReturn(true).when(userRegistry).resourceExists(anyString());
        LogEntry[] resultEntries = TestUtils.readLogEntries("add-log-entry.csv");
        doReturn(resultEntries).when(userRegistry).getLogs(anyString(), eq(0),
                anyString(), (Date)anyObject(), (Date)anyObject(), anyBoolean());

        MessageContext messageContext = new MessageContext();
        HttpServletRequest servletRequest = mock(HttpServletRequest.class);
        HttpSession session = mock(HttpSession.class);
        when(session.getAttribute(RegistryConstants.ROOT_REGISTRY_INSTANCE)).thenReturn(userRegistry);
        when(servletRequest.getSession()).thenReturn(session);
        messageContext.setProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST, servletRequest);
        AxisService axisService = new AxisService();
        axisService.addParameter(new Parameter(CarbonConstants.ADMIN_SERVICE_PARAM_NAME, "true"));
        messageContext.setAxisService(axisService);
        MessageContext.setCurrentMessageContext(messageContext);
    }

    @Test
    public void getActivities() throws Exception {
        ActivityService activityService = new ActivityService();
        String username = "admin";
        String resourcePath = "/_system/governance/apimgt/application";
        String fromDate = "10/10/2016";
        String toDate = "10/10/2017";
        String filter = "resourceAdd";
        String pgStr = null;
        String sessionId = null;
        ActivityBean activityBean = activityService.getActivities(username, resourcePath, fromDate, toDate, filter,
                pgStr, null);
        String[] activities = activityBean.getActivity();
        String errorMessage = activityBean.getErrorMessage();
        assertNotNull(activities);
        assertNull(errorMessage);
        assertEquals(2, activities.length);
    }

    @Test
    public void getActivitiesFailure() throws Exception {
        ActivityService activityService = new ActivityService();
        String username = "admin";
        String resourcePath = "/_system/governance/apimgt/application";
        String fromDate = "10/10/2016";
        String toDate = "10/10/2017";
        String filter = "all";
        String pgStr = null;
        String sessionId = null;
        ActivityBean activityBean = activityService.getActivities(username, resourcePath, fromDate, toDate, filter,
                pgStr, null);
        assertNull(activityBean);
    }

}

@@ -1,8 +1,687 @@
+/*%0A *  Copyright (c) 2017, 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
 package 
@@ -731,43 +731,8 @@
 s;%0A%0A
-import org.apache.axis2.AxisFault;%0A
 impo
@@ -929,33 +929,8 @@
 ts;%0A
-import org.junit.Assert;%0A
 impo
@@ -1226,75 +1226,8 @@
 ts;%0A
-import org.wso2.carbon.registry.core.exceptions.RegistryException;%0A
 impo
@@ -1340,24 +1340,24 @@
 erRegistry;%0A
+
 import org.w
@@ -1400,61 +1400,8 @@
 on;%0A
-import org.wso2.carbon.user.core.UserStoreException;%0A
 impo

Add missing license header