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