Example usage for org.bouncycastle.util Strings toByteArray

List of usage examples for org.bouncycastle.util Strings toByteArray

Introduction

In this page you can find the example usage for org.bouncycastle.util Strings toByteArray.

Prototype

public static byte[] toByteArray(String string) 

Source Link

Usage

From source file:brooklyn.entity.nosql.elasticsearch.ElasticSearchClusterIntegrationTest.java

License:Apache License

@Test(groups = { "Integration" })
public void testPutAndGet() throws URISyntaxException {
    elasticSearchCluster = app.createAndManageChild(
            EntitySpec.create(ElasticSearchCluster.class).configure(DynamicCluster.INITIAL_SIZE, 3));
    app.start(ImmutableList.of(testLocation));

    EntityTestUtils.assertAttributeEqualsEventually(elasticSearchCluster, Startable.SERVICE_UP, true);
    assertEquals(elasticSearchCluster.getMembers().size(), 3);
    assertEquals(clusterDocumentCount(), 0);

    ElasticSearchNode anyNode = (ElasticSearchNode) elasticSearchCluster.getMembers().iterator().next();

    String document = "{\"foo\" : \"bar\",\"baz\" : \"quux\"}";

    String putBaseUri = "http://" + anyNode.getAttribute(Attributes.HOSTNAME) + ":"
            + anyNode.getAttribute(Attributes.HTTP_PORT);

    HttpToolResponse putResponse = HttpTool.httpPut(
            HttpTool.httpClientBuilder().port(anyNode.getAttribute(Attributes.HTTP_PORT)).build(),
            new URI(putBaseUri + "/mydocuments/docs/1"), ImmutableMap.<String, String>of(),
            Strings.toByteArray(document));
    assertEquals(putResponse.getResponseCode(), 201);

    for (Entity entity : elasticSearchCluster.getMembers()) {
        ElasticSearchNode node = (ElasticSearchNode) entity;
        String getBaseUri = "http://" + node.getAttribute(Attributes.HOSTNAME) + ":"
                + node.getAttribute(Attributes.HTTP_PORT);
        HttpToolResponse getResponse = HttpTool.execAndConsume(HttpTool.httpClientBuilder().build(),
                new HttpGet(getBaseUri + "/mydocuments/docs/1/_source"));
        assertEquals(getResponse.getResponseCode(), 200);
        assertEquals(HttpValueFunctions.jsonContents("foo", String.class).apply(getResponse), "bar");
    }/*from w w w.j  a  v  a 2s . c  o  m*/
    Asserts.succeedsEventually(new Runnable() {
        public void run() {
            int count = clusterDocumentCount();
            assertTrue(count >= 1, "count=" + count);
            LOG.debug("Document count is {}", count);
        }
    });
}

From source file:brooklyn.entity.nosql.elasticsearch.ElasticSearchNodeIntegrationTest.java

License:Apache License

@Test(groups = { "Integration" })
public void testDocumentCount() throws URISyntaxException {
    elasticSearchNode = app.createAndManageChild(EntitySpec.create(ElasticSearchNode.class));
    app.start(ImmutableList.of(testLocation));

    EntityTestUtils.assertAttributeEqualsEventually(elasticSearchNode, Startable.SERVICE_UP, true);

    EntityTestUtils.assertAttributeEquals(elasticSearchNode, ElasticSearchNode.DOCUMENT_COUNT, 0);

    String baseUri = "http://" + elasticSearchNode.getAttribute(Attributes.HOSTNAME) + ":"
            + elasticSearchNode.getAttribute(Attributes.HTTP_PORT);

    HttpToolResponse pingResponse = HttpTool.execAndConsume(HttpTool.httpClientBuilder().build(),
            new HttpGet(baseUri));
    assertEquals(pingResponse.getResponseCode(), 200);

    String document = "{\"foo\" : \"bar\",\"baz\" : \"quux\"}";

    HttpToolResponse putResponse = HttpTool.httpPut(
            HttpTool.httpClientBuilder().port(elasticSearchNode.getAttribute(Attributes.HTTP_PORT)).build(),
            new URI(baseUri + "/mydocuments/docs/1"), ImmutableMap.<String, String>of(),
            Strings.toByteArray(document));
    assertEquals(putResponse.getResponseCode(), 201);

    HttpToolResponse getResponse = HttpTool.execAndConsume(HttpTool.httpClientBuilder().build(),
            new HttpGet(baseUri + "/mydocuments/docs/1/_source"));
    assertEquals(getResponse.getResponseCode(), 200);
    assertEquals(HttpValueFunctions.jsonContents("foo", String.class).apply(getResponse), "bar");

    EntityTestUtils.assertAttributeEqualsEventually(elasticSearchNode, ElasticSearchNode.DOCUMENT_COUNT, 1);
}

From source file:com.formkiq.core.service.ArchiveServiceImplTest.java

License:Apache License

/**
 * testCreateZipFile01().//from   w  ww  .  j a  va2s  .  com
 * @throws Exception Exception
 */
@Test
public void testCreateZipFile01() throws Exception {
    // given
    ArchiveDTO archive = ObjectBuilder.buildArchiveDTO("testwf");
    archive.addForm(TestDataBuilder.createSimpleForm());
    archive.addPDF("sample-form1.pdf", Strings.toByteArray("pdf"));
    archive.addSignature("test.sig", Strings.toByteArray("sig"));
    archive.addRoute(new WorkflowRoute());
    archive.addObject("test.docx", Strings.toByteArray("docx"));

    // when
    byte[] data = this.service.createZipFile(archive);
    ArchiveDTO result = this.service.extractJSONFromZipFile(data);

    // then
    assertEquals(archive.getWorkflow().getUUID(), result.getWorkflow().getUUID());

    assertFalse(archive.getForms().isEmpty());
    assertEquals(archive.getForms().size(), result.getForms().size());
    assertTrue(result.getForms().containsKey(archive.getForms().keySet().iterator().next()));

    assertFalse(archive.getPDF().isEmpty());
    assertEquals(archive.getPDF().size(), result.getPDF().size());
    assertTrue(result.getPDF().containsKey(archive.getPDF().keySet().iterator().next()));

    assertFalse(archive.getSignatures().isEmpty());
    assertEquals(archive.getSignatures().size(), result.getSignatures().size());
    assertTrue(result.getSignatures().containsKey(archive.getSignatures().keySet().iterator().next()));

    assertFalse(archive.getRoutes().isEmpty());
    assertEquals(archive.getRoutes().size(), result.getRoutes().size());
    assertTrue(result.getRoutes().containsKey(archive.getRoutes().keySet().iterator().next()));

    assertFalse(archive.getObjects().isEmpty());
    assertEquals(archive.getObjects().size(), result.getObjects().size());
    assertTrue(result.getObjects().containsKey(archive.getObjects().keySet().iterator().next()));
}

From source file:com.formkiq.core.service.workflow.WorkflowEditorServiceImplTest.java

License:Apache License

/**
 * testEventAddStep02().//from   w  w w .  j av  a 2  s.  c  om
 * Add step - MultipartHttpServletRequest
 * @throws IOException IOException
 */
@SuppressWarnings("unchecked")
@Test
public void testEventAddStep02() throws IOException {
    // given
    this.archive.setWorkflow(this.workflow);
    MockMultipartHttpServletRequest req = new MockMultipartHttpServletRequest();
    req.setRequestURI("/home");

    byte[] content = Strings.toByteArray("test");
    req.addFile(new MockMultipartFile("sample.pdf", "sample.pdf", "", content));

    // when
    expect(this.flow.getData()).andReturn(this.archive);

    this.flow.setStates(isA(List.class));
    this.flow.setEventId(1);

    Map<String, WorkflowOutputGenerator> map = ImmutableMap.of("test", this.pdfEditor);
    expect(this.context.getBeansOfType(WorkflowOutputGenerator.class)).andReturn(map);
    expect(this.pdfEditor.getNewWorkflowOutputDocument()).andReturn(new WorkflowOutputPdfForm());
    this.pdfEditor.generate(this.archive, "sample.pdf", content);

    replayAll();
    this.ws.eventIdaddstep(this.flow, req, null);

    // then
    verifyAll();
}

From source file:org.apache.brooklyn.util.executor.HttpExecutorImplTest.java

License:Apache License

@Test
public void testHttpPasswordRequest() throws Exception {
    MockResponse firstServerResponse = new MockResponse().setResponseCode(401)
            .addHeader("WWW-Authenticate", "Basic realm=\"User Visible Realm\"").setBody("Not Authenticated");
    server.enqueue(firstServerResponse);
    MockResponse secondServerResponse = new MockResponse().setResponseCode(200)
            .addHeader(HTTP_RESPONSE_HEADER_KEY, HTTP_RESPONSE_HEADER_VALUE).setBody(HTTP_BODY);
    server.enqueue(secondServerResponse);
    final String USER = "brooklyn", PASSWORD = "apache";
    String authUnencoded = USER + ":" + PASSWORD;
    String authEncoded = Base64.encodeBase64String(Strings.toByteArray(authUnencoded));
    HttpExecutor executor = factory.getHttpExecutor(getProps());
    HttpRequest executorRequest = new HttpRequest.Builder()
            .headers(ImmutableMap.of("RequestHeader", "RequestHeaderValue")).method("GET").uri(baseUrl.toURI())
            .credentials(new UsernamePassword("brooklyn", "apache")).build();
    HttpResponse executorResponse = executor.execute(executorRequest);

    RecordedRequest recordedFirstRequest = server.takeRequest();
    RecordedRequest recordedSecondRequest = server.takeRequest();

    assertRequestAndResponse(recordedFirstRequest, firstServerResponse, executorRequest,
            new HttpResponse.Builder().header("WWW-Authenticate", "Basic realm=\"User Visible Realm\"")
                    .header("Content-Length", "" + "Not Authenticated".length())
                    .content("Not Authenticated".getBytes()).build());
    ArrayListMultimap newHeaders = ArrayListMultimap.create(executorRequest.headers());
    newHeaders.put("Authorization", "Basic " + authEncoded);
    assertRequestAndResponse(recordedSecondRequest, secondServerResponse,
            new HttpRequest.Builder().from((HttpRequestImpl) executorRequest).headers(newHeaders).build(),
            executorResponse);/*w w  w.  j a v  a2s  .c o m*/
}

From source file:org.candlepin.dto.api.v1.JobStatusDTOTest.java

License:Open Source License

public JobStatusDTOTest() {
    super(JobStatusDTO.class);

    Object bytes = Strings.toByteArray("random byte array");
    this.values = new HashMap<>();
    this.values.put("Id", "test-id");
    this.values.put("Group", "test-job-group");
    this.values.put("State", "test-state");
    this.values.put("StartTime", new Date());
    this.values.put("FinishTime", new Date());
    this.values.put("Result", "test-result");
    this.values.put("PrincipalName", "test-principal-name");
    this.values.put("TargetType", "test-target-type");
    this.values.put("TargetId", "test-target-id");
    this.values.put("OwnerId", "test-owner-id");
    this.values.put("CorrelationId", "test-correlation-id");
    this.values.put("ResultData", bytes);
    this.values.put("Done", true);
    this.values.put("Created", new Date());
    this.values.put("Updated", new Date());
}

From source file:org.cryptoworkshop.ximix.node.crypto.key.BLSKeyManager.java

License:Apache License

public synchronized AsymmetricCipherKeyPair generateKeyPair(String keyID, Algorithm algorithm,
        int numberOfPeers, NamedKeyGenParams keyGenParams) {
    BLS01Parameters domainParameters = paramsMap.get(keyID);

    if (domainParameters == null) {
        BLS01KeyPairGenerator kpGen = new BLS01KeyPairGenerator();
        CurveParameters curveParameters = new DefaultCurveParameters()
                .load(this.getClass().getResourceAsStream("d62003-159-158.param"));
        Random random = new Random(makeSeed(Strings.toByteArray(keyID))); // Need a consistent random... TODO: maybe a better way
        Pairing pairing = PairingFactory.getInstance().getPairing(curveParameters, random);
        Element g = pairing.getG2().newRandomElement();

        // we have to do this as the JPBC library ignores the random number generator passed in as
        // a parameter.
        // TODO: need to sort out source of randomness.
        random = new SecureRandom();
        pairing = PairingFactory.getInstance().getPairing(curveParameters, random);

        BLS01Parameters blsParameters = new BLS01Parameters(curveParameters, g.getImmutable());

        kpGen.init(new BLS01KeyGenerationParameters((SecureRandom) random, blsParameters));

        AsymmetricCipherKeyPair kp = kpGen.generateKeyPair();

        sharedPrivateKeyMap.init(keyID, numberOfPeers);
        sharedPublicKeyMap.init(keyID, numberOfPeers);

        hMap.put(keyID, keyGenParams.getH());
        paramsMap.put(keyID, blsParameters);

        return kp;
    } else {/*from   ww  w.  j  a  v a 2  s.c  o  m*/
        throw new IllegalStateException("Key " + keyID + " already exists.");
    }
}