// Path: cheddar/cheddar-metrics-intercom/src/test/java/com/clicktravel/cheddar/metrics/intercom/random/data/RandomMetricDataGenerator.java
// public static MetricOrganisation randomMetricOrganisation() {
//     return randomMetricOrganisation(false);
// }
// 
// Path: cheddar/cheddar-metrics-intercom/src/test/java/com/clicktravel/cheddar/metrics/intercom/random/data/RandomMetricDataGenerator.java
// public static MetricOrganisation randomMetricOrganisationWithCreatedAt() {
//     return randomMetricOrganisation(true);
// }
// 
// Path: cheddar/cheddar-metrics-intercom/src/test/java/com/clicktravel/cheddar/metrics/intercom/random/data/RandomMetricDataGenerator.java
// public static MetricUser randomMetricUser() {
//     final int numberOfOrganisations = randomIntInRange(1, 10);
//     final List<String> organisationIds = new ArrayList<>();
//     for (int i = 0; i < numberOfOrganisations; i++) {
//         organisationIds.add(randomId());
//     }
//     return new MetricUser(randomId(), organisationIds, randomString(), randomEmailAddress(),
//             randomMetricCustomAttributes());
// }
// 
// Path: cheddar/cheddar-metrics/src/main/java/com/clicktravel/cheddar/metrics/MetricOrganisation.java
// public class MetricOrganisation {
// 
//     private final String id;
//     private final String name;
//     private DateTime createdAt;
// 
//     public MetricOrganisation(final String id, final String name) {
//         this.id = id;
//         this.name = name;
//     }
// 
//     public void setCreatedAt(final DateTime createdAt) {
//         this.createdAt = createdAt;
//     }
// 
//     public String id() {
//         return id;
//     }
// 
//     public String name() {
//         return name;
//     }
// 
//     public DateTime createdAt() {
//         return createdAt;
//     }
// 
// }
// 
// Path: cheddar/cheddar-metrics/src/main/java/com/clicktravel/cheddar/metrics/MetricUserNotFoundException.java
// public class MetricUserNotFoundException extends IllegalStateException {
// 
//     private static final long serialVersionUID = 5674381015114502022L;
// 
//     public MetricUserNotFoundException(final String metricUserId) {
//         super(String.format("Metric user with ID %s not found", metricUserId));
//     }
// }


import static com.clicktravel.cheddar.metrics.intercom.random.data.RandomIntercomDataGenerator.randomIntercomUser;
import static com.clicktravel.cheddar.metrics.intercom.random.data.RandomMetricDataGenerator.randomMetricOrganisation;
import static com.clicktravel.cheddar.metrics.intercom.random.data.RandomMetricDataGenerator.randomMetricOrganisationWithCreatedAt;
import static com.clicktravel.cheddar.metrics.intercom.random.data.RandomMetricDataGenerator.randomMetricUser;
import static com.clicktravel.common.random.Randoms.randomId;
import static com.clicktravel.common.random.Randoms.randomString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import java.util.HashMap;
import java.util.Map;
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.clicktravel.cheddar.metrics.MetricException;
import com.clicktravel.cheddar.metrics.MetricOrganisation;
import com.clicktravel.cheddar.metrics.MetricUser;
import com.clicktravel.cheddar.metrics.MetricUserNotFoundException;
import com.clicktravel.common.validation.ValidationException;
import io.intercom.api.*;
        final Map<String, CustomAttribute> customAttributes = mock(Map.class);
        when(metricToIntercomMapper.apply(metricUser.customAttributes())).thenReturn(customAttributes);
        // When
        intercomMetricCollector.updateUser(metricUser);

        // Then
        final ArgumentCaptor<User> userCaptor = ArgumentCaptor.forClass(User.class);
        verifyStatic(User.class);
        User.update(userCaptor.capture());
        final User intercomUser = userCaptor.getValue();
        assertThat(intercomUser.getId(), is(metricUser.id()));
        assertThat(intercomUser.getUserId(), is(metricUser.id()));
        assertThat(intercomUser.getUserId(), is(metricUser.id()));
        assertThat(intercomUser.getCustomAttributes(), is(customAttributes));
        assertThat(intercomUser.getCompanyCollection().getPage().size(), is(metricUser.organisationIds().size()));
        intercomUser.getCompanyCollection().getPage()
                .forEach(company -> assertTrue(metricUser.organisationIds().contains(company.getCompanyID())));
        DateTimeUtils.setCurrentMillisSystem();
    }

    @Test
    public void shouldAddCustomAttributesToUser_withUserIdAndCustomAttributes() {
        // Given
        final String userId = randomId();
        final User mockUser = mock(User.class);
        final Map<String, Object> customAttributes = mock(Map.class);
        final Map<String, String> params = new HashMap<>();
        params.put("user_id", userId);
        when(User.find(params)).thenReturn(mockUser);
        final Map<String, CustomAttribute> mockCustomAttributes = mock(Map.class);
// Path: cheddar/cheddar-metrics-intercom/src/test/java/com/clicktravel/cheddar/metrics/intercom/random/data/RandomMetricDataGenerator.java
// public static MetricOrganisation randomMetricOrganisation() {
//     return randomMetricOrganisation(false);
// }
// 
// Path: cheddar/cheddar-metrics-intercom/src/test/java/com/clicktravel/cheddar/metrics/intercom/random/data/RandomMetricDataGenerator.java
// public static MetricOrganisation randomMetricOrganisationWithCreatedAt() {
//     return randomMetricOrganisation(true);
// }
// 
// Path: cheddar/cheddar-metrics-intercom/src/test/java/com/clicktravel/cheddar/metrics/intercom/random/data/RandomMetricDataGenerator.java
// public static MetricUser randomMetricUser() {
//     final int numberOfOrganisations = randomIntInRange(1, 10);
//     final List<String> organisationIds = new ArrayList<>();
//     for (int i = 0; i < numberOfOrganisations; i++) {
//         organisationIds.add(randomId());
//     }
//     return new MetricUser(randomId(), organisationIds, randomString(), randomEmailAddress(),
//             randomMetricCustomAttributes());
// }
// 
// Path: cheddar/cheddar-metrics/src/main/java/com/clicktravel/cheddar/metrics/MetricOrganisation.java
// public class MetricOrganisation {
// 
//     private final String id;
//     private final String name;
//     private DateTime createdAt;
// 
//     public MetricOrganisation(final String id, final String name) {
//         this.id = id;
//         this.name = name;
//     }
// 
//     public void setCreatedAt(final DateTime createdAt) {
//         this.createdAt = createdAt;
//     }
// 
//     public String id() {
//         return id;
//     }
// 
//     public String name() {
//         return name;
//     }
// 
//     public DateTime createdAt() {
//         return createdAt;
//     }
// 
// }
// 
// Path: cheddar/cheddar-metrics/src/main/java/com/clicktravel/cheddar/metrics/MetricUserNotFoundException.java
// public class MetricUserNotFoundException extends IllegalStateException {
// 
//     private static final long serialVersionUID = 5674381015114502022L;
// 
//     public MetricUserNotFoundException(final String metricUserId) {
//         super(String.format("Metric user with ID %s not found", metricUserId));
//     }
// }


// Path: cheddar/cheddar-metrics-intercom/src/test/java/com/clicktravel/cheddar/metrics/intercom/IntercomMetricCollectorTest.java
import static com.clicktravel.cheddar.metrics.intercom.random.data.RandomIntercomDataGenerator.randomIntercomUser;
import static com.clicktravel.cheddar.metrics.intercom.random.data.RandomMetricDataGenerator.randomMetricOrganisation;
import static com.clicktravel.cheddar.metrics.intercom.random.data.RandomMetricDataGenerator.randomMetricOrganisationWithCreatedAt;
import static com.clicktravel.cheddar.metrics.intercom.random.data.RandomMetricDataGenerator.randomMetricUser;
import static com.clicktravel.common.random.Randoms.randomId;
import static com.clicktravel.common.random.Randoms.randomString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import java.util.HashMap;
import java.util.Map;
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.clicktravel.cheddar.metrics.MetricException;
import com.clicktravel.cheddar.metrics.MetricOrganisation;
import com.clicktravel.cheddar.metrics.MetricUser;
import com.clicktravel.cheddar.metrics.MetricUserNotFoundException;
import com.clicktravel.common.validation.ValidationException;
import io.intercom.api.*;

        final Map<String, CustomAttribute> customAttributes = mock(Map.class);
        when(metricToIntercomMapper.apply(metricUser.customAttributes())).thenReturn(customAttributes);
        // When
        intercomMetricCollector.updateUser(metricUser);

        // Then
        final ArgumentCaptor<User> userCaptor = ArgumentCaptor.forClass(User.class);
        verifyStatic(User.class);
        User.update(userCaptor.capture());
        final User intercomUser = userCaptor.getValue();
        assertThat(intercomUser.getId(), is(metricUser.id()));
        assertThat(intercomUser.getUserId(), is(metricUser.id()));
        assertThat(intercomUser.getUserId(), is(metricUser.id()));
        assertThat(intercomUser.getCustomAttributes(), is(customAttributes));
        assertThat(intercomUser.getCompanyCollection().getPage().size(), is(metricUser.organisationIds().size()));
        intercomUser.getCompanyCollection().getPage()
                .forEach(company -> assertTrue(metricUser.organisationIds().contains(company.getCompanyID())));
        DateTimeUtils.setCurrentMillisSystem();
    }

    @Test
    public void shouldAddCustomAttributesToUser_withUserIdAndCustomAttributes() {
        // Given
        final String userId = randomId();
        final User mockUser = mock(User.class);
        final Map<String, Object> customAttributes = mock(Map.class);
        final Map<String, String> params = new HashMap<>();
        params.put("user_id", userId);
        when(User.find(params)).thenReturn(mockUser);
        final Map<String, CustomAttribute> mockCustomAttributes = mock(Map.class);
        when(metricToIntercomMapper.apply(customAttributes)).thenReturn(mockCustomAttributes);