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