#!/bin/bash

# 1) Input is processed from INPUT_DIRECTORY
# 2) Output is saved to ACTUAL_DIRECTORY
# 3) Output is compared to expectation from EXPECTATION_DIRECTORY
INPUT_DIRECTORY=data/unittests/input
ACTUAL_DIRECTORY=data/unittests/actual
EXPECTATION_DIRECTORY=data/unittests/expectations

/bin/mkdir -p $ACTUAL_DIRECTORY
/bin/rm -f $ACTUAL_DIRECTORY/*
DIFF="/usr/bin/diff --brief"

function performUnitTest() {
    LOGFILE=$1
    TESTNAME=$2
    TESTRESULT="PASS"
    cat $INPUT_DIRECTORY/$LOGFILE | ./parse_apache_log.pl > $ACTUAL_DIRECTORY/$LOGFILE.pl
    $DIFF $EXPECTATION_DIRECTORY/$LOGFILE.pl $ACTUAL_DIRECTORY/$LOGFILE.pl
    if [ "$?" != "0" ]; then
	TESTRESULT="FAIL"
    fi
    cat $INPUT_DIRECTORY/$LOGFILE | ./parse_apache_log.py > $ACTUAL_DIRECTORY/$LOGFILE.py
    $DIFF $EXPECTATION_DIRECTORY/$LOGFILE.py $ACTUAL_DIRECTORY/$LOGFILE.py
    if [ "$?" != "0" ]; then
	TESTRESULT="FAIL"
    fi
    echo "Test [$TESTNAME] Result: $TESTRESULT"
}

LOGFILE=log.create
performUnitTest $LOGFILE "CREATE"

LOGFILE=log.delete
performUnitTest $LOGFILE "DELETE"

LOGFILE=log.failure_stat
performUnitTest $LOGFILE "FAILURE_STAT"

LOGFILE=log.find_instance
performUnitTest $LOGFILE "FIND_INSTANCE"

LOGFILE=log.stat
performUnitTest $LOGFILE "STAT"

LOGFILE=log.unknown
performUnitTest $LOGFILE "Nebulous (unknown)"

LOGFILE=log.non_nebulous
performUnitTest $LOGFILE "Non Nebulous"
