<commit_before>/**
 * Copyright (c) 2014 openHAB UG (haftungsbeschraenkt) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipse.smarthome.core.thing.setup

import static org.hamcrest.CoreMatchers.*
import static org.junit.Assert.*
import static org.junit.matchers.JUnitMatchers.*

import org.eclipse.smarthome.config.core.Configuration
import org.eclipse.smarthome.core.items.GroupItem
import org.eclipse.smarthome.core.items.ItemRegistry
import org.eclipse.smarthome.core.thing.ChannelUID
import org.eclipse.smarthome.core.thing.Thing
import org.eclipse.smarthome.core.thing.ThingRegistry
import org.eclipse.smarthome.core.thing.ThingTypeUID
import org.eclipse.smarthome.core.thing.ThingUID
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler
import org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory
import org.eclipse.smarthome.core.thing.binding.ThingHandler
import org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory
import org.eclipse.smarthome.core.thing.binding.ThingTypeProvider
import org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry
import org.eclipse.smarthome.core.thing.link.ItemThingLinkRegistry
import org.eclipse.smarthome.core.thing.type.ChannelDefinition
import org.eclipse.smarthome.core.thing.type.ChannelGroupDefinition
import org.eclipse.smarthome.core.thing.type.ChannelGroupType
import org.eclipse.smarthome.core.thing.type.ChannelGroupTypeUID
import org.eclipse.smarthome.core.thing.type.ChannelType
import org.eclipse.smarthome.core.thing.type.ChannelTypeUID
import org.eclipse.smarthome.core.thing.type.ThingType
import org.eclipse.smarthome.core.types.Command
import org.eclipse.smarthome.test.OSGiTest
import org.junit.Before
import org.junit.Test
import org.osgi.service.component.ComponentContext


/**
 * ThingSetupManagerTest is a test for the ThingSetupManager class.
 * 
 * @author <NAME> - Initial contribution
 * @author <NAME> - Thing type constructor modified because of thing properties introduction
 */
class ThingSetupManagerOSGiTest extends OSGiTest {
    
    def ThingSetupManager thingSetupManager
    def ThingRegistry thingRegistry
    def ItemRegistry itemRegistry
    def ItemChannelLinkRegistry itemChannelLinkRegistry
    def ItemThingLinkRegistry itemThingLinkRegistry
    
    class DefaultThingHandler extends BaseThingHandler {

        public DefaultThingHandler(Thing thing) {
            super(thing)
        }
        
        @Override
        public void handleCommand(ChannelUID channelUID, Command command) {
        }
        
    } 
    
    class DefaultThingHandlerFactory extends BaseThingHandlerFactory {

        @Override
        public boolean supportsThingType(ThingTypeUID thingTypeUID) {
            return true;
        }

        @Override
        protected ThingHandler createHandler(Thing thing) {
            return new DefaultThingHandler(thing);
        }
        
    } 
    
    @Before
    void setup() {
        registerVolatileStorageService()
        thingSetupManager = getService(ThingSetupManager)
        thingRegistry = getService(ThingRegistry)
        itemRegistry = getService(ItemRegistry)
        itemChannelLinkRegistry = getService(ItemChannelLinkRegistry)
        itemThingLinkRegistry = getService(ItemThingLinkRegistry)
        def componentContext = [getBundleContext: {bundleContext}] as ComponentContext
        def thingHandlerFactory = new DefaultThingHandlerFactory()
        thingHandlerFactory.activate(componentContext)
        registerService(thingHandlerFactory, ThingHandlerFactory.class.getName())
        
        def thingTypeUID1 = new ThingTypeUID("binding:thing-type")
        def thingTypeUID2 = new ThingTypeUID("binding:thing-type-with-channel-groups")
        
        def channelType1 = new ChannelType(new ChannelTypeUID("binding:channelType1"), false, "String", "label", "", "light", [] as Set, null, null)
        def channelType2 = new ChannelType(new ChannelTypeUID("binding:channelType2"), true, "String", "label", "", "light", [] as Set, null, null)
        
        
        def channelDefinitions = [
            new ChannelDefinition("1", channelType1),
            new ChannelDefinition("2", channelType1),
            new ChannelDefinition("3", channelType2),
        ] as List
        
        def ChannelGroupType channelGroupType = new ChannelGroupType(new ChannelGroupTypeUID("binding:channelGroupType"), false, "Group", null, channelDefinitions)
    
        def channelGroupDefinitions = [
            new ChannelGroupDefinition("group1", channelGroupType),
            new ChannelGroupDefinition("group2", channelGroupType),
        ] as List
    
        registerService([
            getThingTypes: {
                return [
                    new ThingType(thingTypeUID1, null, "label", null, channelDefinitions, null, null, null),
                    new ThingType(thingTypeUID2, null, "label", null, null, channelGroupDefinitions, null, null)
                 ]
            } 
        ] as ThingTypeProvider)
    } 
    
	@Test
	void 'addThingWithoutChannelsItems'() {
        def thingUID = new ThingUID("binding", "thing-type", "thing")
        thingSetupManager.addThing(thingUID, new Configuration(), null, "MyThing", [] as List, false)
        def items = itemRegistry.getItems();
        assertThat items.size(), is(1)
        assertThat itemThingLinkRegistry.isLinked(items[0].name, thingUID), is(true)
	}
    
    @Test
    void 'addThingWithChannelsItems'() {
        def thingUID = new ThingUID("binding", "thing-type", "thing")
        thingSetupManager.addThing(thingUID, new Configuration(), null, "MyThing", [] as List, true)
        
        def items = itemRegistry.getItems();
        assertThat items.size(), is(3)
        
        def linkedThingGroupItemName = itemThingLinkRegistry.getLinkedItems(thingUID).first()
        def linkedThingGroupItem = itemRegistry.get(linkedThingGroupItemName)
        assertThat linkedThingGroupItem.label, is(equalTo("MyThing"))
        
        def linkedItemName = itemChannelLinkRegistry.getLinkedItems(new ChannelUID(thingUID, "1")).first()
        def linkedItem = itemRegistry.get(linkedItemName)
        
        assertThat linkedItem.label, is(null)
        assertThat linkedItem.type, is(equalTo("String"))
        assertThat linkedItem.category, is(equalTo("light"))
    }
    
    @Test
    void 'addThingWithChannelsGroups'() {
        def thingUID = new ThingUID("binding", "thing-type-with-channel-groups", "thing")
        thingSetupManager.addThing(thingUID, new Configuration(), null, "MyThing", [] as List, true)
        
        assertThat itemRegistry.getItems().size(), is(7)
        assertThat itemChannelLinkRegistry.getAll().size(), is(4)
        
        def thing = thingSetupManager.getThing(thingUID)
        def item = thing.linkedItem
        
        assertThat item.getMembers().size(), is(2)
        def firstChannelGroupItem = item.getMembers().first()
        assertThat firstChannelGroupItem, is(instanceOf(GroupItem))
        assertThat firstChannelGroupItem.getMembers().size(), is(2)
    }
    
    @Test<commit_msg>fixed unit test<commit_after>/**
 * Copyright (c) 2014 openHAB UG (haftungsbeschraenkt) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipse.smarthome.core.thing.setup

import static org.hamcrest.CoreMatchers.*
import static org.junit.Assert.*
import static org.junit.matchers.JUnitMatchers.*

import org.eclipse.smarthome.config.core.Configuration
import org.eclipse.smarthome.core.items.GroupItem
import org.eclipse.smarthome.core.items.ItemRegistry
import org.eclipse.smarthome.core.thing.ChannelUID
import org.eclipse.smarthome.core.thing.Thing
import org.eclipse.smarthome.core.thing.ThingRegistry
import org.eclipse.smarthome.core.thing.ThingTypeUID
import org.eclipse.smarthome.core.thing.ThingUID
import org.eclipse.smarthome.core.thing.binding.BaseThingHandler
import org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory
import org.eclipse.smarthome.core.thing.binding.ThingHandler
import org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory
import org.eclipse.smarthome.core.thing.binding.ThingTypeProvider
import org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry
import org.eclipse.smarthome.core.thing.link.ItemThingLinkRegistry
import org.eclipse.smarthome.core.thing.type.ChannelDefinition
import org.eclipse.smarthome.core.thing.type.ChannelGroupDefinition
import org.eclipse.smarthome.core.thing.type.ChannelGroupType
import org.eclipse.smarthome.core.thing.type.ChannelGroupTypeUID
import org.eclipse.smarthome.core.thing.type.ChannelType
import org.eclipse.smarthome.core.thing.type.ChannelTypeUID
import org.eclipse.smarthome.core.thing.type.ThingType
import org.eclipse.smarthome.core.types.Command
import org.eclipse.smarthome.test.OSGiTest
import org.junit.Before
import org.junit.Test
import org.osgi.service.component.ComponentContext


/**
 * ThingSetupManagerTest is a test for the ThingSetupManager class.
 * 
 * @author <NAME> - Initial contribution
 * @author <NAME> - Thing type constructor modified because of thing properties introduction
 */
class ThingSetupManagerOSGiTest extends OSGiTest {
    
    def ThingSetupManager thingSetupManager
    def ThingRegistry thingRegistry
    def ItemRegistry itemRegistry
    def ItemChannelLinkRegistry itemChannelLinkRegistry
    def ItemThingLinkRegistry itemThingLinkRegistry
    
    class DefaultThingHandler extends BaseThingHandler {

        public DefaultThingHandler(Thing thing) {
            super(thing)
        }
        
        @Override
        public void handleCommand(ChannelUID channelUID, Command command) {
        }
        
    } 
    
    class DefaultThingHandlerFactory extends BaseThingHandlerFactory {

        @Override
        public boolean supportsThingType(ThingTypeUID thingTypeUID) {
            return true;
        }

        @Override
        protected ThingHandler createHandler(Thing thing) {
            return new DefaultThingHandler(thing);
        }
        
    } 
    
    @Before
    void setup() {
        registerVolatileStorageService()
        thingSetupManager = getService(ThingSetupManager)
        thingRegistry = getService(ThingRegistry)
        itemRegistry = getService(ItemRegistry)
        itemChannelLinkRegistry = getService(ItemChannelLinkRegistry)
        itemThingLinkRegistry = getService(ItemThingLinkRegistry)
        def componentContext = [getBundleContext: {bundleContext}] as ComponentContext
        def thingHandlerFactory = new DefaultThingHandlerFactory()
        thingHandlerFactory.activate(componentContext)
        registerService(thingHandlerFactory, ThingHandlerFactory.class.getName())
        
        def thingTypeUID1 = new ThingTypeUID("binding:thing-type")
        def thingTypeUID2 = new ThingTypeUID("binding:thing-type-with-channel-groups")
        
        def channelType1 = new ChannelType(new ChannelTypeUID("binding:channelType1"), false, "String", "label", "", "light", [] as Set, null, null)
        def channelType2 = new ChannelType(new ChannelTypeUID("binding:channelType2"), true, "String", "label", "", "light", [] as Set, null, null)
        
        
        def channelDefinitions = [
            new ChannelDefinition("1", channelType1),
            new ChannelDefinition("2", channelType1),
            new ChannelDefinition("3", channelType2),
        ] as List
        
        def ChannelGroupType channelGroupType = new ChannelGroupType(new ChannelGroupTypeUID("binding:channelGroupType"), false, "Group", null, channelDefinitions)
    
        def channelGroupDefinitions = [
            new ChannelGroupDefinition("group1", channelGroupType),
            new ChannelGroupDefinition("group2", channelGroupType),
        ] as List
    
        registerService([
            getThingTypes: {
                return [
                    new ThingType(thingTypeUID1, null, "label", null, channelDefinitions, null, null, null),
                    new ThingType(thingTypeUID2, null, "label", null, null, channelGroupDefinitions, null, null)
                 ]
            } 
        ] as ThingTypeProvider)
    } 
    
	@Test
	void 'addThingWithoutChannelsItems'() {
        def thingUID = new ThingUID("binding", "thing-type", "thing")
        thingSetupManager.addThing(thingUID, new Configuration(), null, "MyThing", [] as List, false)
        def items = itemRegistry.getItems();
        assertThat items.size(), is(1)
        assertThat itemThingLinkRegistry.isLinked(items[0].name, thingUID), is(true)
	}
    
    @Test
    void 'addThingWithChannelsItems'() {
        def thingUID = new ThingUID("binding", "thing-type", "thing")
        thingSetupManager.addThing(thingUID, new Configuration(), null, "MyThing", [] as List, true)
        
        def items = itemRegistry.getItems();
        assertThat items.size(), is(3)
        
        def linkedThingGroupItemName = itemThingLinkRegistry.getLinkedItems(thingUID).first()
        def linkedThingGroupItem = itemRegistry.get(linkedThingGroupItemName)
        assertThat linkedThingGroupItem.label, is(equalTo("MyThing"))
        
        def linkedItemName = itemChannelLinkRegistry.getLinkedItems(new ChannelUID(thingUID, "1")).first()
        def linkedItem = itemRegistry.get(linkedItemName)
        
        assertThat linkedItem.label, is(equalTo("label"))
        assertThat linkedItem.type, is(equalTo("String"))
        assertThat linkedItem.category, is(equalTo("light"))
    }
    
    @Test
    void 'addThingWithChannelsGroups'() {
        def thingUID = new ThingUID("binding", "thing-type-with-channel-groups", "thing")
        thingSetupManager.addThing(thingUID, new Configuration(), null, "MyThing", [] as List, true)
        
        assertThat itemRegistry.getItems().size(), is(7)
        assertThat itemChannelLinkRegistry.getAll().size(), is(4)
        
        def thing = thingSetupManager.getThing(thingUID)
        def item = thing.linkedItem
        
        assertThat item.getMembers().size(), is(2)
        def firstChannelGroupItem = item.getMembers().first()
        assertThat firstChannelGroupItem, is(instanceOf(GroupItem))
        assertThat firstChannelGroupItem.getMembers().size(), is(2)
    }
    
    @Test
{'fraction_non_alphanumeric': 0.0919978225367447, 'fraction_numerical': 0.004354926510615134, 'mean_word_length': 3.417493237150586, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2377445', 'n_tokens_mistral': 4012, 'n_tokens_neox': 3949, 'n_words': 831}