/*
 * #%L
 * %%
 * Copyright (C) 2011 - 2017 BMW Car IT GmbH
 * %%
 * 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.
 * #L%
 */
package itest.io.joynr.jeeintegration;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.util.concurrent.ScheduledExecutorService;

import javax.annotation.Resource;
import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.spi.TestResult;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.name.Names;

import io.joynr.capabilities.LocalCapabilitiesDirectory;
import io.joynr.jeeintegration.DefaultJoynrRuntimeFactory;
import io.joynr.jeeintegration.JoynrIntegrationBean;
import io.joynr.jeeintegration.JoynrStatusMetricsAggregator;
import io.joynr.jeeintegration.ServiceProviderDiscovery;
import io.joynr.jeeintegration.api.JeeIntegrationPropertyKeys;
import io.joynr.messaging.routing.MessageRouter;
import io.joynr.runtime.JoynrInjectionConstants;

/**
 * Integration tests for the JEE integration bean.
 */
@RunWith(Arquillian.class)
public class JeeIntegrationBeanTest {

	@Deployment
	public static JavaArchive createTestArchive() {
		// @formatter:off
		return ShrinkWrap.create(JavaArchive.class)
				.addClasses(ServiceProviderDiscovery.class, DefaultJoynrRuntimeFactory.class,
						JeeIntegrationJoynrTestConfigurationProvider.class, JoynrIntegrationBean.class,
						TestResult.class, JoynrStatusMetricsAggregator.class)
				.addAsManifestResource(new File("src/main/resources/META-INF/beans.xml"));
		// @formatter:on
	}

	@Inject
	private JoynrIntegrationBean joynrIntegrationBean;

	@Resource(name = JeeIntegrationPropertyKeys.JEE_MESSAGING_SCHEDULED_EXECUTOR_RESOURCE)
	private ScheduledExecutorService scheduledExecutorService;

	@Test
	public void testJoynrRuntimeAvailable() {
		Assert.assertNotNull(joynrIntegrationBean.getRuntime());
	}

	@Test
	public void testManagedScheduledExecutorServiceUsed() {
		assertNotNull(scheduledExecutorService);
		assertNotNull(joynrIntegrationBean);
		Injector joynrInjector = joynrIntegrationBean.getJoynrInjector();
		assertNotNull(joynrInjector);
		assertEquals(scheduledExecutorService, joynrInjector.getInstance(
				Key.get(ScheduledExecutorService.class, Names.named(JoynrInjectionConstants.JOYNR_SCHEDULER_CLEANUP))));
		assertEquals(scheduledExecutorService, joynrInjector
				.getInstance(Key.get(ScheduledExecutorService.class, Names.named(MessageRouter.SCHEDULEDTHREADPOOL))));
		assertEquals(scheduledExecutorService, joynrInjector
		             .getInstance(Key.get(ScheduledExecutorService.class, Names.named(LocalCapabilitiesDirectory.JOYNR_SCHEDULER_CAPABILITIES_FRESHNESS))));
	}

}

@@ -1496,24 +1496,75 @@
 rationBean;%0A
+import io.joynr.jeeintegration.JoynrStatusMetrics;%0A
 import io.jo
@@ -1608,24 +1608,24 @@
 Aggregator;%0A
-
 import io.jo
@@ -1732,16 +1732,81 @@
 tyKeys;%0A
+import io.joynr.messaging.mqtt.statusmetrics.MqttStatusReceiver;%0A
 import i
@@ -1894,16 +1894,62 @@
 nstants;
+%0Aimport io.joynr.statusmetrics.StatusReceiver;
 %0A%0A/**%0A *
@@ -2587,16 +2587,74 @@
 nBean;%0A%0A
+%09@Inject%0A%09private JoynrStatusMetrics joynrStatusMetrics;%0A%0A
 %09@Resour
@@ -3703,10 +3703,791 @@
 ));%0A%09%7D%0A%0A
+%09@Test%0A%09public void testJoynrStatusMetricsObjectIsUsedAsJoynrStatusReceiver() %7B%0A%09%09Injector joynrInjector = joynrIntegrationBean.getJoynrInjector();%0A%0A%09%09MqttStatusReceiver mqttStatusReceiver = joynrInjector.getInstance(MqttStatusReceiver.class);%0A%09%09StatusReceiver statusReceiver = joynrInjector.getInstance(StatusReceiver.class);%0A%0A%09%09assertNotNull(joynrStatusMetrics);%0A%0A%09%09// We cannot compare these objects directly using the == operator. The reason is that the joynrStatusMetrics object is%0A%09%09// wrapped by a java proxy. Therefore the objects are different. However, the toString() method is called on the%0A%09%09// underlying object.%0A%09%09assertEquals(mqttStatusReceiver.toString(), joynrStatusMetrics.toString());%0A%09%09assertEquals(statusReceiver.toString(), joynrStatusMetrics.toString());%0A%09%7D%0A
 %7D%0A

Add test for status receiver injection