### Question:
OpaJwtPrincipalFactory implements Factory<OpaJwtPrincipal> { @Override public OpaJwtPrincipal provide() { Principal userPrincipal = securityContext.getUserPrincipal(); if (userPrincipal instanceof OpaJwtPrincipal) { return (OpaJwtPrincipal) userPrincipal; } return null; } @Inject  OpaJwtPrincipalFactory(SecurityContext securityContext); @Override OpaJwtPrincipal provide(); @Override void dispose(OpaJwtPrincipal instance);  }### Answer:
@Test public void shouldProvideOpaJwtPrincipalFromSecurityContext() { Principal given = emptyOpaJwtPrincipal(); when(securityContextMock.getUserPrincipal()).thenReturn(given); OpaJwtPrincipal actual = opaJwtPrincipalFactory.provide(); assertThat(actual).isSameAs(given); }
@Test public void shouldSkipOtherTypeOfPrincipal() { Principal given = emptyGenericPrincipal(); when(securityContextMock.getUserPrincipal()).thenReturn(given); OpaJwtPrincipal actual = opaJwtPrincipalFactory.provide(); assertThat(actual).isNull(); }
@Test public void shouldReturnNullIfNoPrincipal() { when(securityContextMock.getUserPrincipal()).thenReturn(null); OpaJwtPrincipal actual = opaJwtPrincipalFactory.provide(); assertThat(actual).isNull(); }