O2Principal implements Principal { public O2Principal merge(final Principal newPrincipal) { O2Principal principal = safeCast(newPrincipal, O2Principal.class) .orElse(null); if (isNull(principal)) { return new O2Principal(contextClient, oAuthToken); } O2Principal merged = new O2Principal( sameOrOne(contextClient, principal.contextClient), sameOrOne(oAuthToken, principal.oAuthToken) ); long validSchemes = Stream.of(getScheme(), principal.getScheme()) .map(O2AuthScheme::valueOf) .distinct() .filter(s -> !s.equals(O2AuthScheme.None)) .count(); if (validSchemes > 1) { throw new AccessDeniedException(); } return merged; } O2Principal();  O2Principal(final Client client);  O2Principal(final OAuthToken token); private  O2Principal(final Client client, final OAuthToken token); OAuthToken getOAuthToken(); @Override String getName(); @Override boolean implies(final Subject subject); String getScheme(); Client getContext(); O2Principal merge(final Principal newPrincipal); @Override boolean equals(final Object o); @Override int hashCode();  }
@Test public void testMergeNullInput() { Client c = new Client(); c.setId(IdUtil.next()); O2Principal principal = new O2Principal(c); O2Principal merged = principal.merge(null); assertEquals(principal, merged); assertNotSame(principal, merged); }
@Test public void testMergeWrongPrincipalInput() { Client c = new Client(); c.setId(IdUtil.next()); O2Principal principal = new O2Principal(c); Principal mergeable = () -> null; O2Principal merged = principal.merge(mergeable); assertEquals(principal, merged); assertNotSame(principal, merged); }
@Test(expected = AccessDeniedException.class) public void testMergeMismatchClient() { Client c1 = new Client(); c1.setId(IdUtil.next()); O2Principal p1 = new O2Principal(c1); Client c2 = new Client(); c2.setId(IdUtil.next()); O2Principal p2 = new O2Principal(c2); p1.merge(p2); }
@Test(expected = AccessDeniedException.class) public void testMergeTwoAuthSchemes() { Client c1 = new Client(); c1.setId(IdUtil.next()); c1.setClientSecret("foo"); O2Principal p1 = new O2Principal(c1); Client c2 = new Client(); c2.setId(c1.getId()); O2Principal p2 = new O2Principal(c2); p1.merge(p2); }
@Test public void testMergeSameAuthScheme() { Client c1 = new Client(); c1.setId(IdUtil.next()); c1.setClientSecret("secret"); O2Principal p1 = new O2Principal(c1); Client c2 = new Client(); c2.setId(c1.getId()); c2.setClientSecret("secret"); O2Principal p2 = new O2Principal(c2); O2Principal merged = p1.merge(p2); assertEquals(p1, merged); assertEquals(p2, merged); assertNotSame(p1, merged); assertNotSame(p2, merged); }
@Test public void testMergeNoAuthSchemePrivate() { Client c = new Client(); c.setId(IdUtil.next()); c.setClientSecret("secret"); O2Principal p1 = new O2Principal(c); O2Principal p2 = new O2Principal(); O2Principal merged = p1.merge(p2); assertEquals(p1, merged); assertNotSame(p1, merged); }
@Test public void testMergeNoAuthScheme() { O2Principal p1 = new O2Principal(); O2Principal p2 = new O2Principal(); O2Principal merged = p1.merge(p2); assertEquals(p1, merged); assertNotSame(p1, merged); O2Principal mergedReverse = p2.merge(p1); assertEquals(p1, mergedReverse); assertNotSame(p1, mergedReverse); }
@Test(expected = AccessDeniedException.class) public void testMergeTokenAndClient() { Client c = new Client(); c.setId(IdUtil.next()); c.setClientSecret("secret"); O2Principal p1 = new O2Principal(c); OAuthToken t = new OAuthToken(); t.setId(IdUtil.next()); t.setClient(c); O2Principal p2 = new O2Principal(t); p1.merge(p2); }
@Test(expected = AccessDeniedException.class) public void testMergeTwoDifferentTokens() { Client c1 = new Client(); c1.setId(IdUtil.next()); c1.setClientSecret("secret"); OAuthToken t1 = new OAuthToken(); t1.setId(IdUtil.next()); t1.setClient(c1); O2Principal p1 = new O2Principal(t1); Client c2 = new Client(); c2.setId(IdUtil.next()); c2.setClientSecret("secret"); OAuthToken t2 = new OAuthToken(); t2.setId(IdUtil.next()); t2.setClient(c2); O2Principal p2 = new O2Principal(t2); p1.merge(p2); }
@Test public void testMergeTwoTokens() { Client c1 = new Client(); c1.setId(IdUtil.next()); c1.setClientSecret("secret"); OAuthToken t = new OAuthToken(); t.setId(IdUtil.next()); t.setClient(c1); O2Principal p1 = new O2Principal(t); O2Principal p2 = new O2Principal(t); O2Principal p = p1.merge(p2); assertEquals(p, p1); assertEquals(p, p2); }
@Test public void testMergeTokenAndNull() { Client c1 = new Client(); c1.setId(IdUtil.next()); c1.setClientSecret("secret"); OAuthToken t = new OAuthToken(); t.setId(IdUtil.next()); t.setClient(c1); O2Principal p1 = new O2Principal(t); O2Principal merged = p1.merge(null); assertEquals(p1, merged); assertNotSame(p1, merged); }
@Test public void testMergeTokenAndEmpty() { Client c1 = new Client(); c1.setId(IdUtil.next()); c1.setClientSecret("secret"); OAuthToken t = new OAuthToken(); t.setId(IdUtil.next()); t.setClient(c1); O2Principal p1 = new O2Principal(t); O2Principal p2 = new O2Principal(); O2Principal merged = p1.merge(p2); assertEquals(p1, merged); assertNotEquals(p2, merged); assertNotSame(p1, merged); }