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.util.Properties;
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  import org.junit.Test;
24  import org.owasp.dependencycheck.data.update.nvd.NvdCveInfo;
25  
26  /**
27   *
28   * @author Jeremy Long
29   */
30  public class DatabasePropertiesIntegrationTest extends BaseDBTestCase {
31  
32      /**
33       * Test of isEmpty method, of class DatabaseProperties.
34       */
35      @Test
36      public void testIsEmpty() throws Exception {
37          CveDB cveDB = null;
38          try {
39              cveDB = new CveDB();
40              cveDB.open();
41              DatabaseProperties instance = cveDB.getDatabaseProperties();
42              boolean expResult = false;
43              boolean result = instance.isEmpty();
44              //no exception means the call worked... whether or not it is empty depends on if the db is new
45              //assertEquals(expResult, result);
46          } finally {
47              if (cveDB != null) {
48                  cveDB.close();
49              }
50          }
51      }
52  
53      /**
54       * Test of save method, of class DatabaseProperties.
55       */
56      @Test
57      public void testSave() throws Exception {
58          NvdCveInfo updatedValue = new NvdCveInfo();
59          String key = "test";
60          long expected = 1337;
61          updatedValue.setId(key);
62          updatedValue.setTimestamp(expected);
63          CveDB cveDB = null;
64          try {
65              cveDB = new CveDB();
66              cveDB.open();
67              DatabaseProperties instance = cveDB.getDatabaseProperties();
68              instance.save(updatedValue);
69              //reload the properties
70              cveDB.close();
71              cveDB = new CveDB();
72              cveDB.open();
73              instance = cveDB.getDatabaseProperties();
74              long results = Long.parseLong(instance.getProperty("NVD CVE " + key));
75              assertEquals(expected, results);
76          } finally {
77              if (cveDB != null) {
78                  cveDB.close();
79              }
80          }
81      }
82  
83      /**
84       * Test of getProperty method, of class DatabaseProperties.
85       */
86      @Test
87      public void testGetProperty_String_String() throws Exception {
88          String key = "doesn't exist";
89          String defaultValue = "default";
90          CveDB cveDB = null;
91          try {
92              cveDB = new CveDB();
93              cveDB.open();
94              DatabaseProperties instance = cveDB.getDatabaseProperties();
95              String expResult = "default";
96              String result = instance.getProperty(key, defaultValue);
97              assertEquals(expResult, result);
98          } finally {
99              if (cveDB != null) {
100                 cveDB.close();
101             }
102         }
103     }
104 
105     /**
106      * Test of getProperty method, of class DatabaseProperties.
107      */
108     @Test
109     public void testGetProperty_String() throws DatabaseException {
110         String key = "version";
111         CveDB cveDB = null;
112         try {
113             cveDB = new CveDB();
114             cveDB.open();
115             DatabaseProperties instance = cveDB.getDatabaseProperties();
116             String result = instance.getProperty(key);
117             double version = Double.parseDouble(result);
118             assertTrue(version >= 2.8);
119             assertTrue(version <= 10);
120         } finally {
121             if (cveDB != null) {
122                 cveDB.close();
123             }
124         }
125     }
126 
127     /**
128      * Test of getProperties method, of class DatabaseProperties.
129      */
130     @Test
131     public void testGetProperties() throws DatabaseException {
132         CveDB cveDB = null;
133         try {
134             cveDB = new CveDB();
135             cveDB.open();
136             DatabaseProperties instance = cveDB.getDatabaseProperties();
137             Properties result = instance.getProperties();
138             assertTrue(result.size() > 0);
139         } finally {
140             if (cveDB != null) {
141                 cveDB.close();
142             }
143         }
144     }
145 }