### Question:
IdpAssertionMetricsCollector { public void update(Assertion assertion) { String idpName = replaceNonAlphaNumericCharacters(assertion); String metricName = NOT_ON_OR_AFTER + idpName; Long maxNotOnOrAfter = findMaxNotOnOrAfter(assertion.getSubject().getSubjectConfirmations(), notOnOrAfterLatestMax.get(metricName)); notOnOrAfterLatestMax.put(metricName, maxNotOnOrAfter); updateMetricRegistry(metricName); } IdpAssertionMetricsCollector(MetricRegistry metricRegistry); void update(Assertion assertion);  }### Answer:
@Test public void shouldRegisterIdpNameFromAssertion() { MetricRegistry metricRegistry = new MetricRegistry(); IdpAssertionMetricsCollector idpAssertionMetricsCollector = new IdpAssertionMetricsCollector(metricRegistry); Assertion anAssertion = anAssertion() .withIssuer(anIssuer().withIssuerId("testIdP").build()) .buildUnencrypted(); idpAssertionMetricsCollector.update(anAssertion); assertThat(metricRegistry.getGauges().keySet()).contains("notOnOrAfter.testIdP"); }
@Test public void shouldNotRegisterIdpAlreadyExist() { MetricRegistry metricRegistry = mock(MetricRegistry.class); SortedMap<String, Gauge> gaugeMock= mock(SortedMap.class); when(gaugeMock.containsKey(any())).thenReturn(true); when(metricRegistry.getGauges()).thenReturn(gaugeMock); Assertion assertion = anAssertion() .withIssuer(anIssuer().withIssuerId("testIdP").build()) .buildUnencrypted(); IdpAssertionMetricsCollector idpAssertionMetricsCollector = new IdpAssertionMetricsCollector(metricRegistry); idpAssertionMetricsCollector.update(assertion); verify(metricRegistry, times(0)).register(any(), any()); }
@Test public void shouldCollectNotOnOrAfterValueFromAssertion() { DateTimeFreezer.freezeTime(); MetricRegistry metricRegistry = new MetricRegistry(); IdpAssertionMetricsCollector idpAssertionMetricsCollector = new IdpAssertionMetricsCollector(metricRegistry); DateTime notOnOrAfter = DateTime.now().plusMinutes(15); Assertion anAssertion = anAssertion() .withIssuer(anIssuer().withIssuerId("testIdP").build()) .withSubject(aSubject().withSubjectConfirmation(aSubjectConfirmation() .withSubjectConfirmationData(aSubjectConfirmationData() .withNotOnOrAfter(notOnOrAfter) .build()) .build()) .build()) .buildUnencrypted(); idpAssertionMetricsCollector.update(anAssertion); Gauge actual = metricRegistry.getGauges().get("notOnOrAfter.testIdP"); assertThat(actual.getValue()).isEqualTo(15L); }
@Test public void shouldGetMaxInNotOnOrAfterFromSubjectConfirmations() { DateTimeFreezer.freezeTime(); MetricRegistry metricRegistry = new MetricRegistry(); IdpAssertionMetricsCollector idpAssertionMetricsCollector = new IdpAssertionMetricsCollector(metricRegistry); DateTime notOnOrAfterSmaller = DateTime.now().plusMinutes(15); DateTime notOnOrAfterBigger = DateTime.now().plusMinutes(30); Assertion anAssertion = anAssertion() .withIssuer(anIssuer().withIssuerId("testIdP").build()) .withSubject(aSubject() .withSubjectConfirmation(aSubjectConfirmation() .withSubjectConfirmationData(aSubjectConfirmationData() .withNotOnOrAfter(notOnOrAfterSmaller) .build()) .build()) .withSubjectConfirmation(aSubjectConfirmation() .withSubjectConfirmationData(aSubjectConfirmationData() .withNotOnOrAfter(notOnOrAfterBigger) .build()) .build()) .build()) .buildUnencrypted(); idpAssertionMetricsCollector.update(anAssertion); Gauge actual = metricRegistry.getGauges().get("notOnOrAfter.testIdP"); assertThat(actual.getValue()).isEqualTo(30L); }