|
| 1 | +package edu.ucsd.sbrg; |
| 2 | + |
| 3 | +import de.zbit.util.prefs.SBProperties; |
| 4 | +import edu.ucsd.sbrg.bigg.IOOptions; |
| 5 | +import edu.ucsd.sbrg.bigg.ModelPolisherOptions; |
| 6 | +import edu.ucsd.sbrg.bigg.Parameters; |
| 7 | +import org.sbml.jsbml.AbstractNamedSBase; |
| 8 | +import org.sbml.jsbml.CVTerm; |
| 9 | + |
| 10 | +import java.lang.reflect.InvocationTargetException; |
| 11 | +import java.util.Collection; |
| 12 | +import java.util.HashSet; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.stream.Collectors; |
| 15 | +import java.util.stream.Stream; |
| 16 | + |
| 17 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 18 | + |
| 19 | +public class TestUtils { |
| 20 | + |
| 21 | + public static void initParameters() { |
| 22 | + initParameters(Map.of()); |
| 23 | + } |
| 24 | + |
| 25 | + public static void initParameters(Map<String, String> params) { |
| 26 | + var props = new SBProperties(); |
| 27 | + props.setProperty(IOOptions.INPUT.getOptionName(), |
| 28 | + "bla"); |
| 29 | + props.setProperty(IOOptions.OUTPUT.getOptionName(), |
| 30 | + "bla"); |
| 31 | + props.setProperty(ModelPolisherOptions.COMPRESSION_TYPE.getOptionName(), |
| 32 | + ModelPolisherOptions.Compression.NONE.name()); |
| 33 | + props.setProperty(ModelPolisherOptions.DOCUMENT_TITLE_PATTERN.getOptionName(), |
| 34 | + ""); |
| 35 | + |
| 36 | + for (var pair : params.entrySet()) { |
| 37 | + props.setProperty(pair.getKey(), pair.getValue()); |
| 38 | + } |
| 39 | + try { |
| 40 | + var parameters = Parameters.class.getDeclaredField("parameters"); |
| 41 | + parameters.setAccessible(true); |
| 42 | + parameters.set(null, null); |
| 43 | + var init = Parameters.class.getDeclaredMethod("init", SBProperties.class); |
| 44 | + init.setAccessible(true); |
| 45 | + init.invoke(null, props); |
| 46 | + } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException | NoSuchFieldException e) { |
| 47 | + e.printStackTrace(); |
| 48 | + assertTrue(false, "Reflection on parameters failed. Sorry"); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + public static void assertCVTermIsPresent(AbstractNamedSBase entity, |
| 54 | + CVTerm.Type t, |
| 55 | + CVTerm.Qualifier q) { |
| 56 | + assertCVTermIsPresent(entity, t, q, null, "CVTerm of type " + t.toString() |
| 57 | + + ", " + q.toString() + " not present on entity " |
| 58 | + + entity.getId()); |
| 59 | + } |
| 60 | + |
| 61 | + public static void assertCVTermIsPresent(AbstractNamedSBase entity, |
| 62 | + CVTerm.Type t, |
| 63 | + CVTerm.Qualifier q, |
| 64 | + String uri) { |
| 65 | + assertCVTermIsPresent(entity, t, q, uri, "CVTerm of type " + t.toString() |
| 66 | + + ", " + q.toString() + " with URI " + uri + " not present on entity " |
| 67 | + + entity.getId()); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + public static void assertCVTermIsPresent(AbstractNamedSBase entity, |
| 72 | + CVTerm.Type t, |
| 73 | + CVTerm.Qualifier q, |
| 74 | + String uri, |
| 75 | + String message) { |
| 76 | + var cvTerms = entity.getCVTerms(); |
| 77 | + for (var term : cvTerms) { |
| 78 | + for (var r : term.getResources()) { |
| 79 | + if (term.getQualifierType().equals(t) |
| 80 | + && (null == q || term.getQualifier().equals(q)) |
| 81 | + && (null == uri || r.equals(uri))) { |
| 82 | + assertTrue(true); |
| 83 | + return; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + assertTrue(false, message); |
| 88 | + } |
| 89 | + |
| 90 | + public static void assertCVTermsArePresent(AbstractNamedSBase entity, |
| 91 | + CVTerm.Type t, |
| 92 | + CVTerm.Qualifier q, |
| 93 | + Collection<String> uris, |
| 94 | + String message) { |
| 95 | + var cvTerms = entity.getCVTerms(); |
| 96 | + for(var term : cvTerms) { |
| 97 | + // this is just an awkward way to implement set difference, sorry bout that |
| 98 | + Collection<String> us = null; |
| 99 | + if(null != uris) { |
| 100 | + us = new HashSet<>(uris.stream().collect(Collectors.toSet())); |
| 101 | + us.removeAll(term.getResources()); |
| 102 | + } |
| 103 | + // <- until here |
| 104 | + |
| 105 | + if (term.getQualifierType().equals(t) |
| 106 | + && (null == q || term.getQualifier().equals(q)) |
| 107 | + && (null == uris || us.isEmpty())) { |
| 108 | + assertTrue(true); |
| 109 | + return; |
| 110 | + } |
| 111 | + } |
| 112 | + assertTrue(false, message); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * This is a development-time utility, which serves to quickly get a glimpse |
| 117 | + * of which CV Terms are present on an entity to ease writing assertions. |
| 118 | + */ |
| 119 | + public static String printCVTerm(CVTerm c) { |
| 120 | + var newline = System.getProperty("line.separator"); |
| 121 | + return Stream.of(c.getQualifierType().toString(), |
| 122 | + c.getQualifier().toString(), |
| 123 | + c.getResources().stream().collect(Collectors.joining(", "))) |
| 124 | + .collect(Collectors.joining(newline)); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * This is a development-time utility, which serves to quickly print assertions. |
| 129 | + */ |
| 130 | + public static String printCVTermAssertion(CVTerm c) { |
| 131 | + var newline = System.getProperty("line.separator"); |
| 132 | + return "assertCVTermIsPresent(__," + newline |
| 133 | + + "CVTerm.Type." + c.getQualifierType().toString() + "," + newline |
| 134 | + + "CVTerm.Qualifier." + c.getQualifier().toString() + "," + newline |
| 135 | + + "\"" + c.getResources().stream().collect(Collectors.joining("\", \"")) |
| 136 | + + "\");"; |
| 137 | + } |
| 138 | + |
| 139 | + public static String printAllCVTerms(AbstractNamedSBase entity) { |
| 140 | + var newline = System.getProperty("line.separator"); |
| 141 | + return entity.getCVTerms().stream().map(TestUtils::printCVTermAssertion).collect(Collectors.joining(newline)); |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments