Search in sources :

Example 1 with MapConverter

use of com.redhat.cloud.notifications.db.converters.MapConverter in project notifications-backend by RedHatInsights.

the class CamelTypeProcessorTest method testCamelEndpointProcessing.

@Test
void testCamelEndpointProcessing() {
    // We need input data for the test.
    Event event = buildEvent();
    Endpoint endpoint1 = buildCamelEndpoint(event.getAction().getAccountId());
    CamelProperties properties1 = endpoint1.getProperties(CamelProperties.class);
    Endpoint endpoint2 = buildCamelEndpoint(event.getAction().getAccountId());
    // Let's trigger the processing.
    List<NotificationHistory> result = processor.process(event, List.of(endpoint1, endpoint2));
    // Two endpoints should have been processed.
    assertEquals(2, result.size());
    // Metrics should report the same thing.
    micrometerAssertionHelper.assertCounterIncrement(PROCESSED_COUNTER_NAME, 2, SUB_TYPE_KEY, SUB_TYPE);
    micrometerAssertionHelper.assertCounterIncrement(PROCESSED_COUNTER_NAME, 0, SUB_TYPE_KEY, "other-type");
    micrometerAssertionHelper.assertCounterIncrement(PROCESSED_COUNTER_NAME, 0);
    // Let's have a look at the first result entry fields.
    assertEquals(event, result.get(0).getEvent());
    assertEquals(endpoint1, result.get(0).getEndpoint());
    assertEquals(CAMEL, result.get(0).getEndpointType());
    assertNotNull(result.get(0).getInvocationTime());
    // The invocation will be complete when the response from Camel has been received.
    assertFalse(result.get(0).isInvocationResult());
    // Now let's check the Kafka messages sent to the outgoing channel.
    InMemorySink<String> inMemorySink = inMemoryConnector.sink(TOCAMEL_CHANNEL);
    // The channel should have received two messages.
    assertEquals(2, inMemorySink.received().size());
    // We'll only check the payload and metadata of the first Kafka message.
    Message<String> message = inMemorySink.received().get(0);
    // The payload should contain the action events.
    JsonObject payload = new JsonObject(message.getPayload());
    assertNotNull(payload.getJsonArray("events").getJsonObject(0).getString("payload"));
    // The processor added a 'notif-metadata' field to the payload, let's have a look at it.
    JsonObject notifMetadata = payload.getJsonObject(NOTIF_METADATA_KEY);
    assertEquals(properties1.getDisableSslVerification().toString(), notifMetadata.getString("trustAll"));
    assertEquals(properties1.getUrl(), notifMetadata.getString("url"));
    assertEquals(endpoint1.getSubType(), notifMetadata.getString("type"));
    // Todo: NOTIF-429 backward compatibility change - Remove soon.
    assertEquals(properties1.getSubType(), notifMetadata.getString("type"));
    assertEquals(new MapConverter().convertToDatabaseColumn(properties1.getExtras()), notifMetadata.getString("extras"));
    assertEquals(properties1.getSecretToken(), notifMetadata.getString(TOKEN_HEADER));
    checkBasicAuthentication(notifMetadata, properties1.getBasicAuthentication());
    // Finally, we need to check the Kafka message metadata.
    UUID historyId = result.get(0).getId();
    checkKafkaMetadata(message, historyId, endpoint1.getSubType());
    checkCloudEventMetadata(message, historyId, endpoint1.getAccountId(), endpoint1.getSubType());
    checkTracingMetadata(message);
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) NotificationHistory(com.redhat.cloud.notifications.models.NotificationHistory) MapConverter(com.redhat.cloud.notifications.db.converters.MapConverter) CamelProperties(com.redhat.cloud.notifications.models.CamelProperties) Event(com.redhat.cloud.notifications.models.Event) JsonObject(io.vertx.core.json.JsonObject) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test) QuarkusTest(io.quarkus.test.junit.QuarkusTest)

Example 2 with MapConverter

use of com.redhat.cloud.notifications.db.converters.MapConverter in project notifications-backend by RedHatInsights.

the class CamelTypeProcessor method process.

private NotificationHistory process(Notification item) {
    Endpoint endpoint = item.getEndpoint();
    String subType = endpoint.getSubType();
    Counter processedCount = registry.counter(PROCESSED_COUNTER_NAME, "subType", subType);
    processedCount.increment();
    CamelProperties properties = (CamelProperties) endpoint.getProperties();
    Map<String, String> metaData = new HashMap<>();
    metaData.put("trustAll", String.valueOf(properties.getDisableSslVerification()));
    metaData.put("url", properties.getUrl());
    metaData.put("type", subType);
    if (properties.getSecretToken() != null && !properties.getSecretToken().isBlank()) {
        metaData.put(TOKEN_HEADER, properties.getSecretToken());
    }
    BasicAuthentication basicAuthentication = properties.getBasicAuthentication();
    if (basicAuthentication != null && basicAuthentication.getUsername() != null && basicAuthentication.getPassword() != null) {
        StringBuilder sb = new StringBuilder(basicAuthentication.getUsername());
        sb.append(":");
        sb.append(basicAuthentication.getPassword());
        String b64 = Base64Utils.encode(sb.toString());
        metaData.put("basicAuth", b64);
    }
    metaData.put("extras", new MapConverter().convertToDatabaseColumn(properties.getExtras()));
    String originalEventId = "-not provided-";
    if (item.getEvent().getId() != null) {
        originalEventId = item.getEvent().getId().toString();
    }
    metaData.put("_originalId", originalEventId);
    JsonObject payload = transformer.transform(item.getEvent().getAction());
    UUID historyId = UUID.randomUUID();
    JsonObject metadataAsJson = new JsonObject();
    payload.put(NOTIF_METADATA_KEY, metadataAsJson);
    metaData.forEach(metadataAsJson::put);
    return callCamel(item, historyId, payload, originalEventId);
}
Also used : Counter(io.micrometer.core.instrument.Counter) Endpoint(com.redhat.cloud.notifications.models.Endpoint) HashMap(java.util.HashMap) MapConverter(com.redhat.cloud.notifications.db.converters.MapConverter) CamelProperties(com.redhat.cloud.notifications.models.CamelProperties) BasicAuthentication(com.redhat.cloud.notifications.models.BasicAuthentication) JsonObject(io.vertx.core.json.JsonObject) UUID(java.util.UUID)

Aggregations

MapConverter (com.redhat.cloud.notifications.db.converters.MapConverter)2 CamelProperties (com.redhat.cloud.notifications.models.CamelProperties)2 Endpoint (com.redhat.cloud.notifications.models.Endpoint)2 JsonObject (io.vertx.core.json.JsonObject)2 UUID (java.util.UUID)2 BasicAuthentication (com.redhat.cloud.notifications.models.BasicAuthentication)1 Event (com.redhat.cloud.notifications.models.Event)1 NotificationHistory (com.redhat.cloud.notifications.models.NotificationHistory)1 Counter (io.micrometer.core.instrument.Counter)1 QuarkusTest (io.quarkus.test.junit.QuarkusTest)1 HashMap (java.util.HashMap)1 Test (org.junit.jupiter.api.Test)1