/*-
 * ~~~~~~licensing~~~~~~
 * cef-logger-layout
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.emdev.cef;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;

import static org.junit.jupiter.api.Assertions.*;

public class CEFRecordTest {

    private final static ObjectMapper mapper = new ObjectMapper();
    private final static String SEPARATOR = "|";
    private final static String HEADER = "CEF:0|VENDOR|PRODUCT|1.0.0";
    public static final int CEF_FIELD_COUNT = 8;

    @Test
    public void cefRecordCanBeBuiltFromEmptyJson() {
        JsonNode jsonNode = readResource("examples/empty.json");
        CEFRecord record = CEFRecord.fromJson(jsonNode);
        assertNotNull(record);
        assertNotEquals(0, record.getEnd());
    }

    @Test
    public void cefRecordCanBeBuiltFromJsonWithouInfo() {
        JsonNode jsonNode = readResource("examples/noinfo.json");
        CEFRecord record = CEFRecord.fromJson(jsonNode);
        assertNotNull(record);
        assertNotEquals(0, record.getEnd());
        assertEquals("type", record.getType());
        assertEquals("action", record.getAction());
        assertEquals("user", record.getPerformedBy());
        assertEquals("0", record.getSeverity());
    }

    @Test
    public void testFormatting() {
        JsonNode jsonNode = readResource("examples/almostFull.json");
        CEFRecord record = CEFRecord.fromJson(jsonNode);
        assertNotNull(record);
        assertEquals("10", record.getSeverity());
        assertEquals("", record.getSuser());
        assertEquals("", record.getSuid());
        String message = record.asString(HEADER, SEPARATOR);
        String[] mainFields = message.split("\\|");
        assertEquals(CEF_FIELD_COUNT, mainFields.length);
        assertEquals("type", mainFields[4]);
        assertEquals("action", mainFields[5]);
        assertEquals("10", mainFields[6]);
        String[] secFields = mainFields[7].split("\\s");
        assertEquals(5, secFields.length);
        assertEquals("src=127.0.0.1", secFields[0]);
        assertEquals("dst=127.0.0.1:443", secFields[1]);
        assertEquals("shost=localhost.localdomain", secFields[2]);
        assertEquals("msg=CODE\\=400", secFields[3]);
        assertTrue(secFields[4].matches("^end=\\d+$"));
    }

    private static JsonNode readResource(String name) {
        try (InputStream in = ClassLoader.getSystemResourceAsStream(name)){
            return mapper.readTree(in);
        } catch (IOException e) {
            return null;
        }
    }
}
