Search in sources :

Example 1 with JSONObject

use of net.minidev.json.JSONObject in project cas by apereo.

the class TokenWebApplicationServiceResponseBuilder method generateToken.

/**
     * Generate token string.
     *
     * @param service    the service
     * @param parameters the parameters
     * @return the jwt
     */
protected String generateToken(final Service service, final Map<String, String> parameters) {
    try {
        final String ticketId = parameters.get(CasProtocolConstants.PARAMETER_TICKET);
        final Cas30ServiceTicketValidator validator = new Cas30ServiceTicketValidator(casProperties.getServer().getPrefix());
        final Assertion assertion = validator.validate(ticketId, service.getId());
        final JWTClaimsSet.Builder claims = new JWTClaimsSet.Builder().audience(service.getId()).issuer(casProperties.getServer().getPrefix()).jwtID(ticketId).issueTime(assertion.getAuthenticationDate()).subject(assertion.getPrincipal().getName());
        assertion.getAttributes().forEach(claims::claim);
        assertion.getPrincipal().getAttributes().forEach(claims::claim);
        if (assertion.getValidUntilDate() != null) {
            claims.expirationTime(assertion.getValidUntilDate());
        } else {
            final ZonedDateTime dt = ZonedDateTime.now().plusSeconds(ticketGrantingTicketExpirationPolicy.getTimeToLive());
            claims.expirationTime(DateTimeUtils.dateOf(dt));
        }
        final JWTClaimsSet claimsSet = claims.build();
        final JSONObject object = claimsSet.toJSONObject();
        return tokenCipherExecutor.encode(object.toJSONString());
    } catch (final Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Cas30ServiceTicketValidator(org.jasig.cas.client.validation.Cas30ServiceTicketValidator) JSONObject(net.minidev.json.JSONObject) ZonedDateTime(java.time.ZonedDateTime) JWTClaimsSet(com.nimbusds.jwt.JWTClaimsSet) Assertion(org.jasig.cas.client.validation.Assertion)

Example 2 with JSONObject

use of net.minidev.json.JSONObject in project nifi by apache.

the class TestPutRethinkDB method testValidSingleMessage.

@Test
public void testValidSingleMessage() {
    runner.setProperty(PutRethinkDB.MAX_DOCUMENTS_SIZE, "1 MB");
    runner.assertValid();
    result.remove(PutRethinkDB.RESULT_FIRST_ERROR_KEY);
    result.remove(PutRethinkDB.RESULT_WARNINGS_KEY);
    JSONObject message = new JSONObject();
    message.put("hello", "rethinkdb");
    byte[] bytes = message.toJSONString().getBytes();
    runner.enqueue(bytes);
    runner.run(1, true, true);
    runner.assertAllFlowFilesTransferred(PutRethinkDB.REL_SUCCESS, 1);
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(PutRethinkDB.REL_SUCCESS);
    assertEquals(flowFiles.get(0).getAttribute(PutRethinkDB.RETHINKDB_INSERT_RESULT_DELETED_KEY), "0");
    assertEquals(flowFiles.get(0).getAttribute(PutRethinkDB.RETHINKDB_INSERT_RESULT_ERROR_KEY), "0");
    assertNotNull(flowFiles.get(0).getAttribute(PutRethinkDB.RETHINKDB_INSERT_RESULT_GENERATED_KEYS_KEY));
    assertEquals(flowFiles.get(0).getAttribute(PutRethinkDB.RETHINKDB_INSERT_RESULT_INSERTED_KEY), "1");
    assertEquals(flowFiles.get(0).getAttribute(PutRethinkDB.RETHINKDB_INSERT_RESULT_REPLACED_KEY), "0");
    assertEquals(flowFiles.get(0).getAttribute(PutRethinkDB.RETHINKDB_INSERT_RESULT_SKIPPED_KEY), "0");
    assertEquals(flowFiles.get(0).getAttribute(PutRethinkDB.RETHINKDB_INSERT_RESULT_UNCHANGED_KEY), "0");
    assertEquals(flowFiles.get(0).getAttribute(PutRethinkDB.RETHINKDB_INSERT_RESULT_FIRST_ERROR_KEY), "null");
    assertEquals(flowFiles.get(0).getAttribute(PutRethinkDB.RETHINKDB_INSERT_RESULT_FIRST_ERROR_KEY), "null");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) JSONObject(net.minidev.json.JSONObject) Test(org.junit.Test)

Example 3 with JSONObject

use of net.minidev.json.JSONObject in project nifi by apache.

the class ITDeleteRethinkDBTest method testDeleteDocumentByIdNoChanges.

@Test
public void testDeleteDocumentByIdNoChanges() {
    JSONObject message = new JSONObject();
    message.put("id", "11");
    message.put("value", "one");
    RethinkDB.r.db(dbName).table(table).insert(message).run(connection);
    long count = RethinkDB.r.db(dbName).table(table).count().run(connection);
    assertEquals("Count should be same", 1L, count);
    runner.setProperty(AbstractRethinkDBProcessor.RETHINKDB_DOCUMENT_ID, "${rethinkdb.id}");
    runner.setProperty(DeleteRethinkDB.RETURN_CHANGES, "false");
    Map<String, String> props = new HashMap<>();
    props.put("rethinkdb.id", "11");
    runner.enqueue(new byte[] {}, props);
    runner.run(1, true, true);
    runner.assertAllFlowFilesTransferred(AbstractRethinkDBProcessor.REL_SUCCESS, 1);
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(AbstractRethinkDBProcessor.REL_SUCCESS);
    assertEquals("Flow file count should be same", 1, flowFiles.size());
    assertEquals("Error should be null", null, flowFiles.get(0).getAttribute(AbstractRethinkDBProcessor.RETHINKDB_ERROR_MESSAGE));
    assertEquals(flowFiles.get(0).getAttribute(DeleteRethinkDB.RETHINKDB_DELETE_RESULT_DELETED_KEY), "1");
    assertEquals(flowFiles.get(0).getAttribute(DeleteRethinkDB.RETHINKDB_DELETE_RESULT_ERROR_KEY), "0");
    assertEquals(flowFiles.get(0).getAttribute(DeleteRethinkDB.RETHINKDB_DELETE_RESULT_INSERTED_KEY), "0");
    assertEquals(flowFiles.get(0).getAttribute(DeleteRethinkDB.RETHINKDB_DELETE_RESULT_REPLACED_KEY), "0");
    assertEquals(flowFiles.get(0).getAttribute(DeleteRethinkDB.RETHINKDB_DELETE_RESULT_SKIPPED_KEY), "0");
    assertEquals(flowFiles.get(0).getAttribute(DeleteRethinkDB.RETHINKDB_DELETE_RESULT_UNCHANGED_KEY), "0");
    assertEquals(flowFiles.get(0).getSize(), 0);
    Map<String, Object> document = RethinkDB.r.db(dbName).table(table).get("1").run(connection);
    assertEquals("Document should be null", document, null);
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) JSONObject(net.minidev.json.JSONObject) HashMap(java.util.HashMap) JSONObject(net.minidev.json.JSONObject) Test(org.junit.Test)

Example 4 with JSONObject

use of net.minidev.json.JSONObject in project nifi by apache.

the class ITGetRethinkDBTest method testGetDocumentByHardCodedId.

@Test
public void testGetDocumentByHardCodedId() {
    JSONObject message = new JSONObject();
    message.put("id", "2");
    message.put("value", "two");
    RethinkDB.r.db(dbName).table(table).insert(message).run(connection);
    long count = RethinkDB.r.db(dbName).table(table).count().run(connection);
    assertEquals("Count should be same", 1, count);
    runner.setProperty(GetRethinkDB.RETHINKDB_DOCUMENT_ID, "2");
    runner.assertValid();
    Map<String, String> props = new HashMap<>();
    runner.enqueue(new byte[] {}, props);
    runner.run(1, true, true);
    runner.assertAllFlowFilesTransferred(AbstractRethinkDBProcessor.REL_SUCCESS, 1);
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(AbstractRethinkDBProcessor.REL_SUCCESS);
    assertEquals("Flow file count should be same", 1, flowFiles.size());
    assertEquals("Error should be null", null, flowFiles.get(0).getAttribute(AbstractRethinkDBProcessor.RETHINKDB_ERROR_MESSAGE));
    assertEquals("Content should be same size", message.toString().length(), flowFiles.get(0).getSize());
    flowFiles.get(0).assertContentEquals(message.toString());
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) JSONObject(net.minidev.json.JSONObject) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 5 with JSONObject

use of net.minidev.json.JSONObject in project nifi by apache.

the class ITPutRethinkDBTest method testValidArrayMessage.

@Test
public void testValidArrayMessage() {
    runner.assertValid();
    RethinkDB.r.db(dbName).table(table).delete().run(connection);
    long count = RethinkDB.r.db(dbName).table(table).count().run(connection);
    assertEquals("Count should be same", 0L, count);
    JSONObject message1 = new JSONObject();
    message1.put("hello", "rethinkdb");
    JSONObject message2 = new JSONObject();
    message2.put("hello2", "rethinkdb2");
    JSONArray array = new JSONArray();
    array.add(message1);
    array.add(message2);
    byte[] bytes = array.toJSONString().getBytes();
    runner.enqueue(bytes);
    runner.run(1, true, true);
    runner.assertAllFlowFilesTransferred(PutRethinkDB.REL_SUCCESS, 1);
    List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship(PutRethinkDB.REL_SUCCESS);
    assertEquals(flowFiles.get(0).getAttribute(PutRethinkDB.RETHINKDB_INSERT_RESULT_DELETED_KEY), "0");
    assertEquals(flowFiles.get(0).getAttribute(PutRethinkDB.RETHINKDB_INSERT_RESULT_ERROR_KEY), "0");
    assertNotNull(flowFiles.get(0).getAttribute(PutRethinkDB.RETHINKDB_INSERT_RESULT_GENERATED_KEYS_KEY));
    assertEquals(flowFiles.get(0).getAttribute(PutRethinkDB.RETHINKDB_INSERT_RESULT_INSERTED_KEY), "2");
    assertEquals(flowFiles.get(0).getAttribute(PutRethinkDB.RETHINKDB_INSERT_RESULT_REPLACED_KEY), "0");
    assertEquals(flowFiles.get(0).getAttribute(PutRethinkDB.RETHINKDB_INSERT_RESULT_SKIPPED_KEY), "0");
    assertEquals(flowFiles.get(0).getAttribute(PutRethinkDB.RETHINKDB_INSERT_RESULT_UNCHANGED_KEY), "0");
    assertEquals(flowFiles.get(0).getAttribute(PutRethinkDB.RETHINKDB_INSERT_RESULT_FIRST_ERROR_KEY), "null");
    count = RethinkDB.r.db(dbName).table(table).count().run(connection);
    assertEquals("Count should be same", 2L, count);
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) JSONObject(net.minidev.json.JSONObject) JSONArray(org.json.simple.JSONArray) Test(org.junit.Test)

Aggregations

JSONObject (net.minidev.json.JSONObject)254 JSONArray (net.minidev.json.JSONArray)49 Test (org.junit.Test)39 Test (org.testng.annotations.Test)38 JSONParser (net.minidev.json.parser.JSONParser)23 HashMap (java.util.HashMap)22 MockFlowFile (org.apache.nifi.util.MockFlowFile)16 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)14 Test (org.junit.jupiter.api.Test)14 Map (java.util.Map)13 ParseException (net.minidev.json.parser.ParseException)13 JSONConverterException (org.btrplace.json.JSONConverterException)13 SignedJWT (com.nimbusds.jwt.SignedJWT)12 Node (org.btrplace.model.Node)12 VM (org.btrplace.model.VM)12 JWSHeader (com.nimbusds.jose.JWSHeader)10 RSASSASigner (com.nimbusds.jose.crypto.RSASSASigner)10 AuthenticationContext (org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext)10 OpenAPI (io.swagger.v3.oas.models.OpenAPI)9 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)9