<commit_before>package com.hpe.caf.services.audit.api;

import com.hpe.caf.auditing.schema.AuditEvent;
import com.hpe.caf.auditing.schema.AuditEventParam;
import com.hpe.caf.auditing.schema.AuditEventParamType;
import com.hpe.caf.auditing.schema.AuditedApplication;

import org.junit.Test;
import org.junit.runner.RunWith;

import org.mockito.Mockito;

import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import javax.xml.bind.JAXBException;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

import java.nio.charset.StandardCharsets;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ApplicationAddPost.class,JAXBUnmarshal.class})
public class ApplicationAddPostTest {

    private static String AUDIT_EVENTS_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<AuditedApplication\n" +
            "\t\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
            "\t\txmlns=\"http://www.hpe.com/CAF/Auditing/Schema/AuditedApplication.xsd\"\n" +
            "\t\txsi:schemaLocation=\"http://www.hpe.com/CAF/Auditing/Schema/AuditedApplication.xsd\"\n" +
            ">\n" +
            "\t<ApplicationId>TestApplication</ApplicationId>\n" +
            "\t<AuditEvents>\n" +
            "\t\t<AuditEvent>\n" +
            "\t\t\t<TypeId>TestEvent1</TypeId>\n" +
            "\t\t\t<CategoryId>TestCategory1</CategoryId>\n" +
            "\t\t\t<Params>\n" +
            "\t\t\t\t<Param>\n" +
            "\t\t\t\t\t<Name>StringType</Name>\n" +
            "\t\t\t\t\t<Type>string</Type>\n" +
            "\t\t\t\t\t<ColumnName>StringType</ColumnName>\n" +
            "\t\t\t\t\t<Description>Description for StringType</Description>\n" +
            "\t\t\t\t</Param>\n" +
            "\t\t\t</Params>\n" +
            "\t\t</AuditEvent>\n" +
            "\t</AuditEvents>\n" +
            "</AuditedApplication>\n" +
            "\n";

    @Test
    public void testAddPost_Success_RegisterApplication () throws Exception {

        //  Mock JAXB xml binding code.
        AuditedApplication auditedApp = getAuditedApplication("TestApplication");
        PowerMockito.mockStatic(JAXBUnmarshal.class);
        PowerMockito.when(JAXBUnmarshal.bindAuditEventsXml(Mockito.any()))
                .thenReturn(auditedApp);

        //  Mock DatabaseHelper calls.
        DatabaseHelper mockDatabaseHelper = Mockito.mock(DatabaseHelper.class);
        Mockito.doNothing().when(mockDatabaseHelper).createAuditManagementSchema();
        Mockito.when(mockDatabaseHelper.doesApplicationEventsRowExist(Mockito.anyString())).thenReturn(false);
        Mockito.doNothing().when(mockDatabaseHelper).insertApplicationEventsRow(Mockito.anyString(),Mockito.anyString());
        PowerMockito.whenNew(DatabaseHelper.class).withArguments(Mockito.any()).thenReturn(mockDatabaseHelper);

        //  Set-up test database connection properties.
        HashMap<String, String> newEnv  = new HashMap<>();
        newEnv.put("database.url","testUrl");
        newEnv.put("database.username","testUserName");
        newEnv.put("database.password","<PASSWORD>");
        TestUtil.setSystemEnvironmentFields(newEnv);

        //  Test successful run of applications endpoint.
        InputStream auditConfigXMLStream = new ByteArrayInputStream(AUDIT_EVENTS_XML.getBytes(StandardCharsets.UTF_8));
        ApplicationAddPost.addApplication(auditConfigXMLStream);

        Mockito.verify(mockDatabaseHelper, Mockito.times(1)).createAuditManagementSchema();
        Mockito.verify(mockDatabaseHelper, Mockito.times(1)).doesApplicationEventsRowExist(Mockito.anyString());
        Mockito.verify(mockDatabaseHelper, Mockito.times(1)).insertApplicationEventsRow(Mockito.anyString(),Mockito.anyString());

    }

    @Test
    public void testAddPost_Success_ModifyApplication () throws Exception {

        //  Mock JAXB xml binding code.
        AuditedApplication auditedApp = getAuditedApplication("TestApplication");
        PowerMockito.mockStatic(JAXBUnmarshal.class);
        PowerMockito.when(JAXBUnmarshal.bindAuditEventsXml(Mockito.any()))
                .thenReturn(auditedApp);

        //  Mock DatabaseHelper calls.
        DatabaseHelper mockDatabaseHelper = Mockito.mock(DatabaseHelper.class);
        Mockito.doNothing().when(mockDatabaseHelper).createAuditManagementSchema();
        Mockito.when(mockDatabaseHelper.doesApplicationEventsRowExist(Mockito.anyString())).thenReturn(true);
        Mockito.doNothing().when(mockDatabaseHelper).updateApplicationEventsRow(Mockito.anyString(),Mockito.anyString());
        List<String> tenants = new ArrayList<>();
        tenants.add("tenant1");
        tenants.add("tenant2");
        Mockito.when(mockDatabaseHelper.getTenantsForApp(Mockito.anyString())).thenReturn(tenants);
        Mockito.when(mockDatabaseHelper.doesColumnExist(Mockito.anyString(),Mockito.anyString(),Mockito.anyString())).thenReturn(false);
        Mockito.doNothing().when(mockDatabaseHelper).addColumn(Mockito.anyString());
        PowerMockito.whenNew(DatabaseHelper.class).withArguments(Mockito.any()).thenReturn(mockDatabaseHelper);

        //  Test successful run of applications endpoint.
        InputStream auditConfigXMLStream = new ByteArrayInputStream(AUDIT_EVENTS_XML.getBytes(StandardCharsets.UTF_8));
        ApplicationAddPost.addApplication(auditConfigXMLStream);
        auditConfigXMLStream.close();

        Mockito.verify(mockDatabaseHelper, Mockito.times(1)).createAuditManagementSchema();
        Mockito.verify(mockDatabaseHelper, Mockito.times(1)).doesApplicationEventsRowExist(Mockito.anyString());
        Mockito.verify(mockDatabaseHelper, Mockito.times(1)).updateApplicationEventsRow(Mockito.anyString(),Mockito.anyString());
        Mockito.verify(mockDatabaseHelper, Mockito.times(1)).getTenantsForApp(Mockito.anyString());
        Mockito.verify(mockDatabaseHelper, Mockito.times(2)).doesColumnExist(Mockito.anyString(),Mockito.anyString(),Mockito.anyString());
        Mockito.verify(mockDatabaseHelper, Mockito.times(2)).addColumn(Mockito.anyString());

    }

    @Test(expected = BadRequestException.class)
    public void testAddPost_Failure_InvalidAuditEventsFile () throws Exception {

        //  Test expected failure run due to invalid XML.
        String INVALID_AUDIT_EVENTS_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<AuditedApplication\n" +
                "\t\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                "\t\txmlns=\"http://www.hpe.com/CAF/Auditing/Schema/AuditedApplication.xsd\"\n" +<commit_msg>Unit test change<commit_after>package com.hpe.caf.services.audit.api;

import com.hpe.caf.auditing.schema.AuditEvent;
import com.hpe.caf.auditing.schema.AuditEventParam;
import com.hpe.caf.auditing.schema.AuditEventParamType;
import com.hpe.caf.auditing.schema.AuditedApplication;

import org.junit.Test;
import org.junit.runner.RunWith;

import org.mockito.Mockito;

import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import javax.xml.bind.JAXBException;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

import java.nio.charset.StandardCharsets;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ApplicationAddPost.class,JAXBUnmarshal.class})
public class ApplicationAddPostTest {

    private static String AUDIT_EVENTS_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<AuditedApplication\n" +
            "\t\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
            "\t\txmlns=\"http://www.hpe.com/CAF/Auditing/Schema/AuditedApplication.xsd\"\n" +
            "\t\txsi:schemaLocation=\"http://www.hpe.com/CAF/Auditing/Schema/AuditedApplication.xsd\"\n" +
            ">\n" +
            "\t<ApplicationId>TestApplication</ApplicationId>\n" +
            "\t<AuditEvents>\n" +
            "\t\t<AuditEvent>\n" +
            "\t\t\t<TypeId>TestEvent1</TypeId>\n" +
            "\t\t\t<CategoryId>TestCategory1</CategoryId>\n" +
            "\t\t\t<Params>\n" +
            "\t\t\t\t<Param>\n" +
            "\t\t\t\t\t<Name>StringType</Name>\n" +
            "\t\t\t\t\t<Type>string</Type>\n" +
            "\t\t\t\t\t<ColumnName>StringType</ColumnName>\n" +
            "\t\t\t\t\t<Description>Description for StringType</Description>\n" +
            "\t\t\t\t</Param>\n" +
            "\t\t\t</Params>\n" +
            "\t\t</AuditEvent>\n" +
            "\t</AuditEvents>\n" +
            "</AuditedApplication>\n" +
            "\n";

    @Test
    public void testAddPost_Success_RegisterApplication () throws Exception {

        //  Mock JAXB xml binding code.
        AuditedApplication auditedApp = getAuditedApplication("TestApplication");
        PowerMockito.mockStatic(JAXBUnmarshal.class);
        PowerMockito.when(JAXBUnmarshal.bindAuditEventsXml(Mockito.any()))
                .thenReturn(auditedApp);

        //  Mock DatabaseHelper calls.
        DatabaseHelper mockDatabaseHelper = Mockito.mock(DatabaseHelper.class);
        Mockito.doNothing().when(mockDatabaseHelper).createAuditManagementSchema();
        Mockito.when(mockDatabaseHelper.doesApplicationEventsRowExist(Mockito.anyString())).thenReturn(false);
        Mockito.doNothing().when(mockDatabaseHelper).insertApplicationEventsRow(Mockito.anyString(),Mockito.anyString());
        PowerMockito.whenNew(DatabaseHelper.class).withArguments(Mockito.any()).thenReturn(mockDatabaseHelper);

        //  Set-up test database connection properties.
        HashMap<String, String> newEnv  = new HashMap<>();
        newEnv.put("database.url","testUrl");
        newEnv.put("database.username","testUserName");
        newEnv.put("database.password","<PASSWORD>");
        TestUtil.setSystemEnvironmentFields(newEnv);

        //  Test successful run of applications endpoint.
        InputStream auditConfigXMLStream = new ByteArrayInputStream(AUDIT_EVENTS_XML.getBytes(StandardCharsets.UTF_8));
        ApplicationAddPost.addApplication(auditConfigXMLStream);

        Mockito.verify(mockDatabaseHelper, Mockito.times(1)).createAuditManagementSchema();
        Mockito.verify(mockDatabaseHelper, Mockito.times(1)).doesApplicationEventsRowExist(Mockito.anyString());
        Mockito.verify(mockDatabaseHelper, Mockito.times(1)).insertApplicationEventsRow(Mockito.anyString(),Mockito.anyString());

    }

    @Test
    public void testAddPost_Success_ModifyApplication () throws Exception {

        //  Mock JAXB xml binding code.
        AuditedApplication auditedApp = getAuditedApplication("TestApplication");
        PowerMockito.mockStatic(JAXBUnmarshal.class);
        PowerMockito.when(JAXBUnmarshal.bindAuditEventsXml(Mockito.any()))
                .thenReturn(auditedApp);

        //  Mock DatabaseHelper calls.
        DatabaseHelper mockDatabaseHelper = Mockito.mock(DatabaseHelper.class);
        Mockito.doNothing().when(mockDatabaseHelper).createAuditManagementSchema();
        Mockito.when(mockDatabaseHelper.doesApplicationEventsRowExist(Mockito.anyString())).thenReturn(true);
        Mockito.doNothing().when(mockDatabaseHelper).updateApplicationEventsRow(Mockito.anyString(),Mockito.anyString());
        List<String> tenants = new ArrayList<>();
        tenants.add("tenant1");
        tenants.add("tenant2");
        Mockito.when(mockDatabaseHelper.getTenantsForApp(Mockito.anyString())).thenReturn(tenants);
        Mockito.when(mockDatabaseHelper.doesColumnExist(Mockito.anyString(),Mockito.anyString(),Mockito.anyString())).thenReturn(false);
        Mockito.doNothing().when(mockDatabaseHelper).addColumn(Mockito.anyString());
        PowerMockito.whenNew(DatabaseHelper.class).withArguments(Mockito.any()).thenReturn(mockDatabaseHelper);

        //  Set-up test database connection properties.
        HashMap<String, String> newEnv  = new HashMap<>();
        newEnv.put("database.url","testUrl");
        newEnv.put("database.username","testUserName");
        newEnv.put("database.password","<PASSWORD>");
        TestUtil.setSystemEnvironmentFields(newEnv);

        //  Test successful run of applications endpoint.
        InputStream auditConfigXMLStream = new ByteArrayInputStream(AUDIT_EVENTS_XML.getBytes(StandardCharsets.UTF_8));
        ApplicationAddPost.addApplication(auditConfigXMLStream);
        auditConfigXMLStream.close();

        Mockito.verify(mockDatabaseHelper, Mockito.times(1)).createAuditManagementSchema();
        Mockito.verify(mockDatabaseHelper, Mockito.times(1)).doesApplicationEventsRowExist(Mockito.anyString());
        Mockito.verify(mockDatabaseHelper, Mockito.times(1)).updateApplicationEventsRow(Mockito.anyString(),Mockito.anyString());
        Mockito.verify(mockDatabaseHelper, Mockito.times(1)).getTenantsForApp(Mockito.anyString());
        Mockito.verify(mockDatabaseHelper, Mockito.times(2)).doesColumnExist(Mockito.anyString(),Mockito.anyString(),Mockito.anyString());
        Mockito.verify(mockDatabaseHelper, Mockito.times(2)).addColumn(Mockito.anyString());

    }

    @Test(expected = BadRequestException.class)
    public void testAddPost_Failure_InvalidAuditEventsFile () throws Exception {

        //  Test expected failure run due to invalid XML.
        String INVALID_AUDIT_EVENTS_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<AuditedApplication\n" +
                "\t\txmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                "\t\txmlns=\"http://www.hpe.com/CAF/Auditing/Schema/AuditedApplication.xsd\"\n" +
{'fraction_non_alphanumeric': 0.13604802545927963, 'fraction_numerical': 0.004628959930565601, 'mean_word_length': 3.6508577194752774, 'pattern_counts': {'":': 0, '<': 70, '<?xml version=': 4, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 10, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4671888', 'n_tokens_mistral': 4149, 'n_tokens_neox': 4078, 'n_words': 442}