<commit_before>package brooklyn.entity.dns;

import static org.testng.AssertJUnit.*
import groovy.lang.MetaClass

import java.util.Map
import java.util.Set

import org.testng.annotations.AfterMethod
import org.testng.annotations.BeforeMethod
import org.testng.annotations.Test

import brooklyn.entity.Entity
import brooklyn.entity.basic.AbstractApplication
import brooklyn.entity.basic.DynamicGroup
import brooklyn.entity.group.DynamicFabric
import brooklyn.entity.trait.Startable
import brooklyn.location.Location
import brooklyn.location.basic.GeneralPurposeLocation
import brooklyn.location.basic.SshMachineLocation
import brooklyn.test.entity.TestEntity
import brooklyn.util.internal.EntityStartUtils

public class AbstractGeoDnsServiceTest {
    private static final String MONTEREY_WEST_IP = "172.16.31.10";
    private static final String MONTEREY_EAST_IP = "172.16.31.10";
    private static final double CALIFORNIA_LATITUDE = 37.43472, CALIFORNIA_LONGITUDE = -121.89500;
    private static final double NEW_YORK_LATITUDE = 41.10361, NEW_YORK_LONGITUDE = -73.79583;
    
    private static final Location CALIFORNIA = new GeneralPurposeLocation(
        name: "California", latitude: CALIFORNIA_LATITUDE, longitude: CALIFORNIA_LONGITUDE);
    
    private static final Location NEW_YORK = new GeneralPurposeLocation(
        name: "New York", latitude: NEW_YORK_LATITUDE, longitude: NEW_YORK_LONGITUDE);

    private static final Location CALIFORNIA_MACHINE = new SshMachineLocation(
        name: "California machine", address: MONTEREY_WEST_IP, parentLocation: CALIFORNIA); 
        
    private static final Location NEW_YORK_MACHINE = new SshMachineLocation(
        name: "New York machine", address: MONTEREY_EAST_IP, parentLocation: NEW_YORK); 
    
    
    private AbstractApplication app;
    private DynamicFabric fabric
    private AbstractGeoDnsService geoDns;
    

    @BeforeMethod
    public void setup() {
        def template = { properties -> new TestEntity(properties) }
        app = new AbstractApplication() { };
        fabric = new DynamicFabric(owner:app, newEntity:template);
    }

    @AfterMethod
    public void shutdown() {
        if (fabric != null && fabric.getAttribute(Startable.SERVICE_UP)) {
            EntityStartUtils.stopEntity(fabric)
        }
    }

    
    @Test(enabled=false)
    public void geoInfoOnLocations() {
        DynamicFabric fabric = new DynamicFabric([newEntity:{ Map properties -> return new TestEntity(properties) }], app)
        DynamicGroup testEntities = new DynamicGroup([:], app, { Entity e -> (e instanceof TestEntity) });
        geoDns = new TestService(app);
        geoDns.setTargetEntityProvider(testEntities);
        
        app.start( [ CALIFORNIA_MACHINE, NEW_YORK_MACHINE ] );
        
        // FIXME: remove this sleep once the location-polling mechanism has been replaced with proper subscriptions
        Thread.sleep(7000);
        
        assertTrue(geoDns.targetHostsByName.containsKey("California machine"));
        assertTrue(geoDns.targetHostsByName.containsKey("New York machine"));
    }
    
    @Test
    public void geoInfoNotFound() {
        // TODO
    }
    
    @Test
    public void emptyGroup() {
        // TODO
    }
    
    
    private class TestService extends AbstractGeoDnsService {
        public Map<String, HostGeoInfo> targetHostsByName = new LinkedHashMap<String, HostGeoInfo>();
        
        public TestService(properties=[:], Entity owner) {
            super(properties, owner);
        }
        
        @Override
        protected void reconfigureService(Set<HostGeoInfo> targetHosts) {
            targetHostsByName.clear();
            for (HostGeoInfo host : targetHosts) {
                if (host != null) targetHostsByName.put(host.displayName, host);
            }
        }
    }
    
}<commit_msg>ENGR-1588: Slightly improved geo-dns tests<commit_after>package brooklyn.entity.dns;

import static org.testng.Assert.*
import groovy.lang.MetaClass

import java.util.Map
import java.util.Set

import org.testng.annotations.AfterMethod
import org.testng.annotations.BeforeMethod
import org.testng.annotations.Test

import brooklyn.entity.Entity
import brooklyn.entity.basic.AbstractApplication
import brooklyn.entity.basic.DynamicGroup
import brooklyn.entity.group.DynamicFabric
import brooklyn.entity.trait.Startable
import brooklyn.location.Location
import brooklyn.location.basic.GeneralPurposeLocation
import brooklyn.location.basic.SshMachineLocation
import brooklyn.test.entity.TestEntity
import brooklyn.util.internal.EntityStartUtils


public class AbstractGeoDnsServiceTest {
    private static final String WEST_IP = "172.16.31.10";
    private static final String EAST_IP = "172.16.31.10";
    private static final double WEST_LATITUDE = 37.43472, WEST_LONGITUDE = -121.89500;
    private static final double EAST_LATITUDE = 41.10361, EAST_LONGITUDE = -73.79583;
    
    private static final Location WEST_PARENT = new GeneralPurposeLocation(
        name: "West parent", latitude: WEST_LATITUDE, longitude: WEST_LONGITUDE);
    private static final Location WEST_CHILD = new SshMachineLocation(
        name: "West child", address: WEST_IP, parentLocation: WEST_PARENT); 
    private static final Location WEST_CHILD_WITH_LOCATION = new SshMachineLocation(
        name: "West child with location", address: WEST_IP, parentLocation: WEST_PARENT,
        latitude: WEST_LATITUDE, longitude: WEST_LONGITUDE); 
    
    private static final Location EAST_PARENT = new GeneralPurposeLocation(
            name: "East parent", latitude: EAST_LATITUDE, longitude: EAST_LONGITUDE);
    private static final Location EAST_CHILD = new SshMachineLocation(
            name: "East child", address: EAST_IP, parentLocation: EAST_PARENT); 
    private static final Location EAST_CHILD_WITH_LOCATION = new SshMachineLocation(
        name: "East child with location", address: EAST_IP, parentLocation: EAST_PARENT,
        latitude: EAST_LATITUDE, longitude: EAST_LONGITUDE); 
    
    private AbstractApplication app;
    private DynamicFabric fabric
    private AbstractGeoDnsService geoDns;
    

    @BeforeMethod
    public void setup() {
        def template = { properties -> new TestEntity(properties) }
        app = new AbstractApplication() { };
        fabric = new DynamicFabric(owner:app, newEntity:template);
    }

    @AfterMethod
    public void shutdown() {
        if (fabric != null && fabric.getAttribute(Startable.SERVICE_UP)) {
            EntityStartUtils.stopEntity(fabric)
        }
    }

    
    @Test
    public void testGeoInfoOnLocation() {
        DynamicFabric fabric = new DynamicFabric([newEntity:{ Map properties -> return new TestEntity(properties) }], app)
        DynamicGroup testEntities = new DynamicGroup([:], app, { Entity e -> (e instanceof TestEntity) });
        geoDns = new TestService(app);
        geoDns.setTargetEntityProvider(testEntities);
        
        app.start( [ WEST_CHILD_WITH_LOCATION, EAST_CHILD_WITH_LOCATION ] );
        
        // FIXME: remove this sleep once the location-polling mechanism has been replaced with proper subscriptions
        Thread.sleep(7000);
        
        assertTrue(geoDns.targetHostsByName.containsKey("West child with location"));
        assertTrue(geoDns.targetHostsByName.containsKey("East child with location"));
    }
    
    @Test
    public void testGeoInfoOnParentLocation() {
        DynamicFabric fabric = new DynamicFabric([newEntity:{ Map properties -> return new TestEntity(properties) }], app)
        DynamicGroup testEntities = new DynamicGroup([:], app, { Entity e -> (e instanceof TestEntity) });
        geoDns = new TestService(app);
        geoDns.setTargetEntityProvider(testEntities);
        
        app.start( [ WEST_CHILD, EAST_CHILD ] );
        
        // FIXME: remove this sleep once the location-polling mechanism has been replaced with proper subscriptions
        Thread.sleep(7000);
        
        assertTrue(geoDns.targetHostsByName.containsKey("West child"));
        assertTrue(geoDns.targetHostsByName.containsKey("East child"));
    }
    
    @Test
    public void testMissingGeoInfo() {
        // TODO
    }
    
    @Test
    public void testEmptyGroup() {
        // TODO
    }
    
    
    private class TestService extends AbstractGeoDnsService {
        public Map<String, HostGeoInfo> targetHostsByName = new LinkedHashMap<String, HostGeoInfo>();
        
        public TestService(properties=[:], Entity owner) {
            super(properties, owner);
        }
        
        @Override
        protected void reconfigureService(Set<HostGeoInfo> targetHosts) {
            targetHostsByName.clear();
            for (HostGeoInfo host : targetHosts) {
                if (host != null) targetHostsByName.put(host.displayName, host);
            }
        }
    }
    
}
{'fraction_non_alphanumeric': 0.08298568046002931, 'fraction_numerical': 0.012402751155710903, 'mean_word_length': 3.2278360343183983, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '7591587', 'n_tokens_mistral': 2692, 'n_tokens_neox': 2557, 'n_words': 601}