X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cadi%2Faaf%2Fsrc%2Ftest%2Fjava%2Forg%2Fonap%2Faaf%2Fcadi%2Fcm%2Ftest%2FJU_PlaceArtifactInKeystore.java;h=237e0f64c560643648e10ccced380207f71ff205;hb=1296352d8eafee57f982a4342ad79ada4aa56d28;hp=d61ac4997171e3ffd23d9c5251d5696da5b0fe71;hpb=047edb5a4312cc7dfb3172b7e11ab1a9e2315ba2;p=aaf%2Fauthz.git diff --git a/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_PlaceArtifactInKeystore.java b/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_PlaceArtifactInKeystore.java index d61ac499..237e0f64 100644 --- a/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_PlaceArtifactInKeystore.java +++ b/cadi/aaf/src/test/java/org/onap/aaf/cadi/cm/test/JU_PlaceArtifactInKeystore.java @@ -54,100 +54,100 @@ import certman.v1_0.CertInfo; public class JU_PlaceArtifactInKeystore { - @Mock private Trans transMock; - @Mock private CertInfo certInfoMock; - @Mock private Artifact artiMock; - - private static final String caName = "onap"; - private static final String dirName = "src/test/resources/artifacts"; - private static final String nsName = "org.onap.test"; - private static final String mechID = "m12345"; - private static final String luggagePassword = "12345"; // That's the stupidest combination I've ever heard in my life - - private static String privateKeyString; - private static String x509Chain; - private static String x509String; - - private List certs; - - @Before - public void setup() throws FileNotFoundException, IOException, CertificateException { - MockitoAnnotations.initMocks(this); - - x509Chain = fromFile(new File("src/test/resources/cert.pem")); - x509String = fromFile(new File("src/test/resources/exampleCertificate.cer")); - privateKeyString = fromFile(new File("src/test/resources/key.pem")); - - certs = new ArrayList<>(); - - when(certInfoMock.getChallenge()).thenReturn(luggagePassword); - when(certInfoMock.getCerts()).thenReturn(certs); - - when(artiMock.getCa()).thenReturn(caName); - when(artiMock.getDir()).thenReturn(dirName); - when(artiMock.getNs()).thenReturn(nsName); - when(artiMock.getMechid()).thenReturn(mechID); - - when(certInfoMock.getPrivatekey()).thenReturn(privateKeyString); - - when(transMock.start("Reconstitute Private Key", Env.SUB)).thenReturn(mock(TimeTaken.class)); - } - - @AfterClass - public static void tearDownOnce() { - cleanup(); - PlaceArtifactInKeystore.clear(); - } - - @Test - public void test() throws CadiException { - // Note: PKCS12 can't be tested in JDK 7 and earlier. Can't handle Trusting Certificates. - PlaceArtifactInKeystore placer = new PlaceArtifactInKeystore(Agent.JKS); - - certs.add(x509String); - certs.add(x509Chain); - assertThat(placer.place(transMock, certInfoMock, artiMock, "machine"), is(true)); - for (String ext : new String[] {"chal", "keyfile", Agent.JKS, "trust.jks", "cred.props"}) { - File f = new File(dirName + '/' + nsName + '.' + ext); - assertThat(f.exists(), is(true)); - } - - // coverage - assertThat(placer.place(transMock, certInfoMock, artiMock, "machine"), is(true)); - - when(certInfoMock.getCerts()).thenReturn(null); - try { - placer._place(transMock, certInfoMock, artiMock); - fail("Should've thrown an exception"); - } catch (Exception e) { - } - - } - - private static void cleanup() { - File dir = new File(dirName); - if (dir.exists()) { - for (File f : dir.listFiles()) { - f.delete(); - } - dir.delete(); - } - } - - public String fromFile(File file) throws IOException { - BufferedReader br = new BufferedReader(new FileReader(file)); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - String line; - baos.write(br.readLine().getBytes()); - // Here comes the hacky part - baos.write("\n".getBytes()); - while((line=br.readLine())!=null) { - if(line.length()>0) { - baos.write(line.getBytes()); - baos.write("\n".getBytes()); - } - } - br.close(); - return baos.toString(); - } + @Mock private Trans transMock; + @Mock private CertInfo certInfoMock; + @Mock private Artifact artiMock; + + private static final String caName = "onap"; + private static final String dirName = "src/test/resources/artifacts"; + private static final String nsName = "org.onap.test"; + private static final String mechID = "m12345"; + private static final String luggagePassword = "12345"; // That's the stupidest combination I've ever heard in my life + + private static String privateKeyString; + private static String x509Chain; + private static String x509String; + + private List certs; + + @Before + public void setup() throws FileNotFoundException, IOException, CertificateException { + MockitoAnnotations.initMocks(this); + + x509Chain = fromFile(new File("src/test/resources/cert.pem")); + x509String = fromFile(new File("src/test/resources/exampleCertificate.cer")); + privateKeyString = fromFile(new File("src/test/resources/key.pem")); + + certs = new ArrayList<>(); + + when(certInfoMock.getChallenge()).thenReturn(luggagePassword); + when(certInfoMock.getCerts()).thenReturn(certs); + + when(artiMock.getCa()).thenReturn(caName); + when(artiMock.getDir()).thenReturn(dirName); + when(artiMock.getNs()).thenReturn(nsName); + when(artiMock.getMechid()).thenReturn(mechID); + + when(certInfoMock.getPrivatekey()).thenReturn(privateKeyString); + + when(transMock.start("Reconstitute Private Key", Env.SUB)).thenReturn(mock(TimeTaken.class)); + } + + @AfterClass + public static void tearDownOnce() { + cleanup(); + PlaceArtifactInKeystore.clear(); + } + + @Test + public void test() throws CadiException { + // Note: PKCS12 can't be tested in JDK 7 and earlier. Can't handle Trusting Certificates. + PlaceArtifactInKeystore placer = new PlaceArtifactInKeystore(Agent.JKS); + + certs.add(x509String); + certs.add(x509Chain); + assertThat(placer.place(transMock, certInfoMock, artiMock, "machine"), is(true)); + for (String ext : new String[] { Agent.JKS, "trust.jks"}) { + File f = new File(dirName + '/' + nsName + '.' + ext); + assertThat(f.exists(), is(true)); + } + + // coverage + assertThat(placer.place(transMock, certInfoMock, artiMock, "machine"), is(true)); + + when(certInfoMock.getCerts()).thenReturn(null); + try { + placer._place(transMock, certInfoMock, artiMock); + fail("Should've thrown an exception"); + } catch (Exception e) { + } + + } + + private static void cleanup() { + File dir = new File(dirName); + if (dir.exists()) { + for (File f : dir.listFiles()) { + f.delete(); + } + dir.delete(); + } + } + + public String fromFile(File file) throws IOException { + BufferedReader br = new BufferedReader(new FileReader(file)); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + String line; + baos.write(br.readLine().getBytes()); + // Here comes the hacky part + baos.write("\n".getBytes()); + while ((line=br.readLine())!=null) { + if (line.length()>0) { + baos.write(line.getBytes()); + baos.write("\n".getBytes()); + } + } + br.close(); + return baos.toString(); + } }