/*
 * Copyright (c) OSGi Alliance (2014). All Rights Reserved.
 * 
 * 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.
 */

package org.osgi.test.cases.enocean;

import org.osgi.framework.Bundle;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.enocean.EnOceanHost;
import org.osgi.test.cases.enocean.utils.Fixtures;
import org.osgi.test.support.OSGiTestCaseProperties;
import org.osgi.test.support.sleep.Sleep;

/**
 * This class contains:
 * 
 * - testDeviceExportPersistency, tests device export persistency: registers a
 * device, get its chip ID (referred to as the original chip ID), stops, and
 * restarts the base driver, finally get the device’s new chip ID, and checks
 * that the original chip ID, and the new one are equal.
 * 
 * @author $Id$
 */
public class PersistencyTestCase extends AbstractEnOceanTestCase {

	/**
	 * Tests device export persistency.
	 * 
	 * @throws Exception
	 */
	public void testDeviceExportPersistency() throws Exception {
		ServiceRegistration sReg = Fixtures.registerDevice(getContext());

		/* Get CHIP_ID attributed by the driver from the given service PID. */
		ServiceReference hostRef = getContext().getServiceReference(EnOceanHost.class.getName());
		EnOceanHost defaultHost = (EnOceanHost) getContext().getService(hostRef);
		int originalChipId = defaultHost.getChipId(Fixtures.DEVICE_PID);

		Bundle baseDriver = getBaseDriverBundle();
		assertNotNull("baseDriver must not be null.", baseDriver);

		baseDriver.stop();
		Sleep.sleep(1000 * OSGiTestCaseProperties.getScaling());
		

		hostRef = getContext().getServiceReference(EnOceanHost.class.getName());
		assertNull("hostRef must be null, once the base driver is stopped.", hostRef);

		baseDriver.start();
		// TODO AAA: Replace the following line by a
		// "devices.waitForService();"...;
		Sleep.sleep(1000 * OSGiTestCaseProperties.getScaling());

		hostRef = getContext().getServiceReference(EnOceanHost.class.getName());
		assertNotNull("One EnOceanHost service must be registered (hostRef must not be null), once the base driver is started.", hostRef);
		defaultHost = (EnOceanHost) getContext().getService(hostRef);
		int newChipId = defaultHost.getChipId(Fixtures.DEVICE_PID);

		assertEquals("Original, and new chip ids mismatch.", originalChipId, newChipId);

		sReg.unregister();
		getContext().ungetService(hostRef);
	}

	private Bundle getBaseDriverBundle() {
		
		// TODO AAA: Search for the EnOceanHost service, then get the
		// corresponding bundle, and return it;
		
		Bundle[] bundles = getContext().getBundles();
		for (int i = 0; i < bundles.length; i++) {
			Bundle b = bundles[i];
			if (b.getSymbolicName().equals("org.osgi.impl.service.enocean")) {
				return b;
			}
		}
		return null;
	}

}

@@ -1927,29 +1927,27 @@
 river = 
-getBaseDriver
+hostRef.get
 Bundle()
@@ -2903,389 +2903,6 @@
 %09%7D%0A%0A
-%09private Bundle getBaseDriverBundle() %7B%0A%09%09%0A%09%09// TODO AAA: Search for the EnOceanHost service, then get the%0A%09%09// corresponding bundle, and return it;%0A%09%09%0A%09%09Bundle%5B%5D bundles = getContext().getBundles();%0A%09%09for (int i = 0; i %3C bundles.length; i++) %7B%0A%09%09%09Bundle b = bundles%5Bi%5D;%0A%09%09%09if (b.getSymbolicName().equals(%22org.osgi.impl.service.enocean%22)) %7B%0A%09%09%09%09return b;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return null;%0A%09%7D%0A%0A
 %7D%0A

Fix PersistencyTestCase's TODO task.