package org.openengsb.core.services.internal;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;
import org.openengsb.core.api.OsgiUtilsService;
import org.openengsb.core.api.remote.MethodCall;
import org.openengsb.core.api.remote.MethodResult;
import org.openengsb.core.api.remote.RequestHandler;
import org.openengsb.core.common.OpenEngSBCoreServices;
import org.openengsb.core.common.util.DefaultOsgiUtilsService;
import org.openengsb.core.test.AbstractOsgiMockServiceTest;
import org.osgi.framework.BundleContext;

import com.google.common.collect.ImmutableMap;

public class RequestHandlerImplTest extends AbstractOsgiMockServiceTest {

    private DefaultOsgiUtilsService serviceUtils;
    private RequestHandler requestHandler;

    public static interface TestInterface {
        Integer test(Integer arg);
    }

    @Before
    public void setup() throws Exception {
        requestHandler = new RequestHandlerImpl();
    }

    private TestInterface mockServiceWithProps(Map<String, Object> propData) {
        TestInterface mockService = mock(TestInterface.class);
        Dictionary<String, Object> props = new Hashtable<String, Object>(propData);
        registerService(mockService, props, TestInterface.class);
        return mockService;
    }

    @Test
    public void testCallByServiceId_shouldCallService() throws Exception {
        TestInterface mockService = mockService(TestInterface.class, "testid");
        when(mockService.test(anyInt())).thenReturn(21);

        Map<String, String> metaData = ImmutableMap.of("serviceId", "testid");
        MethodCall c = new MethodCall("test", new Object[]{ 42 }, metaData);
        MethodResult result = requestHandler.handleCall(c);

        verify(mockService).test(42);
        assertThat(result.getClassName(), is(Integer.class.getName()));
        assertThat((Integer) result.getArg(), is(21));
    }

    @Test
    public void testCallByFilter_shouldCallService() throws Exception {
        Map<String, Object> propData = ImmutableMap.of("testprop", (Object) new String[]{ "bla", "bleh", });
        TestInterface mockService = mockServiceWithProps(propData);
        when(mockService.test(anyInt())).thenReturn(21);

        Map<String, String> metaData = ImmutableMap.of("serviceFilter", "(testprop=bla)");
        MethodCall c = new MethodCall("test", new Object[]{ 42 }, metaData);
        MethodResult result = requestHandler.handleCall(c);

        verify(mockService).test(42);
        assertThat(result.getClassName(), is(Integer.class.getName()));
        assertThat((Integer) result.getArg(), is(21));
    }

    @Test
    public void testCallByFilterAndServiceId_shouldCallService() throws Exception {
        Map<String, Object> propData =
            ImmutableMap.of("testprop", (Object) new String[]{ "bla", "bleh", }, "id", "xxx");
        TestInterface mockService = mockServiceWithProps(propData);
        when(mockService.test(anyInt())).thenReturn(21);

        Map<String, String> metaData = ImmutableMap.of("serviceFilter", "(testprop=bla)", "serviceId", "xxx");
        MethodCall c = new MethodCall("test", new Object[]{ 42 }, metaData);
        MethodResult result = requestHandler.handleCall(c);

        verify(mockService).test(42);
        assertThat(result.getClassName(), is(Integer.class.getName()));
        assertThat((Integer) result.getArg(), is(21));
    }

    @Test
    public void testCallButBothAttrsNull_shouldThrowException() throws Exception {
        Map<String, Object> propData =
            ImmutableMap.of("testprop", (Object) new String[]{ "bla", "bleh", });
        TestInterface mockService = mockServiceWithProps(propData);

        MethodCall c = new MethodCall("test", new Object[]{ 42 });
        try {
            requestHandler.handleCall(c);
            fail("IllegalArgumentException expected");
        } catch (IllegalArgumentException e) {
            verifyZeroInteractions(mockService);
        }
    }

    @Override
    protected void setBundleContext(BundleContext bundleContext) {
        serviceUtils = new DefaultOsgiUtilsService();
        serviceUtils.setBundleContext(bundleContext);
        OpenEngSBCoreServices.setOsgiServiceUtils(serviceUtils);
        registerService(serviceUtils, new Hashtable<String, Object>(), OsgiUtilsService.class);
    }

}

@@ -1,8 +1,838 @@
+/**%0A * Licensed to the Austrian Association for Software Tool Integration (AASTI)%0A * under one or more contributor license agreements. See the NOTICE file%0A * distributed with this work for additional information regarding copyright%0A * ownership. The AASTI licenses this file to you under the Apache License,%0A * Version 2.0 (the %22License%22); you may not use this file except in compliance%0A * with the License. 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, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0A
 package 

fix missing licences