Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *             National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author: tao $'
7
 *     '$Date: 2015-02-06 16:47:25 -0800 (Fri, 06 Feb 2015) $'
8
 * '$Revision: 9108 $'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 */
24
package edu.ucsb.nceas.metacattest;
25

    
26
import java.sql.Timestamp;
27
import java.text.ParseException;
28
import java.text.SimpleDateFormat;
29

    
30
import edu.ucsb.nceas.MCTestCase;
31
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
32
import edu.ucsb.nceas.metacat.EventLog;
33

    
34
/**
35
 * Test the logging facility against the database connection.
36
 * 
37
 * @author jones
38
 */
39
public class EventLogTest extends MCTestCase
40
{
41
    private static final String USERAGENT="useragent-useragent-useragent-useragent-useragent-useragent-useragent-useragent-useragent-useragent-"+
42
                                    "useragent-useragent-useragent-useragent-useragent-useragent-useragent-useragent-useragent-useragent-"+
43
                                    "useragent-useragent-useragent-useragent-useragent-useragent-useragent-useragent-useragent-useragent-"+
44
                                    "useragent-useragent-useragent-useragent-useragent-useragent-useragent-useragent-useragent-useragent-"+
45
                                    "useragent-useragent-useragent-useragent-useragent-useragent-useragent-useragent-useragent-useragent-"+
46
                                    "useragent-12";
47
    protected void setUp() throws Exception
48
    {    	
49
        super.setUp();
50
        DBConnectionPool pool = DBConnectionPool.getInstance();
51
    }
52

    
53
    /**
54
     * Test whether a valid instance of the EventLog can be retrieved.
55
     *
56
     */
57
    public void testGetInstance()
58
    {
59
        EventLog logger = EventLog.getInstance();
60
        assertTrue(logger != null);
61
    }
62

    
63
    /**
64
     * Test whether the log method can properly insert a log record.
65
     */
66
    public void testLog() throws Exception
67
    {
68
        
69
        long time = System.nanoTime();
70
        String id = "test-1934-wemewen-3-2"+time+".1";
71
        EventLog.getInstance().log("192.168.1.103", "Mozilla", "public", id, "read");
72
        Thread.sleep(2000);
73
        Timestamp startDate = null;
74
        Timestamp endDate = null;
75
        boolean anonymous = false;
76
        String[] principals = {"public", "someone"};
77
        String[] ipList = {"192.168.1.103", "192.168.1.104"};
78
        String[] docList = {id};
79
        String[] eventList = {"read", "insert", "update"};
80
        String report = EventLog.getInstance().getReport(ipList, principals, docList, 
81
                eventList, startDate, endDate, anonymous);
82
        //System.out.println("the report is "+report);
83
        assertTrue(report.contains("<event>read</event>"));
84
        assertTrue(report.contains("<ipAddress>192.168.1.103</ipAddress>"));
85
        assertTrue(report.contains("<userAgent>Mozilla</userAgent>"));
86
        assertTrue(report.contains("<principal>public</principal>"));
87
        assertTrue(report.contains("<docid>"+id+"</docid>"));
88
        
89
        //test a case with a user-agent which length is greater than 512.
90
        time = System.nanoTime();
91
        id = "test-1934-wemewen-3-3"+time+".1";
92
        EventLog.getInstance().log("192.168.1.103", USERAGENT+"extral characters", "public", id, "read");
93
        Thread.sleep(2000);
94
   
95
        String[] docs = {id};
96
        report = EventLog.getInstance().getReport(ipList, principals, docs, 
97
                eventList, startDate, endDate, anonymous);
98
        System.out.println("the report is "+report);
99
        assertTrue(report.contains("<event>read</event>"));
100
        assertTrue(report.contains("<ipAddress>192.168.1.103</ipAddress>"));
101
        assertTrue(report.contains("<userAgent>"+USERAGENT+"</userAgent>"));
102
        assertTrue(report.contains("<principal>public</principal>"));
103
        assertTrue(report.contains("<docid>"+id+"</docid>"));
104
    }
105

    
106
    /**
107
     * Test whether getReport returns correct reports.
108
     */
109
    public void testGetReport()
110
    {
111
        String[] principals = {"jones", "public", "someone"};
112
        String[] ipList = {"192.168.1.103", "192.168.1.104"};
113
        String[] docList = {"test.2.1", "test.2"};
114
        String[] eventList = {"read", "insert", "update"};
115
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
116
        Timestamp startDate = null;
117
        Timestamp endDate = null;
118
        try {
119
            startDate = new Timestamp((format.parse("2004-04-08 02:32:00")).getTime());
120
            endDate = new Timestamp((format.parse("2004-04-08 11:20:00")).getTime());
121
        } catch (ParseException e) {
122
            System.out.println("Failed to created endDate from format.");
123
        }
124
        String report = EventLog.getInstance().getReport(ipList, principals, docList, 
125
                        eventList, null, null, false);
126
        System.out.println(report);
127
        report = EventLog.getInstance().getReport(null, null, null, 
128
                        null, startDate, endDate, false);
129
        System.out.println(report);
130
    }
131
    
132
    /**
133
     * Test if the isDeleted method
134
     */
135
    public void testIsDeleted() throws Exception{
136
        long time = System.nanoTime();
137
        String id = "test-1934-weme123-3-1"+time+".1";
138
        EventLog.getInstance().log("192.168.1.103", "Mozilla", "public", id, "read");
139
        Thread.sleep(2000);
140
        boolean deleted = EventLog.getInstance().isDeleted(id);
141
        assertTrue(deleted == false);
142
        Thread.sleep(2000);
143
        EventLog.getInstance().log("192.168.1.103", "Mozilla", "public", id, EventLog.DELETE);
144
        deleted = EventLog.getInstance().isDeleted(id);
145
        assertTrue(deleted == true);
146
    }
147
}
(6-6/30)