Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shamseen committed Jul 22, 2017
1 parent b381aa4 commit c52764b
Show file tree
Hide file tree
Showing 5 changed files with 422 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ private static URI soMapping(String s) {
}

// Update Lookup Table
_logicLayer.AddOrUpdateSO(retVal, s);
_logicLayer.AddOrUpdateSO(retVal.toString(), s);
return retVal;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
import org.bson.Document;

/**
Expand All @@ -24,24 +27,28 @@ public MongoCollection getCollection(String colName) {
return collection;
}

public MongoIterable<String> getCollectionNames() {
// Document retVal = db.runCommand(new Document("listCollections", 1));
MongoIterable<String> retVal = db.listCollectionNames();

return retVal;
}

public void login(String user, String pw) throws Exception {
if (user == null || pw == null) {
}
// To connect to mongodb server
String uri = String.format("mongodb://%s:%[email protected]:53412/lookuptables", user, pw);

MongoCredential mongoCredential = MongoCredential.createScramSha1Credential("admin", "admin",
"admin123".toCharArray());
MongoClientURI connectionString = new MongoClientURI(uri);
client = new MongoClient(connectionString);
db = client.getDatabase("lookuptables");
System.out.println("Connected to database successfully!");
//boolean auth = db.authenticate(myUserName, myPassword);
//System.out.println("Authentication: "+auth);

try {

// To connect to mongodb server
String uri = String.format("mongodb://%s:%[email protected]:53412/lookuptables", user, pw);
MongoClientURI connectionString = new MongoClientURI(uri);
client = new MongoClient(connectionString);
db = client.getDatabase("lookuptables");
System.out.println("Connect to database successfully");
//boolean auth = db.authenticate(myUserName, myPassword);
//System.out.println("Authentication: "+auth);

} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}

public void logout() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class SBOLConversionDataLayer {
private Document doc;

public SBOLConversionDataLayer() {
setUp();
}

public ArrayList<String> getEugenePartNames(String so) {
Expand All @@ -43,42 +44,51 @@ public ArrayList<String> getEugenePartNames(String so) {
return names;
}

public boolean insertEugenePartName(String so, String name) {
public void UpsertEugenePartName(String so, String name) {
if (findDocument(so)) {
updateSOTerm(so, name, true);
return;
}

Document d = new Document("sequenceOntologyNumber", so)
.append("eugenePartNames", Arrays.asList(name));

collection.insertOne(d);

return true;
}

public boolean removeEugenePartName(String so, String name) {
boolean retVal = true;

if (findDocument(so)) {
updateSOTerm(so, name, false);
}

return retVal;
}

public boolean updateSOTerm(String so, String name) {
private void updateSOTerm(String so, String name, boolean addName) {
ArrayList<String> names = (ArrayList) doc.get("eugenePartNames");

names.add(name);
if (addName == names.contains(name)) {
return;
}

doc.put("eugenePartNames", names);
if (addName) {
names.add(name);
} else {
names.remove(name);
}

// TO DO: Don't replace whole doc, just update it.
collection.updateOne(
eq("sequenceOntologyNumber", so),
doc);
return true;
collection.updateOne(eq("sequenceOntologyNumber", so),
new Document("$set", new Document("eugenePartNames", names)));
}

private boolean findDocument(String so) {
boolean retVal = true;

doc = (Document) collection.find(eq("sequenceOntologyNumber", so)).first();

//if doc something
boolean retVal = doc != null;

return retVal;
}

Expand All @@ -87,7 +97,7 @@ private void setUp() {
_instance = new MongoConnection();

try {
_instance.login("shamseen@bu.edu", "lcp");
_instance.login("shamseen", "lcp");
collection = _instance.getCollection("SBOL2Conversions");

} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,50 @@ public class SBOLConversionLogicLayer {
private SBOLConversionDataLayer _dataLayer;
private List<String> names;
private int uriIndex;
private String _so;

// TO DO: Error handling
public SBOLConversionLogicLayer() {
_dataLayer = new SBOLConversionDataLayer();
uriIndex = SequenceOntology.NAMESPACE.toString().length();
String uri = SequenceOntology.NAMESPACE.toString() + "SO_";
uriIndex = uri.length();
}

public void AddOrUpdateSO(URI so, String name) {
insertOrUpdate(so, name);
public void AddOrUpdateSO(String so, String name) {
takeSONumberFromURI(so);
insertOrUpdate(name);
}

public boolean DeleteEugenePartName(String so, String name) {
return deleteEugenePartName(so, name);
takeSONumberFromURI(so);
return deleteEugenePartName(name);
}

public List<String> GetEugeneNames(String so) {
getEugeneNames(so);
takeSONumberFromURI(so);
getEugeneNames();
return names;
}

private boolean deleteEugenePartName(String so, String name) {
boolean retVal = _dataLayer.removeEugenePartName(so, name);
private boolean deleteEugenePartName(String name) {
boolean retVal = _dataLayer.removeEugenePartName(_so, name);
return retVal;
}

private void getEugeneNames(String so) {
names = _dataLayer.getEugenePartNames(so);
private void getEugeneNames() {
names = _dataLayer.getEugenePartNames(_so);
}

private void insertOrUpdate(URI _so, String name) {
String so = _so.toString().substring(uriIndex);
private void insertOrUpdate(String name) {
_dataLayer.UpsertEugenePartName(_so, name);
}

getEugeneNames(so);
if (names.isEmpty()) {
_dataLayer.insertEugenePartName(so, name);
private void takeSONumberFromURI(String so) {
if (so.length() > uriIndex) {
so = so.substring(uriIndex);
}

_so = so;
}

}
Loading

0 comments on commit c52764b

Please sign in to comment.