View Javadoc
1   package emissary.directory;
2   
3   import emissary.core.Form;
4   import emissary.test.core.junit5.UnitTest;
5   import emissary.util.xml.JDOMUtil;
6   
7   import jakarta.annotation.Nullable;
8   import org.jdom2.Document;
9   import org.jdom2.JDOMException;
10  import org.junit.jupiter.api.AfterEach;
11  import org.junit.jupiter.api.BeforeEach;
12  import org.junit.jupiter.api.Test;
13  
14  import static org.junit.jupiter.api.Assertions.assertEquals;
15  import static org.junit.jupiter.api.Assertions.assertFalse;
16  import static org.junit.jupiter.api.Assertions.assertNotNull;
17  import static org.junit.jupiter.api.Assertions.assertNull;
18  import static org.junit.jupiter.api.Assertions.assertTrue;
19  
20  class DirectoryEntryTest extends UnitTest {
21  
22      private static final String key = "UNKNOWN.FOOPLACE.ID.http://host.domain.com:8001/ThePlace";
23      private static final int cost = 50;
24      private static final int quality = 50;
25  
26      @Nullable
27      private DirectoryEntry d = null;
28      @Nullable
29      private DirectoryEntry d2 = null;
30  
31      @Override
32      @BeforeEach
33      public void setUp() throws Exception {
34          this.d = new DirectoryEntry(key, "This is a place", cost, quality);
35          this.d2 = new DirectoryEntry(key, "Another place", cost * 2, quality);
36      }
37  
38      @Override
39      @AfterEach
40      public void tearDown() throws Exception {
41          super.tearDown();
42          this.d = null;
43          this.d2 = null;
44      }
45  
46      @Test
47      void testConstructor() {
48          assertEquals(cost, this.d.getCost(), "Cost set via ctor");
49          assertEquals(quality, this.d.getQuality(), "Quality set via ctor");
50          assertEquals(key, this.d.getKey(), "Key set via ctor");
51          assertEquals("This is a place", this.d.getDescription(), "Description via ctor");
52      }
53  
54      @Test
55      void testParsing() {
56          assertEquals("ID", this.d.getServiceType(), "Service type");
57          assertEquals("FOOPLACE", this.d.getServiceName(), "Service name");
58          assertEquals(5050, this.d.getExpense(), "Calculate expense");
59          assertEquals("UNKNOWN::ID", this.d.getDataId(), "DataId");
60          assertEquals(Form.UNKNOWN, this.d.getDataType(), "Data Type");
61          assertEquals("http://host.domain.com:8001/", this.d.getServiceHostUrl(), "ServiceHostURL");
62          assertNull(this.d.getLocalPlace(), "Local place");
63      }
64  
65      @Test
66      void testReverseCostComp() {
67          final DirectoryEntry n = new DirectoryEntry(key);
68          assertEquals(0, n.getCost(), "Computed cost");
69          assertEquals(100, n.getQuality(), "Computed quality");
70          assertEquals(0, n.getExpense(), "Computed expense");
71  
72          final DirectoryEntry n2 = new DirectoryEntry(key + "$5050");
73          assertEquals(cost, n2.getCost(), "Computed cost");
74          assertEquals(quality, n2.getQuality(), "Computed quality");
75          assertEquals(5050, n2.getExpense(), "Computed expense");
76      }
77  
78      @Test
79      void testBetter() {
80          assertTrue(this.d.getExpense() < this.d2.getExpense(), "External expense comparison");
81          assertTrue(this.d.isBetterThan(this.d2), "Cheaper is better");
82          assertFalse(this.d2.isBetterThan(this.d), "More expensive is not better");
83      }
84  
85      @Test
86      void testAdd() {
87          this.d.addCost(100);
88          assertEquals(100 + cost, this.d.getCost(), "Cost increment");
89          final int newExp = DirectoryEntry.calculateExpense(cost + 100, quality);
90          assertEquals(newExp, this.d.getExpense(), "Expense computation on cost increment");
91          assertTrue(this.d.toString().contains("$" + newExp), "Correct expense in key");
92      }
93  
94      @Test
95      void testXmlToObject() throws JDOMException {
96          final int exp = DirectoryEntry.calculateExpense(cost, quality);
97          final String xml =
98                  "<entry><key>" + key + "</key><cost>" + cost + "</cost><quality>" + quality
99                          + "</quality><description>This is the description</description><expense>" + exp + "</expense><place>" + key
100                         + "</place></entry>";
101         final Document jdom = JDOMUtil.createDocument(xml, false);
102         final DirectoryEntry dxml = DirectoryEntry.fromXml(jdom.getRootElement());
103         assertNotNull(dxml, "DirectoryEntry created from xml");
104         assertEquals(cost, dxml.getCost(), "Cost from xml");
105         assertEquals(quality, dxml.getQuality(), "Quality from xml");
106         assertEquals(exp, dxml.getExpense(), "Expense from xml");
107         assertEquals("This is the description", dxml.getDescription(), "Description from xml");
108         assertEquals(key, dxml.getKey(), "Key from xml");
109         assertEquals(key + KeyManipulator.DOLLAR + exp, dxml.getFullKey(), "Full key from xml");
110     }
111 
112     @Test
113     void testEquality() {
114         assertFalse(this.d.matches(KeyManipulator.addExpense(this.d2.getKey(), this.d2.getExpense())), "Entry equality");
115         assertFalse(this.d2.matches(KeyManipulator.addExpense(this.d.getKey(), this.d.getExpense())), "Entry equality");
116         assertTrue(this.d.matchesIgnoreCost(KeyManipulator.addExpense(this.d2.getKey(), this.d2.getExpense())), "Entry equality without cost");
117         assertTrue(this.d2.matchesIgnoreCost(KeyManipulator.addExpense(this.d.getKey(), this.d.getExpense())), "Entry equality without cost");
118     }
119 
120     @Test
121     void testProxySettings() {
122         String proxyKey = "*.*.*.http://host2.domain.com:9001/OtherPlace";
123         this.d.proxyFor(proxyKey);
124         assertEquals(cost, this.d.getCost(), "Proxy cost");
125         assertEquals(quality, this.d.getQuality(), "Proxy qual");
126         assertEquals(KeyManipulator.getServiceType(key), this.d.getServiceType(), "Proxy service type");
127         assertEquals(KeyManipulator.getServiceName(key), this.d.getServiceName(), "Proxy service name");
128         assertEquals(KeyManipulator.getDataType(key), KeyManipulator.getDataType(this.d.getKey()), "Proxy data type");
129         assertEquals(DirectoryEntry.calculateExpense(cost, quality), this.d.getExpense(), "Proxy expense");
130 
131         // Proxy key cost does not change it
132         proxyKey = "*.*.*.http://host2.domain.com:9001/OtherPlace$12345";
133         this.d.proxyFor(proxyKey);
134         assertEquals(cost, this.d.getCost(), "Proxy cost");
135         assertEquals(quality, this.d.getQuality(), "Proxy qual");
136         assertEquals(KeyManipulator.getServiceType(key), this.d.getServiceType(), "Proxy service type");
137         assertEquals(KeyManipulator.getServiceName(key), this.d.getServiceName(), "Proxy service name");
138         assertEquals(KeyManipulator.getDataType(key), KeyManipulator.getDataType(this.d.getKey()), "Proxy data type");
139         assertEquals(DirectoryEntry.calculateExpense(cost, quality), this.d.getExpense(), "Proxy expense");
140     }
141 
142     @Test
143     void testCopyConstructor() {
144         final DirectoryEntry copy = new DirectoryEntry(this.d);
145         assertEquals(key, copy.getKey(), "Copied key");
146         assertEquals(cost, copy.getCost(), "Copied cost");
147         assertEquals(quality, copy.getQuality(), "Copied qual");
148         assertEquals(this.d.getExpense(), copy.getExpense(), "Copied expense");
149         assertEquals(this.d.getDescription(), copy.getDescription(), "Copied desc");
150         assertEquals(this.d.toString(), copy.toString(), "Copied tostring");
151         assertEquals(this.d.getFullKey(), copy.getFullKey(), "Full key");
152     }
153 
154     @Test
155     void testUpdateMethods() {
156         this.d.setDataType("KNOWN");
157         assertEquals("KNOWN", KeyManipulator.getDataType(this.d.getFullKey()), "update key with new data type");
158         assertEquals("KNOWN::ID", this.d.getDataId(), "update dataid with new data type");
159 
160         this.d.setServiceType("DI");
161         assertEquals("DI", KeyManipulator.getServiceType(this.d.getFullKey()), "update key with new service type");
162         assertEquals("KNOWN::DI", this.d.getDataId(), "update dataid with new service type");
163 
164         this.d.setServiceName("BARPLACE");
165         assertEquals("BARPLACE", KeyManipulator.getServiceName(this.d.getFullKey()), "update key with new service name");
166         assertEquals("KNOWN::DI", this.d.getDataId(), "no change to dataid with new service name");
167     }
168 
169     @Test
170     void testAge() {
171         final DirectoryEntry x1 = new DirectoryEntry(key, "Another place", cost * 2, quality);
172         pause(50);
173         final DirectoryEntry x2 = new DirectoryEntry(key, "Another fine place", cost * 2, quality);
174         assertTrue(x1.getAge() < x2.getAge(), "Age must vary by creation time");
175     }
176 
177     @Test
178     void testAgeOnCopy() {
179         final DirectoryEntry x1 = new DirectoryEntry(key, "Another place", cost * 2, quality);
180         pause(50);
181         final DirectoryEntry x2 = new DirectoryEntry(x1);
182         assertTrue(x1.getAge() < x2.getAge(), "Age must vary by creation time on a copy");
183     }
184 }