Skip to content

Commit eb188d4

Browse files
committed
improvements to contract violation messages updated readme
1 parent d868924 commit eb188d4

File tree

8 files changed

+47
-6
lines changed

8 files changed

+47
-6
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,16 @@ from your molwitch implementation root directory:
2222

2323
This will run thousands of automated tests that will produce a report when it's
2424
complete to show which features are Fully Compliant, Partially Compliant or Not Compliant.
25-
There may be additional notes or comments about which specific sub-features are not passing.
25+
There may be additional notes or comments about which specific sub-features are not passing.
26+
27+
## How To make Tests Fail
28+
By default, the API Checker will still make all the junit tests pass
29+
even if they throw exceptions or assertion errors . This is so a molwitch-implementation
30+
that isn't fully compliant can still be move through all the phases of the maven pipeline
31+
to be packaged and deployed even if some molwitch features are missing.
32+
33+
To make the failing tests fail the build, set this parameter ot true `molwitch.apiContract.ErrorOnFailure`
34+
35+
```
36+
mvn clean test -Dtest=ApiCheckerSuite -Dmolwitch.apiContract.ErrorOnFailure=true
37+
```

src/test/java/gov/nih/ncats/molwitch/tests/AtomAliasTestApi.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.Optional;
2424

25+
import gov.nih.ncats.molwitch.tests.contract.ApiContract;
2526
import gov.nih.ncats.molwitch.tests.contract.BasicApiContractChecker;
2627
import org.junit.Before;
2728
import org.junit.ClassRule;
@@ -57,32 +58,37 @@ public void setAliasShouldBeEmpty() {
5758
}
5859

5960
@Test
61+
@ApiContract(category = "R Group")
6062
public void notRGroupWithAliasUnset() {
6163
assertFalse(atom.isRGroupAtom());
6264
assertTrue(!atom.getRGroupIndex().isPresent());
6365
}
6466
@Test
67+
@ApiContract(category = "R Group")
6568
public void notRGroupWithAliasSetToSomethingElse() {
6669
atom.setAlias("myAlias");
6770
assertFalse(atom.isRGroupAtom());
6871
assertTrue(!atom.getRGroupIndex().isPresent());
6972
}
7073

7174
@Test
75+
@ApiContract(category = "R Group")
7276
public void isRGroupWithAliasSetToRXX() {
7377
atom.setAlias("R10");
7478
assertTrue(atom.isRGroupAtom());
7579
assertEquals(10, atom.getRGroupIndex().getAsInt());
7680
}
7781

7882
@Test
83+
@ApiContract(category = "R Group")
7984
public void isRGroupWithRGroupSetDirectly() {
8085
atom.setRGroup(10);
8186
assertTrue(atom.isRGroupAtom());
8287
assertEquals(10, atom.getRGroupIndex().getAsInt());
8388
}
8489

8590
@Test
91+
@ApiContract(category = "R Group")
8692
public void unsetAnAlreadySetRGroup() {
8793
atom.setRGroup(10);
8894
assertTrue(atom.isRGroupAtom());
@@ -92,6 +98,7 @@ public void unsetAnAlreadySetRGroup() {
9298
}
9399

94100
@Test
101+
@ApiContract(category = "R Group")
95102
public void unsetAnRGroupWithAnAliasAleadyExisting() {
96103
atom.setAlias("foo");
97104
atom.setRGroup(null);
@@ -103,6 +110,7 @@ public void unsetAnRGroupWithAnAliasAleadyExisting() {
103110
}
104111

105112
@Test
113+
@ApiContract(category = "R Group")
106114
public void changeRGroup() {
107115
atom.setRGroup(10);
108116
atom.setRGroup(5);

src/test/java/gov/nih/ncats/molwitch/tests/CreateChemicalTestApi.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.stream.Collectors;
3939

4040
import gov.nih.ncats.molwitch.io.*;
41+
import gov.nih.ncats.molwitch.tests.contract.ApiContract;
4142
import gov.nih.ncats.molwitch.tests.contract.BasicApiContractChecker;
4243
import org.junit.Assert;
4344
import org.junit.ClassRule;
@@ -308,6 +309,7 @@ public void toKekulizedSmilesFile() throws IOException{
308309
}
309310

310311
@Test
312+
@ApiContract(category = "Atom Map", message = "Can't Set Atom Map")
311313
public void setAtomMap() throws IOException {
312314
Chemical chem = Chemical.createFromSmilesAndComputeCoordinates(smiles);
313315

@@ -317,13 +319,15 @@ public void setAtomMap() throws IOException {
317319
assertEquals(8, chem.getAtom(4).getAtomToAtomMap().getAsInt());
318320
}
319321
@Test
322+
@ApiContract(category = "Atom Map", message = "Can't Clear Atom Map")
320323
public void notSetAtomMap() throws IOException {
321324
Chemical chem = Chemical.createFromSmilesAndComputeCoordinates(smiles);
322325

323326
chem.getAtom(4).clearAtomToAtomMap();
324327
assertFalse(chem.getAtom(4).getAtomToAtomMap().isPresent());
325328
}
326329
@Test
330+
@ApiContract(category = "Atom Map", message = "Can't Set Atom Map")
327331
public void clearAtomMap() throws IOException {
328332
Chemical chem = Chemical.createFromSmilesAndComputeCoordinates(smiles);
329333

src/test/java/gov/nih/ncats/molwitch/tests/WriteAsMolFileTestApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222

2323
import gov.nih.ncats.molwitch.io.ChemFormat;
24+
import gov.nih.ncats.molwitch.tests.contract.ApiContract;
2425
import gov.nih.ncats.molwitch.tests.contract.BasicApiContractChecker;
2526
import org.junit.ClassRule;
2627
import org.junit.Rule;
@@ -127,6 +128,7 @@ public void aromatizeAsISMol() throws IOException{
127128
}
128129

129130
@Test
131+
@ApiContract( message = "can't write v3000")
130132
public void kekulizeMol3000() throws IOException{
131133
Chemical chem = Chemical.createFromSmilesAndComputeCoordinates(smiles);
132134
chem.setName("my_id");

src/test/java/gov/nih/ncats/molwitch/tests/contract/ApiContract.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
@Retention(RetentionPolicy.RUNTIME)
2828
public @interface ApiContract {
2929
public static String DEFAULT_NULL_MESSAGE= "";
30-
String category();
30+
String category() default DEFAULT_NULL_MESSAGE;
3131

3232
String message() default DEFAULT_NULL_MESSAGE;
3333

src/test/java/gov/nih/ncats/molwitch/tests/contract/ApiContractChecker.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ protected void before() throws Throwable {
5353
complianceMessageMap = new HashMap<>();
5454
}
5555

56+
protected String getDefaultCategory(){
57+
return "";
58+
}
59+
5660
@Override
5761
protected void after() {
5862
String errorMessage = failIf.apply(map);
@@ -92,6 +96,9 @@ public void evaluate() throws Throwable {
9296

9397
if(contract!=null){
9498
String category = contract.category();
99+
if(category==null ||category.trim().isEmpty() ){
100+
category=getDefaultCategory();
101+
}
95102
String message = contract.message();
96103
if(ApiContract.DEFAULT_NULL_MESSAGE.equals(message)){
97104
message =null;

src/test/java/gov/nih/ncats/molwitch/tests/contract/ApiContractSuiteRule.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class ApiContractSuiteRule extends ExternalResource {
3131
protected void after() {
3232
// System.out.println(GlobalApiContractChecker.INSTANCE.getMap());
3333
System.out.println("====== " + MolWitch.getModuleName() + " COMPLIANCE CONTRACT RESULTS ========");
34+
35+
System.out.println("| Feature | Compliance Level | Comments|\n | ------ | ---------- | ---------- |");
3436
try {
3537
for (Map.Entry<String, Map<ApiContractChecker.ComplianceLevel, SingleThreadCounter>> entry : GlobalApiContractChecker.INSTANCE.getMap().entrySet()) {
3638
String category = entry.getKey();
@@ -40,8 +42,8 @@ protected void after() {
4042
if (entry.getValue().size() == 1) {
4143
Map.Entry<ApiContractChecker.ComplianceLevel, SingleThreadCounter> singleEntry = entry.getValue().entrySet().iterator().next();
4244
result = singleEntry.getKey().toString();
43-
System.out.println(category + "\t" + result + ((messageMap==null || messageMap.get(singleEntry.getKey()) == null) ? "" : messageMap.get(singleEntry.getKey()).stream().collect(Collectors.joining("; "))));
44-
System.out.println("--------------------------");
45+
System.out.println("| " + category + "| " + result + " | " + ((messageMap==null || messageMap.get(singleEntry.getKey()) == null) ? "" : messageMap.get(singleEntry.getKey()).stream().collect(Collectors.joining("; "))) + " |");
46+
4547

4648
} else {
4749
Comparator<Map.Entry<ApiContractChecker.ComplianceLevel, Long>> SortByLargest = Comparator.<Map.Entry<ApiContractChecker.ComplianceLevel, Long>>comparingLong(e -> e.getValue()).reversed();
@@ -55,9 +57,9 @@ protected void after() {
5557
LinkedHashMap::new));
5658

5759
for (Map.Entry<ApiContractChecker.ComplianceLevel, Long> entry2 : map.entrySet()) {
58-
System.out.println(category + "\t" + entry2.getKey() + " ( " + entry2.getValue() + " )" + ((messageMap==null || messageMap.get(entry2.getKey()) == null )? "" : messageMap.get(entry2.getKey()).stream().collect(Collectors.joining("; ", " ", ""))));
60+
System.out.println("| " + category + " | " + entry2.getKey() + " ( " + entry2.getValue() + " ) | " + ((messageMap==null || messageMap.get(entry2.getKey()) == null )? "" : messageMap.get(entry2.getKey()).stream().collect(Collectors.joining("; ", " ", ""))) + " |");
5961
}
60-
System.out.println("--------------------------");
62+
// System.out.println("--------------------------");
6163
}
6264

6365
}

src/test/java/gov/nih/ncats/molwitch/tests/contract/BasicApiContractChecker.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public enum TestResult{
3535
public BasicApiContractChecker(String defaultCategory) {
3636
this.defaultCategory = defaultCategory;
3737
}
38+
39+
@Override
40+
protected String getDefaultCategory() {
41+
return defaultCategory;
42+
}
43+
3844
private Map<TestResult, SingleThreadCounter> testResult = new EnumMap<>(TestResult.class);
3945

4046
@Override

0 commit comments

Comments
 (0)