-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathXCOMTest.java
184 lines (157 loc) · 7.94 KB
/
XCOMTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Copyright (C) 2022 Vaticle
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.vaticle.typedb.example.gaming.xcom;
import com.vaticle.typedb.driver.TypeDB;
import com.vaticle.typedb.driver.api.TypeDBDriver;
import com.vaticle.typedb.driver.api.TypeDBSession;
import com.vaticle.typedb.driver.api.TypeDBTransaction;
import com.vaticle.typeql.lang.TypeQL;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class XCOMTest {
TypeDBDriver driver;
TypeDBSession session;
String databaseName = "xcom_test";
@Before
public void migrateDatabase() throws IOException {
Migration.main(new String[]{databaseName});
driver = TypeDB.coreDriver("localhost:1729");
session = driver.session(databaseName, TypeDBSession.Type.DATA);
}
@After
public void deleteDatabase() {
session.close();
driver.close();
}
@Test
public void testMigration() {
assertMigrationResults();
}
@Test
public void testQueries() {
Queries.databaseName = databaseName;
// Start a new campaign, named Gatecrasher
Queries.askQuestions(new String[] { String.valueOf(Queries.START_NEW_CAMPAIGN), "Gatecrasher", "0" });
final String gatecrasher = "1";
// Fetch available research projects in Gatecrasher
Queries.askQuestions(new String[] { String.valueOf(Queries.GET_AVAILABLE_RESEARCH), gatecrasher, "0" });
assertEquals(1, Queries.answers.size());
List<ResearchTask> researchTasks = (List<ResearchTask>) Queries.answers.get(0).getEntity();
// Check that we have exactly 5 research projects available
assertEquals(5, researchTasks.size());
ResearchTask alienBiotech = null;
int alienBiotechKey = -1;
for (int key = 1; key <= researchTasks.size(); key++) {
ResearchTask rt = researchTasks.get(key - 1);
if (rt.name.equals("Alien Biotech")) {
alienBiotech = rt;
alienBiotechKey = key;
}
}
// Check that Alien Biotech is available to research
assertNotNull(alienBiotech);
assertNotEquals(-1, alienBiotechKey);
// View Gatecrasher's inventory
Queries.askQuestions(new String[] { String.valueOf(Queries.VIEW_INVENTORY), gatecrasher, "0" });
assertEquals(2, Queries.answers.size());
List<InventoryItem> inventory = (List<InventoryItem>) Queries.answers.get(1).getEntity();
// Check that the inventory is empty
assertEquals(0, inventory.size());
// Research Alien Biotech
Queries.askQuestions(new String[] { String.valueOf(Queries.ADVANCE_RESEARCH), gatecrasher, String.valueOf(alienBiotechKey), "0" });
// Fetch the research projects that are now available
Queries.askQuestions(new String[] { String.valueOf(Queries.GET_AVAILABLE_RESEARCH), gatecrasher, "0" });
assertEquals(3, Queries.answers.size());
researchTasks = (List<ResearchTask>) Queries.answers.get(2).getEntity();
// Check that, after researching Alien Biotech, we have only 4 research projects available
assertEquals(4, researchTasks.size());
// Fetch all items
Queries.askQuestions(new String[] { String.valueOf(Queries.LIST_ALL_ITEMS), "0" });
assertEquals(4, Queries.answers.size());
List<String> items = (List<String>) Queries.answers.get(3).getEntity();
int sectoidCorpseKey = -1;
for (int key = 1; key <= items.size(); key++) {
String item = items.get(key - 1);
if (item.equals("Sectoid Corpse")) {
sectoidCorpseKey = key;
}
}
assertNotEquals(-1, sectoidCorpseKey);
// Acquire one Sectoid Corpse
Queries.askQuestions(new String[] { String.valueOf(Queries.ACQUIRE_ITEM), gatecrasher, "Sectoid Corpse", "1", "0" });
// Fetch the research projects that are now available
Queries.askQuestions(new String[] { String.valueOf(Queries.GET_AVAILABLE_RESEARCH), gatecrasher, "0" });
assertEquals(5, Queries.answers.size());
researchTasks = (List<ResearchTask>) Queries.answers.get(4).getEntity();
// Check that, after acquiring a Sectoid Corpse, a new research project has been unlocked
assertEquals(5, researchTasks.size());
ResearchTask sectoidAutopsy = null;
for (int key = 1; key <= researchTasks.size(); key++) {
ResearchTask rt = researchTasks.get(key - 1);
if (rt.name.equals("Sectoid Autopsy")) {
sectoidAutopsy = rt;
}
}
// Check that the newly unlocked research project is Sectoid Autopsy
assertNotNull(sectoidAutopsy);
// Fetch all techs
Queries.askQuestions(new String[] { String.valueOf(Queries.LIST_ALL_TECHS), "0" });
assertEquals(6, Queries.answers.size());
List<String> techs = (List<String>) Queries.answers.get(5).getEntity();
int gaussWeaponsKey = -1;
for (int key = 1; key <= techs.size(); key++) {
String tech = techs.get(key - 1);
if (tech.equals("Gauss Weapons")) {
gaussWeaponsKey = key;
}
}
// Check that Gauss Weapons is a tech
assertNotEquals(-1, gaussWeaponsKey);
// Fetch the tree of prerequisite techs that must be researched in order to unlock Gauss Weapons
Queries.askQuestions(new String[] { String.valueOf(Queries.COMPUTE_TECH_REQUIREMENTS), String.valueOf(gaussWeaponsKey), "0" });
assertEquals(7, Queries.answers.size());
Set<String> techRequirements = (Set<String>) Queries.answers.get(6).getEntity();
// Check that the prerequisites are [Modular Weapons, Magnetic Weapons]
assertEquals(2, techRequirements.size());
assertTrue(techRequirements.contains("Modular Weapons"));
assertTrue(techRequirements.contains("Magnetic Weapons"));
}
public void assertMigrationResults() {
final TypeDBTransaction transaction = session.transaction(TypeDBTransaction.Type.READ);
final long totalTechs = transaction.query().get(TypeQL.parseQuery("match $x isa research-project; get $x; count;").asGetAggregate()).resolve().get().asLong();
assertEquals(46, totalTechs);
final long totalItems = transaction.query().get(TypeQL.parseQuery("match $x isa item; get $x; count;").asGetAggregate()).resolve().get().asLong();
assertEquals(34, totalItems);
final long totalResearchTechRequirements = transaction.query().get(TypeQL.parseQuery("match $x isa tech-requirement-to-begin-research; get $x; count;").asGetAggregate()).resolve().get().asLong();
assertEquals(36, totalResearchTechRequirements);
final long totalResearchResourceCosts = transaction.query().get(TypeQL.parseQuery("match $x isa resource-cost-to-begin-research; get $x; count;").asGetAggregate()).resolve().get().asLong();
assertEquals(44, totalResearchResourceCosts);
transaction.close();
}
}