/**
 * Copyright © 2012-2024 by Data Virtuality GmbH
 */
package org.teiid.adminapi.impl;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.teiid.adminapi.impl.DataPolicyMetadata.PermissionMetaData;

/**
 * @author Allan Vital
 * @date 2024.05.17
 */
public class TestPermissionMetaData {

    PermissionMetaData p1;
    PermissionMetaData p2;
    PermissionMetaData p3;
    PermissionMetaData p4;
    PermissionMetaData p5;
    PermissionMetaData p6;
    PermissionMetaData p7;

    @BeforeEach
    public void setup() {
        p1 = new PermissionMetaData();
        p1.setResourceName("r");
        p1.setAllowCreate(true);

        p2 = new PermissionMetaData();
        p2.setResourceName("r");
        p2.setAllowRead(true);

        p3 = new PermissionMetaData();
        p3.setResourceName("r");
        p3.setAllowUpdate(true);

        p4 = new PermissionMetaData();
        p4.setResourceName("r");
        p4.setAllowDelete(true);

        p5 = new PermissionMetaData();
        p5.setResourceName("r");
        p5.setAllowExecute(true);

        p6 = new PermissionMetaData();
        p6.setResourceName("r");
        p6.setAllowAlter(true);

        p7 = new PermissionMetaData();
        p7.setResourceName("r");
        p7.setAllowLanguage(true);

        createPermissionMetadata(p1, p2, p3, p4, p5, p6, p7);
    }

    private void createPermissionMetadata(PermissionMetaData...permissionsMetadata) {
        for (PermissionMetaData permissionMetadata : permissionsMetadata) {
            DataPolicyMetadata policy = new DataPolicyMetadata();
            policy.addPermission(permissionMetadata);
        }
    }


    @Test
    public void testDefault() {
        Assertions.assertTrue(p1.getAllowCreate());
        Assertions.assertTrue(p2.getAllowRead());
        Assertions.assertTrue(p3.getAllowUpdate());
        Assertions.assertTrue(p4.getAllowDelete());
        Assertions.assertTrue(p5.getAllowExecute());
        Assertions.assertTrue(p6.getAllowAlter());
        Assertions.assertTrue(p7.getAllowLanguage());
    }

    @Test
    public void testNotConcatenateDifferentResources() {
        p2.setResourceName("othername");
        p1.concatenatePermissionMetadata(p2);

        Assertions.assertTrue(p1.getAllowCreate());
        Assertions.assertNull(p1.getAllowRead());
        Assertions.assertNull(p2.getAllowCreate());
        Assertions.assertTrue(p2.getAllowRead());
    }

    @Test
    public void testConcatenate1() {
        p1.concatenatePermissionMetadata(p2);
        Assertions.assertTrue(p1.getAllowCreate());
        Assertions.assertTrue(p1.getAllowRead());
        Assertions.assertNull(p2.getAllowCreate());
        Assertions.assertTrue(p2.getAllowRead());
    }

    @Test
    public void testConcatenate2() {
        p1.concatenatePermissionMetadata(p2);
        p1.concatenatePermissionMetadata(p3);

        Assertions.assertTrue(p1.getAllowCreate());
        Assertions.assertTrue(p1.getAllowRead());
        Assertions.assertTrue(p1.getAllowUpdate());

        Assertions.assertNull(p2.getAllowCreate());
        Assertions.assertTrue(p2.getAllowRead());
        Assertions.assertNull(p2.getAllowUpdate());

        Assertions.assertNull(p3.getAllowRead());
        Assertions.assertNull(p3.getAllowCreate());
        Assertions.assertTrue(p3.getAllowUpdate());
    }

    @Test
    public void testConcatenate3() {
        p1.concatenatePermissionMetadata(p2);
        p1.concatenatePermissionMetadata(p3);
        p1.concatenatePermissionMetadata(p4);
        p1.concatenatePermissionMetadata(p5);
        p1.concatenatePermissionMetadata(p6);
        p1.concatenatePermissionMetadata(p7);

        Assertions.assertTrue(p1.getAllowCreate());
        Assertions.assertTrue(p1.getAllowRead());
        Assertions.assertTrue(p1.getAllowUpdate());
        Assertions.assertTrue(p1.getAllowDelete());
        Assertions.assertTrue(p1.getAllowExecute());
        Assertions.assertTrue(p1.getAllowAlter());
        Assertions.assertTrue(p1.getAllowLanguage());
    }

}