Search in sources :

Example 16 with Certificate

use of org.olat.course.certificate.Certificate in project OpenOLAT by OpenOLAT.

the class CertificationTest method uploadCertificate_standalone.

@Test
public void uploadCertificate_standalone() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login("administrator", "openolat"));
    Identity assessedIdentity = JunitTestHelper.createAndPersistIdentityAsRndUser("cert-1");
    dbInstance.commitAndCloseSession();
    Long resourceKey = 23687468l;
    URI uri = UriBuilder.fromUri(getContextURI()).path("repo").path("courses").path(resourceKey.toString()).path("certificates").path(assessedIdentity.getKey().toString()).build();
    URL certificateUrl = CertificationTest.class.getResource("certificate.pdf");
    Assert.assertNotNull(certificateUrl);
    File certificateFile = new File(certificateUrl.toURI());
    HttpPost method = conn.createPost(uri, MediaType.APPLICATION_JSON);
    Date creationDate = createDate(2014, 7, 1);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).addTextBody("filename", certificateFile.getName()).addBinaryBody("file", certificateFile, ContentType.APPLICATION_OCTET_STREAM, certificateFile.getName()).addTextBody("creationDate", ObjectFactory.formatDate(creationDate));
    method.setEntity(builder.build());
    HttpResponse response = conn.execute(method);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    EntityUtils.consume(response.getEntity());
    // check certificate
    Certificate certificate = certificatesManager.getLastCertificate(assessedIdentity, resourceKey);
    Assert.assertNotNull(certificate);
    Assert.assertEquals(creationDate, certificate.getCreationDate());
    // check the certificate file
    VFSLeaf certificateLeaf = certificatesManager.getCertificateLeaf(certificate);
    Assert.assertNotNull(certificateLeaf);
    Assert.assertEquals(certificateFile.length(), certificateLeaf.getSize());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) HttpResponse(org.apache.http.HttpResponse) Identity(org.olat.core.id.Identity) URI(java.net.URI) File(java.io.File) URL(java.net.URL) Date(java.util.Date) Certificate(org.olat.course.certificate.Certificate) Test(org.junit.Test)

Example 17 with Certificate

use of org.olat.course.certificate.Certificate in project OpenOLAT by OpenOLAT.

the class OLATUpgrade_11_0_6 method processCourse.

private void processCourse(RepositoryEntry entry) {
    List<Certificate> certificates = certificatesManager.getCertificates(entry.getOlatResource());
    for (Certificate certificate : certificates) {
        if (certificate.getNextRecertificationDate() != null)
            continue;
        Date nextCertification = certificatesManager.getDateNextRecertification(certificate, entry);
        if (nextCertification != null) {
            certificate.setNextRecertificationDate(nextCertification);
            dbInstance.getCurrentEntityManager().merge(certificate);
        }
    }
}
Also used : Date(java.util.Date) Certificate(org.olat.course.certificate.Certificate)

Example 18 with Certificate

use of org.olat.course.certificate.Certificate in project openolat by klemens.

the class CertificationTest method getCertificate_head.

@Test
public void getCertificate_head() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login("administrator", "openolat"));
    Identity assessedIdentity = JunitTestHelper.createAndPersistIdentityAsRndUser("cert-11");
    Identity unassessedIdentity = JunitTestHelper.createAndPersistIdentityAsRndUser("cert-12");
    Identity author = JunitTestHelper.createAndPersistIdentityAsAuthor("cert-2");
    RepositoryEntry entry = JunitTestHelper.deployBasicCourse(author);
    CertificateInfos certificateInfos = new CertificateInfos(assessedIdentity, 2.0f, true);
    Certificate certificate = certificatesManager.generateCertificate(certificateInfos, entry, null, false);
    dbInstance.commitAndCloseSession();
    Assert.assertNotNull(certificate);
    sleep(1000);
    URI uri = UriBuilder.fromUri(getContextURI()).path("repo").path("courses").path(entry.getOlatResource().getKey().toString()).path("certificates").path(assessedIdentity.getKey().toString()).build();
    HttpHead method = conn.createHead(uri, "application/pdf", true);
    HttpResponse response = conn.execute(method);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    EntityUtils.consume(response.getEntity());
    // check  with a stupid number
    URI nonExistentUri = UriBuilder.fromUri(getContextURI()).path("repo").path("courses").path(entry.getOlatResource().getKey().toString()).path("certificates").path(unassessedIdentity.getKey().toString()).build();
    HttpHead nonExistentMethod = conn.createHead(nonExistentUri, "application/pdf", true);
    HttpResponse nonExistentResponse = conn.execute(nonExistentMethod);
    Assert.assertEquals(404, nonExistentResponse.getStatusLine().getStatusCode());
    EntityUtils.consume(nonExistentResponse.getEntity());
    conn.shutdown();
}
Also used : CertificateInfos(org.olat.course.certificate.model.CertificateInfos) HttpResponse(org.apache.http.HttpResponse) RepositoryEntry(org.olat.repository.RepositoryEntry) Identity(org.olat.core.id.Identity) URI(java.net.URI) HttpHead(org.apache.http.client.methods.HttpHead) Certificate(org.olat.course.certificate.Certificate) Test(org.junit.Test)

Example 19 with Certificate

use of org.olat.course.certificate.Certificate in project openolat by klemens.

the class CertificationTest method getCertificate_file.

@Test
public void getCertificate_file() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login("administrator", "openolat"));
    Identity assessedIdentity = JunitTestHelper.createAndPersistIdentityAsRndUser("cert-1");
    Identity author = JunitTestHelper.createAndPersistIdentityAsAuthor("cert-2");
    RepositoryEntry entry = JunitTestHelper.deployBasicCourse(author);
    CertificateInfos certificateInfos = new CertificateInfos(assessedIdentity, 2.0f, true);
    Certificate certificate = certificatesManager.generateCertificate(certificateInfos, entry, null, false);
    dbInstance.commitAndCloseSession();
    Assert.assertNotNull(certificate);
    // wait until the certificate is created
    waitForCondition(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            Certificate reloadedCertificate = certificatesManager.getCertificateById(certificate.getKey());
            return CertificateStatus.ok.equals(reloadedCertificate.getStatus());
        }
    }, 30000);
    URI uri = UriBuilder.fromUri(getContextURI()).path("repo").path("courses").path(entry.getOlatResource().getKey().toString()).path("certificates").path(assessedIdentity.getKey().toString()).build();
    HttpGet method = conn.createGet(uri, "application/pdf", true);
    HttpResponse response = conn.execute(method);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    EntityUtils.consume(response.getEntity());
    conn.shutdown();
}
Also used : CertificateInfos(org.olat.course.certificate.model.CertificateInfos) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) RepositoryEntry(org.olat.repository.RepositoryEntry) Identity(org.olat.core.id.Identity) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Certificate(org.olat.course.certificate.Certificate) Test(org.junit.Test)

Example 20 with Certificate

use of org.olat.course.certificate.Certificate in project openolat by klemens.

the class CertificationTest method generateCertificate.

@Test
public void generateCertificate() throws IOException, URISyntaxException {
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login("administrator", "openolat"));
    Identity assessedIdentity = JunitTestHelper.createAndPersistIdentityAsRndUser("cert-1");
    Identity author = JunitTestHelper.createAndPersistIdentityAsAuthor("cert-2");
    RepositoryEntry entry = JunitTestHelper.deployBasicCourse(author);
    dbInstance.commitAndCloseSession();
    Date creationDate = createDate(2014, 9, 9);
    URI uri = UriBuilder.fromUri(getContextURI()).path("repo").path("courses").path(entry.getOlatResource().getKey().toString()).path("certificates").path(assessedIdentity.getKey().toString()).queryParam("score", "3.2").queryParam("passed", "true").queryParam("creationDate", ObjectFactory.formatDate(creationDate)).build();
    HttpPut method = conn.createPut(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    EntityUtils.consume(response.getEntity());
    // async process mean sleep a little
    sleep(2000);
    // check certificate
    Certificate certificate = certificatesManager.getLastCertificate(assessedIdentity, entry.getOlatResource().getKey());
    Assert.assertNotNull(certificate);
    Assert.assertEquals(creationDate, certificate.getCreationDate());
    // check the certificate file
    VFSLeaf certificateLeaf = certificatesManager.getCertificateLeaf(certificate);
    Assert.assertNotNull(certificateLeaf);
    Assert.assertTrue(certificateLeaf.getSize() > 500);
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) HttpResponse(org.apache.http.HttpResponse) RepositoryEntry(org.olat.repository.RepositoryEntry) Identity(org.olat.core.id.Identity) URI(java.net.URI) Date(java.util.Date) HttpPut(org.apache.http.client.methods.HttpPut) Certificate(org.olat.course.certificate.Certificate) Test(org.junit.Test)

Aggregations

Certificate (org.olat.course.certificate.Certificate)54 Identity (org.olat.core.id.Identity)42 Test (org.junit.Test)30 RepositoryEntry (org.olat.repository.RepositoryEntry)30 Date (java.util.Date)20 CertificateInfos (org.olat.course.certificate.model.CertificateInfos)18 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)16 File (java.io.File)14 Calendar (java.util.Calendar)14 URI (java.net.URI)12 URL (java.net.URL)12 HttpResponse (org.apache.http.HttpResponse)12 Path (javax.ws.rs.Path)8 BaseSecurity (org.olat.basesecurity.BaseSecurity)8 CertificatesManager (org.olat.course.certificate.CertificatesManager)8 IOException (java.io.IOException)6 Roles (org.olat.core.id.Roles)6 ICourse (org.olat.course.ICourse)6 OLATResource (org.olat.resource.OLATResource)6 OLATResourceManager (org.olat.resource.OLATResourceManager)6