package net.kencochrane.raven.appengine.event.helper;

import com.google.apphosting.api.ApiProxy;
import mockit.*;
import net.kencochrane.raven.event.EventBuilder;
import org.testng.annotations.Test;

import java.util.Collections;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class AppEngineEventBuilderHelperTest {
    @Tested
    private AppEngineEventBuilderHelper eventBuilderHelper;
    @Injectable
    private EventBuilder mockEventBuilder;
    @Mocked("getCurrentEnvironment")
    private ApiProxy mockApiProxy;

    @Test
    public void ensureHostnameDefineByApiProxyEnvironment(
            @Injectable("d7b8f251-ebe1-446f-8549-2b37982bd548") final String mockHostname,
            @Injectable final ApiProxy.Environment mockEnvironment) {
        new NonStrictExpectations() {{
            mockEnvironment.getAttributes();
            result = Collections.singletonMap("com.google.appengine.runtime.default_version_hostname", mockHostname);
            ApiProxy.getCurrentEnvironment();
            result = mockEnvironment;
        }};

        eventBuilderHelper.helpBuildingEvent(mockEventBuilder);

        new Verifications() {{
            String hostname;
            mockEventBuilder.setServerName(hostname = withCapture());
            assertThat(hostname, is(mockHostname));
        }};
    }
}

@@ -48,16 +48,70 @@
 elper;%0A%0A
+import com.google.appengine.api.utils.SystemProperty;%0A
 import c
@@ -245,45 +245,126 @@
 ons.
-Test;%0A%0Aimport java.util.Collections;%0A
+BeforeMethod;%0Aimport org.testng.annotations.Test;%0A%0Aimport java.util.*;%0A%0Aimport static mockit.Deencapsulation.setField;
 %0Aimp
@@ -413,16 +413,62 @@
 rtThat;%0A
+import static org.hamcrest.Matchers.hasEntry;%0A
 import s
@@ -749,16 +749,529 @@
 piProxy;
+%0A    @Injectable%0A    private ApiProxy.Environment mockEnvironment;%0A    @Injectable%0A    private SystemProperty mockApplicationVersion;%0A%0A    @BeforeMethod%0A    public void setUp() throws Exception %7B%0A        setField(SystemProperty.class, %22applicationVersion%22, mockApplicationVersion);%0A%0A        new NonStrictExpectations() %7B%7B%0A            ApiProxy.getCurrentEnvironment();%0A            result = mockEnvironment;%0A            mockEnvironment.getAttributes();%0A            result = Collections.emptyMap();%0A        %7D%7D;%0A    %7D
 %0A%0A    @T
@@ -1426,76 +1426,8 @@
 name
-,%0A            @Injectable final ApiProxy.Environment mockEnvironment
 ) %7B%0A
@@ -1632,92 +1632,8 @@
 e);%0A
-            ApiProxy.getCurrentEnvironment();%0A            result = mockEnvironment;%0A
     
@@ -1906,10 +1906,923 @@
 ;%0A    %7D%0A
+%0A    @Test%0A    public void ensureApplicationVersionIsAddedAsTag(%0A            @Injectable(%22dc485fa3-fc23-4e6c-b374-0d05d248e5a5%22) final String version) throws Exception %7B%0A        new NonStrictExpectations() %7B%7B%0A            mockApplicationVersion.get();%0A            result = version;%0A        %7D%7D;%0A%0A        eventBuilderHelper.helpBuildingEvent(mockEventBuilder);%0A%0A        new Verifications() %7B%7B%0A            List%3CString%3E tagNames = new LinkedList%3C%3E();%0A            List%3CString%3E tagValues = new LinkedList%3C%3E();%0A            mockEventBuilder.addTag(withCapture(tagNames), withCapture(tagValues));%0A%0A            Map%3CString, String%3E tags = new HashMap%3C%3E();%0A            for (int i = 0; i %3C tagNames.size(); i++) %7B%0A                String tagName = tagNames.get(i);%0A                tags.put(tagName, tagValues.get(i));%0A            %7D%0A%0A            assertThat(tags, hasEntry(%22GAE Application Version%22, version));%0A        %7D%7D;%0A    %7D%0A%0A
 %7D%0A

Add test for Application Version tag in GAE