### Question:
IntercomMetricCollector implements MetricCollector { @Override public MetricUser getUser(final String userId) { Check.isNotNull("userId", userId); User intercomUser = null; try { intercomUser = findIntercomUserByUserId(userId); } catch (final Exception e) { logger.info("Failed to get user with Id: {} from Intercom - {}", userId, e.getMessage()); throw new MetricUserNotFoundException(userId); } if (intercomUser == null) { throw new MetricUserNotFoundException(userId); } else { return new MetricUser(intercomUser.getUserId(), getUserOrganisations(intercomUser), intercomUser.getName(), intercomUser.getEmail(), getCustomAttributes(intercomUser)); } } IntercomMetricCollector(final String personalAccessToken); @Override void createOrganisation(final MetricOrganisation organisation); @Override void updateOrganisation(final MetricOrganisation organisation); @Override void tagOrganisation(final String tagName, final MetricOrganisation metricOrganisation); @Override void createUser(final MetricUser user); @Override void updateUser(final MetricUser user); @Override void addCustomAttributesToUser(final String userId, final Map<String, Object> customAttributes); @Override void addOrganisationToUser(final String userId, final String organisationId); @Override void removeOrganisationFromUser(final String userId, final String organisationId); @Override void deleteUser(final String userId); @Override void sendMetric(final Metric metric); @Override MetricUser getUser(final String userId); @Override void convertExistingContactToUser(final String contactId, final MetricUser metricUser);  }### Answer:
@Test public void shouldReturnUser_withUserId() { final String userId = randomId(); final Map<String, String> params = new HashMap<>(); params.put("user_id", userId); final User user = randomIntercomUser(); when(User.find(params)).thenReturn(user); final Map<String, CustomAttribute> mockCustomAttributes = mock(Map.class); user.setCustomAttributes(mockCustomAttributes); final MetricUser result = intercomMetricCollector.getUser(userId); assertNotNull(result); assertThat(result.id(), is(user.getUserId())); user.getCompanyCollection().getPage().forEach(company -> { assertTrue(result.organisationIds().contains(company.getCompanyID())); }); assertThat(result.name(), is(user.getName())); assertThat(result.emailAddress(), is(user.getEmail())); assertThat(result.customAttributes(), is(mockCustomAttributes)); }
@Test public void shouldNotReturnUser_withNullUserId() { final String userId = null; ValidationException thrownException = null; try { intercomMetricCollector.getUser(userId); } catch (final ValidationException e) { thrownException = e; } assertNotNull(thrownException); }
@Test public void shouldNotReturnUser_withExceptionThrownByIntercom() { final String userId = randomId(); final Map<String, String> params = new HashMap<>(); params.put("user_id", userId); when(User.find(params)).thenThrow(IntercomException.class); MetricUserNotFoundException thrownException = null; try { intercomMetricCollector.getUser(userId); } catch (final MetricUserNotFoundException e) { thrownException = e; } assertNotNull(thrownException); }
@Test public void shouldNotReturnUser_withNullUserReturnedByIntercom() { final String userId = randomId(); final Map<String, String> params = new HashMap<>(); params.put("user_id", userId); final User user = null; when(User.find(params)).thenReturn(user); MetricUserNotFoundException thrownException = null; try { intercomMetricCollector.getUser(userId); } catch (final MetricUserNotFoundException e) { thrownException = e; } assertNotNull(thrownException); }