X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cadi%2Fcore%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Faaf%2Fcadi%2Fprincipal%2Ftest%2FJU_X509Principal.java;h=a8839f2c2a7e9e13f31d06d684d4f0201162a47f;hb=refs%2Fchanges%2F75%2F65275%2F1;hp=e62dda4f7777784a41a3380fe2514d44acf1f989;hpb=4fec2a6ea44a28d676dd70d25228f615c4dd47ce;p=aaf%2Fauthz.git diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_X509Principal.java b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_X509Principal.java index e62dda4f..a8839f2c 100644 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_X509Principal.java +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_X509Principal.java @@ -38,103 +38,103 @@ import org.onap.aaf.cadi.principal.X509Principal; public class JU_X509Principal { - private final String name = "x509 name"; - private final byte[] cred = "super duper secret password".getBytes(); - - @Mock - X509Certificate cert; - - @Mock - Principal subject; - - @Before - public void setup() throws CertificateEncodingException { - MockitoAnnotations.initMocks(this); - when(cert.getEncoded()).thenReturn(cred); - } - - @Test - public void constructor1Test() throws IOException { - X509Principal x509 = new X509Principal(name, cert); - // Call twice to hit both branches - assertThat(x509.getAsHeader(), is("X509 " + cred)); - assertThat(x509.getAsHeader(), is("X509 " + cred)); - assertThat(x509.toString(), is("X509 Authentication for " + name)); - assertTrue(x509.getCred().equals(cred)); - assertThat(x509.getName(), is(name)); - assertThat(x509.tag(), is("x509")); - } - - @Test - public void constructor2Test() throws IOException { - X509Principal x509 = new X509Principal(name, cert, cred); - // Call twice to hit both branches - assertThat(x509.getAsHeader(), is("X509 " + cred)); - assertThat(x509.toString(), is("X509 Authentication for " + name)); - assertTrue(x509.getCred().equals(cred)); - assertThat(x509.getName(), is(name)); - assertThat(x509.tag(), is("x509")); - } - - @Test - public void constructor3Test() throws IOException { - final String longName = "name@domain"; - when(subject.getName()).thenReturn("OU=" + longName + ",extra"); - when(cert.getSubjectDN()).thenReturn(subject); - X509Principal x509 = new X509Principal(cert, cred); - // Call twice to hit both branches - assertThat(x509.getAsHeader(), is("X509 " + cred)); - assertThat(x509.toString(), is("X509 Authentication for " + longName)); - assertTrue(x509.getCred().equals(cred)); - assertThat(x509.getName(), is(longName)); - - when(subject.getName()).thenReturn(longName + ",extra"); - when(cert.getSubjectDN()).thenReturn(subject); - try { - x509 = new X509Principal(cert, cred); - fail("Should have thrown an Exception"); - } catch(IOException e) { - assertThat(e.getMessage(), is("X509 does not have Identity as CN")); - } - - when(subject.getName()).thenReturn("OU=" + longName); - when(cert.getSubjectDN()).thenReturn(subject); - try { - x509 = new X509Principal(cert, cred); - fail("Should have thrown an Exception"); - } catch(IOException e) { - assertThat(e.getMessage(), is("X509 does not have Identity as CN")); - } - - when(subject.getName()).thenReturn("OU=" + name + ",exta"); - when(cert.getSubjectDN()).thenReturn(subject); - try { - x509 = new X509Principal(cert, cred); - fail("Should have thrown an Exception"); - } catch(IOException e) { - assertThat(e.getMessage(), is("X509 does not have Identity as CN")); - } - - } - - @Test - public void throwsTest() throws CertificateEncodingException { - when(cert.getEncoded()).thenThrow(new CertificateEncodingException()); - X509Principal x509 = new X509Principal(name, cert); - assertThat(x509.getCred(), is(nullValue())); - try { - x509.getAsHeader(); - fail("Should have thrown an Exception"); - } catch (IOException e) { - } - } - - @Test - public void getCredTest() { - X509Principal x509 = new X509Principal(name, cert); - // Call twice to hit both branches - assertTrue(x509.getCred().equals(cred)); - assertTrue(x509.getCred().equals(cred)); - } + private final String name = "x509 name"; + private final byte[] cred = "super duper secret password".getBytes(); + + @Mock + X509Certificate cert; + + @Mock + Principal subject; + + @Before + public void setup() throws CertificateEncodingException { + MockitoAnnotations.initMocks(this); + when(cert.getEncoded()).thenReturn(cred); + } + + @Test + public void constructor1Test() throws IOException { + X509Principal x509 = new X509Principal(name, cert); + // Call twice to hit both branches + assertThat(x509.getAsHeader(), is("X509 " + cred)); + assertThat(x509.getAsHeader(), is("X509 " + cred)); + assertThat(x509.toString(), is("X509 Authentication for " + name)); + assertTrue(x509.getCred().equals(cred)); + assertThat(x509.getName(), is(name)); + assertThat(x509.tag(), is("x509")); + } + + @Test + public void constructor2Test() throws IOException { + X509Principal x509 = new X509Principal(name, cert, cred,null); + // Call twice to hit both branches + assertThat(x509.getAsHeader(), is("X509 " + cred)); + assertThat(x509.toString(), is("X509 Authentication for " + name)); + assertTrue(x509.getCred().equals(cred)); + assertThat(x509.getName(), is(name)); + assertThat(x509.tag(), is("x509")); + } + + @Test + public void constructor3Test() throws IOException { + final String longName = "name@domain"; + when(subject.getName()).thenReturn("OU=" + longName + ",extra"); + when(cert.getSubjectDN()).thenReturn(subject); + X509Principal x509 = new X509Principal(cert, cred,null); + // Call twice to hit both branches + assertThat(x509.getAsHeader(), is("X509 " + cred)); + assertThat(x509.toString(), is("X509 Authentication for " + longName)); + assertTrue(x509.getCred().equals(cred)); + assertThat(x509.getName(), is(longName)); + + when(subject.getName()).thenReturn(longName + ",extra"); + when(cert.getSubjectDN()).thenReturn(subject); + try { + x509 = new X509Principal(cert, cred, null); + fail("Should have thrown an Exception"); + } catch(IOException e) { + assertThat(e.getMessage(), is("X509 does not have Identity as CN")); + } + + when(subject.getName()).thenReturn("OU=" + longName); + when(cert.getSubjectDN()).thenReturn(subject); + try { + x509 = new X509Principal(cert, cred, null); + fail("Should have thrown an Exception"); + } catch(IOException e) { + assertThat(e.getMessage(), is("X509 does not have Identity as CN")); + } + + when(subject.getName()).thenReturn("OU=" + name + ",exta"); + when(cert.getSubjectDN()).thenReturn(subject); + try { + x509 = new X509Principal(cert, cred, null); + fail("Should have thrown an Exception"); + } catch(IOException e) { + assertThat(e.getMessage(), is("X509 does not have Identity as CN")); + } + + } + + @Test + public void throwsTest() throws CertificateEncodingException { + when(cert.getEncoded()).thenThrow(new CertificateEncodingException()); + X509Principal x509 = new X509Principal(name, cert); + assertThat(x509.getCred(), is(nullValue())); + try { + x509.getAsHeader(); + fail("Should have thrown an Exception"); + } catch (IOException e) { + } + } + + @Test + public void getCredTest() { + X509Principal x509 = new X509Principal(name, cert); + // Call twice to hit both branches + assertTrue(x509.getCred().equals(cred)); + assertTrue(x509.getCred().equals(cred)); + } }