View Javadoc
1   /*
2    * This file is part of dependency-check-core.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.data.nvdcve;
19  
20  import java.text.DateFormat;
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  import org.owasp.dependencycheck.BaseDBTestCase;
24  import java.util.Properties;
25  import org.joda.time.DateTime;
26  import org.joda.time.format.DateTimeFormat;
27  import org.joda.time.format.DateTimeFormatter;
28  import static org.junit.Assert.assertEquals;
29  import static org.junit.Assert.assertTrue;
30  import org.junit.Test;
31  import org.owasp.dependencycheck.data.update.nvd.NvdCveInfo;
32  
33  /**
34   *
35   * @author Jeremy Long
36   */
37  public class DatabasePropertiesIntegrationTest extends BaseDBTestCase {
38  
39      /**
40       * Test of isEmpty method, of class DatabaseProperties.
41       */
42      @Test
43      public void testIsEmpty() throws Exception {
44          CveDB cveDB = null;
45          try {
46              cveDB = new CveDB();
47              cveDB.open();
48              DatabaseProperties instance = cveDB.getDatabaseProperties();
49              boolean expResult = false;
50              boolean result = instance.isEmpty();
51              //no exception means the call worked... whether or not it is empty depends on if the db is new
52              //assertEquals(expResult, result);
53          } finally {
54              if (cveDB != null) {
55                  cveDB.close();
56              }
57          }
58      }
59  
60      /**
61       * Test of save method, of class DatabaseProperties.
62       */
63      @Test
64      public void testSave() throws Exception {
65          NvdCveInfo updatedValue = new NvdCveInfo();
66          String key = "test";
67          long expected = 1337;
68          updatedValue.setId(key);
69          updatedValue.setTimestamp(expected);
70          CveDB cveDB = null;
71          try {
72              cveDB = new CveDB();
73              cveDB.open();
74              DatabaseProperties instance = cveDB.getDatabaseProperties();
75              instance.save(updatedValue);
76              //reload the properties
77              cveDB.close();
78              cveDB = new CveDB();
79              cveDB.open();
80              instance = cveDB.getDatabaseProperties();
81              long results = Long.parseLong(instance.getProperty("NVD CVE " + key));
82              assertEquals(expected, results);
83          } finally {
84              if (cveDB != null) {
85                  cveDB.close();
86              }
87          }
88      }
89  
90      /**
91       * Test of getProperty method, of class DatabaseProperties.
92       */
93      @Test
94      public void testGetProperty_String_String() throws Exception {
95          String key = "doesn't exist";
96          String defaultValue = "default";
97          CveDB cveDB = null;
98          try {
99              cveDB = new CveDB();
100             cveDB.open();
101             DatabaseProperties instance = cveDB.getDatabaseProperties();
102             String expResult = "default";
103             String result = instance.getProperty(key, defaultValue);
104             assertEquals(expResult, result);
105         } finally {
106             if (cveDB != null) {
107                 cveDB.close();
108             }
109         }
110     }
111 
112     /**
113      * Test of getProperty method, of class DatabaseProperties.
114      */
115     @Test
116     public void testGetProperty_String() throws DatabaseException {
117         String key = "version";
118         CveDB cveDB = null;
119         try {
120             cveDB = new CveDB();
121             cveDB.open();
122             DatabaseProperties instance = cveDB.getDatabaseProperties();
123             String result = instance.getProperty(key);
124             double version = Double.parseDouble(result);
125             assertTrue(version >= 2.8);
126             assertTrue(version <= 10);
127         } finally {
128             if (cveDB != null) {
129                 cveDB.close();
130             }
131         }
132     }
133 
134     /**
135      * Test of getProperties method, of class DatabaseProperties.
136      */
137     @Test
138     public void testGetProperties() throws DatabaseException {
139         CveDB cveDB = null;
140         try {
141             cveDB = new CveDB();
142             cveDB.open();
143             DatabaseProperties instance = cveDB.getDatabaseProperties();
144             Properties result = instance.getProperties();
145             assertTrue(result.size() > 0);
146         } finally {
147             if (cveDB != null) {
148                 cveDB.close();
149             }
150         }
151     }
152 }