/** @file tst_pmConfig.c
 *
 *  @brief Contains the tests for pmConfig.c:
 *
 * test00: This code will test the pmConfig() routine.
 *
 *  @author GLG, MHPCC
 *
 * XXX: Untested:
 * pmConfigValidateCamera()
 * pmConfigCameraFromHeader()
 * pmConfigRecipeFromCamera()
 * pmConfigDB()
 *
 * XXXX: Must determine what to do with NULL arguments, then test it.
 *
 *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2007-06-19 18:28:38 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
 */
#include <stdio.h>
#include <string.h>
#include <pslib.h>
#include <psmodules.h>
#include "tap.h"
#include "pstap.h"

int main(int argc, char* argv[])
{
    psLogSetFormat("HLNM");
    psLogSetLevel(PS_LOG_INFO);
    psTraceSetLevel(".", 0);
    plan_tests(20);

    // Tests for pmConfigAlloc()
    // XXX: Add a MemCheckConfig() function to verify the return type
    {
        psMemId id = psMemGetId();

        pmConfig *myConfig = pmConfigAlloc();
        ok(myConfig != NULL, "pmConfigAlloc() returned non-NULL");
        skip_start(myConfig == NULL, 10, "skipping tests because pmConfigAlloc() returned NULL");
        ok(myConfig->site == NULL, "pmConfigAlloc() initialized pmConfig->site properly");
        ok(myConfig->camera == NULL, "pmConfigAlloc() initialized pmConfig->camera properly");
        ok(myConfig->cameraName == NULL, "pmConfigAlloc() initialized pmConfig->cameraName properly");
        ok(myConfig->format == NULL, "pmConfigAlloc() initialized pmConfig->format properly");
        ok(myConfig->formatName == NULL, "pmConfigAlloc() initialized pmConfig->formatName properly");
        ok(myConfig->recipes != NULL && psMemCheckMetadata(myConfig->recipes),
            "pmConfigAlloc() initialized pmConfig->recipes properly");
        ok(myConfig->recipesRead == PM_RECIPE_SOURCE_NONE, "pmConfigAlloc() initialized pmConfig->recipesRead properly");
        ok(myConfig->recipeSymbols != NULL && psMemCheckMetadata(myConfig->recipeSymbols),
            "pmConfigAlloc() initialized pmConfig->recipesSymbols properly");
        ok(myConfig->arguments != NULL && psMemCheckMetadata(myConfig->arguments),
            "pmConfigAlloc() initialized pmConfig->arguments properly");
        ok(myConfig->database == NULL, "pmConfigAlloc() initialized pmConfig->database properly");
        ok(myConfig->defaultRecipe == NULL, "pmConfigAlloc() initialized pmConfig->defaultRecipe properly");

        skip_end();
        psFree(myConfig);
        ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
    }


    // Verify error with NULL argc pointer
    {
        psMemId id = psMemGetId();
        psString str[3];
        str[0] = "ARGS:";
        str[1] = "-site";
        str[2] = "data/SampleIPPConfig";
        pmConfig *myConfig = pmConfigRead(NULL, str, "RecipeName");
        ok(myConfig == NULL, "pmConfigRead() returned NULL with NULL argc pointer");
        psFree(myConfig);
        ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
    }

    // Verify error with NULL argv pointer
    {
        psMemId id = psMemGetId();
        pmConfig *myConfig = pmConfigRead(&argc, NULL, "RecipeName");
        ok(myConfig == NULL, "pmConfigRead() returned NULL with NULL argv pointer");
        psFree(myConfig);
        ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
    }

    // Verify non-error with NULL recipe string
    {
        psMemId id = psMemGetId();
        psString str[3];
        str[0] = "ARGS:";
        str[1] = "-site";
        str[2] = "data/SampleIPPConfig";
        pmConfig *myConfig = pmConfigRead(&argc, str, NULL);
        ok(myConfig != NULL, "pmConfigRead() returned non-NULL with NULL recipe");
        psFree(myConfig);
        ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
    }

    {
        psMemId id = psMemGetId();
        bool testStatus = true;
        psMetadata *site = psMetadataAlloc();
        psMetadata *camera = psMetadataAlloc();
        psMetadata *recipe = psMetadataAlloc();
        psString str[3];
        str[0] = "ARGS:";
        str[1] = "-site";
        str[2] = "data/SampleIPPConfig";
        psS32 argc = 3;
        printf("----------------------------------------------------------------\n");
        printf("Calling pmConfigRead() with acceptable arguments.\n");
        bool rc;
        pmConfig *myConfig = pmConfigRead(&argc, str, "RecipeName");
        if (myConfig == NULL) {
            printf("TEST ERROR: pmConfigRead() returned NULL\n");
            testStatus = false;
        } else {
            //
            // Ensure that the various trace and logging values are set properly
            //
            if (1 != psTraceGetLevel("dummyTraceFunc01")) {
                printf("TEST ERROR: failed to properly set tracelevel for dummyTraceFunc01\n");
                printf("    tracelevel was %d, should be 1.\n", psTraceGetLevel("dummyTraceFunc01"));
                testStatus = false;
            }

            if (2 != psTraceGetLevel("dummyTraceFunc02")) {
                printf("TEST ERROR: failed to properly set tracelevel for dummyTraceFunc02\n");
                printf("    tracelevel was %d, should be 2.\n", psTraceGetLevel("dummyTraceFunc02"));
                testStatus = false;
            }

            if (1 != psLogGetDestination()) {
                printf("TEST ERROR: failed to properly set log destination.\n");
                printf("    it was %d, should be 1\n", psLogGetDestination());
                testStatus = false;
            }

            if (3 != psLogGetLevel()) {
                printf("TEST ERROR: failed to properly set log level.\n");
                printf("    it was %d, should be 3\n", psLogGetLevel());
                testStatus = false;
            }

            //
            // Test the several arbitrary config file strings.
            //
            psS32 tmpInt = psMetadataLookupS32(&rc, site, "ARBITRARY_STRING_S32");
            if ((rc == false) || (tmpInt != 20)) {
                printf("TEST ERROR: failed to properly set metadata integer ARBITRARY_STRING_S32.\n");
                testStatus = false;
            }

            psF32 tmpFloat = psMetadataLookupS32(&rc, site, "ARBITRARY_STRING_F32");
            if ((rc == false) || (tmpFloat != 21.0)) {
                printf("TEST ERROR: failed to properly set metadata float ARBITRARY_STRING_F32.\n");
                testStatus = false;
            }

            psF64 tmpDub = psMetadataLookupS32(&rc, site, "ARBITRARY_STRING_F64");
            if ((rc == false) || (tmpDub != 22.0)) {
                printf("TEST ERROR: failed to properly set metadata double ARBITRARY_STRING_F64.\n");
                testStatus = false;
            }

            psBool tmpBool;
            tmpBool = psMetadataLookupS32(&rc, site, "ARBITRARY_STRING_BOOL_T");
            if ((rc == false) || (tmpBool != true)) {
                printf("TEST ERROR: failed to properly set metadata double ARBITRARY_STRING_BOOL_T.\n");
                testStatus = false;
            }
            tmpBool = psMetadataLookupS32(&rc, site, "ARBITRARY_STRING_BOOL_F");
            if ((rc == false) || (tmpBool != false)) {
                printf("TEST ERROR: failed to properly set metadata double ARBITRARY_STRING_BOOL_F.\n");
                testStatus = false;
            }

            //
            // Test the database camera metadata keywords.
            //
            psMetadata *tmpMeta = psMetadataLookupMetadata(&rc, site, "CAMERAS");
            if (rc == false) {
                printf("TEST ERROR: failed to properly set metadata metadata CAMERAS.\n");
                testStatus = false;
            } else {
                psString tmpStr;
                tmpStr = psMetadataLookupStr(&rc, tmpMeta, "MEGACAM_RAW");
                if ((rc == false) || (0 != strcmp(tmpStr, "megacam_raw.config"))) {
                    printf("TEST ERROR: failed to properly set metadata metadata CAMERAS->MEGACAM_RAW.\n");
                    testStatus = false;
                }

                tmpStr = psMetadataLookupStr(&rc, tmpMeta, "MEGACAM_SPLICE");
                if ((rc == false) || (0 != strcmp(tmpStr, "megacam_splice.config"))) {
                    printf("TEST ERROR: failed to properly set metadata metadata CAMERAS->MEGACAM_SPLICE.\n");
                    testStatus = false;
                }

                tmpStr = psMetadataLookupStr(&rc, tmpMeta, "GPC1_RAW");
                if ((rc == false) || (0 != strcmp(tmpStr, "gpc1_raw.config"))) {
                    printf("TEST ERROR: failed to properly set metadata metadata CAMERAS->GPC1_RAW.\n");
                    testStatus = false;
                }

                tmpStr = psMetadataLookupStr(&rc, tmpMeta, "LRIS_BLUE");
                if ((rc == false) || (0 != strcmp(tmpStr, "lris_blue.config"))) {
                    printf("TEST ERROR: failed to properly set metadata metadata CAMERAS->LRIS_BLUE.\n");
                    testStatus = false;
                }

                tmpStr = psMetadataLookupStr(&rc, tmpMeta, "LRIS_RED");
                if ((rc == false) || (0 != strcmp(tmpStr, "lris_red.config"))) {
                    printf("TEST ERROR: failed to properly set metadata metadata CAMERAS->LRIS_RED.\n");
                    testStatus = false;
                }
            }

            //
            // Test the database metadata keywords.  This is somewhat redundant since
            // we already test metadat strings.
            //
            psString tmpStr;
            tmpStr = psMetadataLookupStr(&rc, site, "DBSERVER");
            if ((rc == false) || (0 != strcmp(tmpStr, "ippdb.ifa.hawaii.edu"))) {
                printf("TEST ERROR: failed to properly set metadata string DBSERVER.\n");
                testStatus = false;
            }

            tmpStr = psMetadataLookupStr(&rc, site, "DBUSER");
            if ((rc == false) || (0 != strcmp(tmpStr, "ipp"))) {
                printf("TEST ERROR: failed to properly set metadata string DBUSER.\n");
                testStatus = false;
            }

            tmpStr = psMetadataLookupStr(&rc, site, "DBPASSWORD");
            if ((rc == false) || (0 != strcmp(tmpStr, "password"))) {
                printf("TEST ERROR: failed to properly set metadata string DBPASSWORD.\n");
                testStatus = false;
            }
        }
        psFree(site);
        psFree(camera);
        psFree(recipe);
        ok(!psMemCheckLeaks (id, NULL, NULL, false), "no memory leaks");
    }
}
//HERE
