| 1 |  | package org.kuali.student.common.assembly.dictionary; | 
            
  
    | 2 |  |  | 
            
  
    | 3 |  | import java.io.File; | 
            
  
    | 4 |  | import java.io.FileNotFoundException; | 
            
  
    | 5 |  | import java.io.FileOutputStream; | 
            
  
    | 6 |  | import java.io.OutputStream; | 
            
  
    | 7 |  | import java.io.PrintStream; | 
            
  
    | 8 |  | import java.util.Date; | 
            
  
    | 9 |  | import java.util.HashSet; | 
            
  
    | 10 |  | import java.util.LinkedHashMap; | 
            
  
    | 11 |  | import java.util.LinkedHashSet; | 
            
  
    | 12 |  | import static org.junit.Assert.assertTrue; | 
            
  
    | 13 |  | import static org.junit.Assert.assertEquals; | 
            
  
    | 14 |  | import static org.junit.Assert.assertFalse; | 
            
  
    | 15 |  | import static org.junit.Assert.assertNotNull; | 
            
  
    | 16 |  | import static org.junit.Assert.assertNull; | 
            
  
    | 17 |  |  | 
            
  
    | 18 |  | import java.util.Map; | 
            
  
    | 19 |  | import java.util.Set; | 
            
  
    | 20 |  |  | 
            
  
    | 21 |  | import org.junit.Test; | 
            
  
    | 22 |  | import org.kuali.student.common.assembly.data.ConstraintMetadata; | 
            
  
    | 23 |  | import org.kuali.student.common.assembly.data.Metadata; | 
            
  
    | 24 |  | import org.kuali.student.common.assembly.dictionary.MetadataFormatter; | 
            
  
    | 25 |  | import org.kuali.student.common.assembly.dictionary.MetadataServiceImpl; | 
            
  
    | 26 |  | import org.kuali.student.common.dictionary.service.impl.DictionaryServiceImpl; | 
            
  
    | 27 |  |  | 
               
        |  |  | 
           
           |  | 98.9% | Uncovered Elements: 1 (88) | Complexity: 4 | Complexity Density: 0.05 |  | 
  
  
    | 28 |  | public class TestMetadataServiceImpl { | 
            
  
    | 29 |  |  | 
            
  
    | 30 |  | DictionaryServiceImpl dictionaryDelegate = new DictionaryServiceImpl("classpath:test-validator-context.xml"); | 
            
  
    | 31 |  | private static final String SIMPLE_STUDENT = "simpleStudent"; | 
            
  
    | 32 |  | private static final String ADDRESS_INFO = "addressInfo"; | 
            
  
    | 33 |  | private static final String US_ADDRESS_TYPE = "US"; | 
            
  
    | 34 |  | private static final String CANADIAN_ADDRESS_TYPE = "CAN"; | 
            
  
    | 35 |  |  | 
               
        |  |  | 
           
           |  | 100% | Uncovered Elements: 0 (42) | Complexity: 1 | Complexity Density: 0.02 | 1
    PASS |  | 
  
  
    | 36 | 1 |  @Test... | 
            
  
    | 37 |  | public void testMetadataService() { | 
            
  
    | 38 | 1 | MockDictionaryService mockDictionaryService = new MockDictionaryService(); | 
            
  
    | 39 | 1 | mockDictionaryService.setDictionaryServiceDelegate(dictionaryDelegate); | 
            
  
    | 40 |  |  | 
            
  
    | 41 | 1 | MetadataServiceImpl metadataService = new MetadataServiceImpl( | 
            
  
    | 42 |  | mockDictionaryService); | 
            
  
    | 43 | 1 | metadataService.setUiLookupContext("classpath:test-lookup-context.xml"); | 
            
  
    | 44 | 1 | Metadata metadata = metadataService.getMetadata(SIMPLE_STUDENT); | 
            
  
    | 45 |  |  | 
            
  
    | 46 | 1 | Map<String, Metadata> properties = metadata.getProperties(); | 
            
  
    | 47 | 1 | assertTrue(properties.containsKey("firstName")); | 
            
  
    | 48 | 1 | assertTrue(properties.containsKey("dob")); | 
            
  
    | 49 | 1 | assertTrue(properties.containsKey("gpa")); | 
            
  
    | 50 |  |  | 
            
  
    | 51 | 1 | assertEquals(1, properties.get("firstName").getConstraints().size()); | 
            
  
    | 52 | 1 | assertEquals(1, properties.get("dob").getConstraints().size()); | 
            
  
    | 53 | 1 | assertEquals(1, properties.get("gpa").getConstraints().size()); | 
            
  
    | 54 |  |  | 
            
  
    | 55 | 1 | ConstraintMetadata nameConstraints = properties.get("firstName").getConstraints().get(0); | 
            
  
    | 56 | 1 | assertTrue(nameConstraints.getMinLength() == 2); | 
            
  
    | 57 | 1 | assertTrue(nameConstraints.getMaxLength() == 20); | 
            
  
    | 58 |  |  | 
            
  
    | 59 |  |  | 
            
  
    | 60 | 1 | ConstraintMetadata gpaConstraints = properties.get("gpa").getConstraints().get(0); | 
            
  
    | 61 | 1 | assertTrue(gpaConstraints.isRequiredForNextState()); | 
            
  
    | 62 | 1 | assertEquals("SUBMITTED", gpaConstraints.getNextState()); | 
            
  
    | 63 |  |  | 
            
  
    | 64 |  |  | 
            
  
    | 65 | 1 | metadata = metadataService.getMetadata(SIMPLE_STUDENT, "RETIRED"); | 
            
  
    | 66 | 1 | gpaConstraints = metadata.getProperties().get("gpa").getConstraints().get(0); | 
            
  
    | 67 | 1 | assertFalse(gpaConstraints.isRequiredForNextState()); | 
            
  
    | 68 | 1 | assertNull(gpaConstraints.getNextState()); | 
            
  
    | 69 |  |  | 
            
  
    | 70 |  |  | 
            
  
    | 71 | 1 | metadata = metadataService.getMetadata(ADDRESS_INFO); | 
            
  
    | 72 | 1 | ConstraintMetadata addrLineConstraint = metadata.getProperties().get("line1").getConstraints().get(0); | 
            
  
    | 73 | 1 | assertEquals(2, addrLineConstraint.getMinLength().intValue()); | 
            
  
    | 74 | 1 | assertEquals(1, addrLineConstraint.getMinOccurs().intValue()); | 
            
  
    | 75 | 1 | assertEquals(30, addrLineConstraint.getMaxLength().intValue()); | 
            
  
    | 76 |  |  | 
            
  
    | 77 | 1 | metadata = metadataService.getMetadata(ADDRESS_INFO, US_ADDRESS_TYPE, "SUBMITTED"); | 
            
  
    | 78 | 1 | addrLineConstraint = metadata.getProperties().get("line1").getConstraints().get(0); | 
            
  
    | 79 | 1 | assertEquals(2, addrLineConstraint.getMinLength().intValue()); | 
            
  
    | 80 | 1 | assertEquals(1, addrLineConstraint.getMinOccurs().intValue()); | 
            
  
    | 81 | 1 | assertEquals(30, addrLineConstraint.getMaxLength().intValue()); | 
            
  
    | 82 |  |  | 
            
  
    | 83 | 1 | metadata = metadataService.getMetadata(ADDRESS_INFO, US_ADDRESS_TYPE, "DRAFT"); | 
            
  
    | 84 | 1 | addrLineConstraint = metadata.getProperties().get("line1").getConstraints().get(0); | 
            
  
    | 85 | 1 | assertEquals(2, addrLineConstraint.getMinLength().intValue()); | 
            
  
    | 86 | 1 | assertEquals(1, addrLineConstraint.getMinOccurs().intValue()); | 
            
  
    | 87 | 1 | assertEquals(30, addrLineConstraint.getMaxLength().intValue()); | 
            
  
    | 88 |  |  | 
            
  
    | 89 | 1 | metadata = metadataService.getMetadata(ADDRESS_INFO, CANADIAN_ADDRESS_TYPE, "DRAFT"); | 
            
  
    | 90 | 1 | addrLineConstraint = metadata.getProperties().get("line1").getConstraints().get(0); | 
            
  
    | 91 | 1 | assertEquals(2, addrLineConstraint.getMinLength().intValue()); | 
            
  
    | 92 | 1 | assertEquals(1, addrLineConstraint.getMinOccurs().intValue()); | 
            
  
    | 93 | 1 | assertEquals(30, addrLineConstraint.getMaxLength().intValue()); | 
            
  
    | 94 |  | } | 
            
  
    | 95 |  |  | 
               
        |  |  | 
           
           |  | 97.7% | Uncovered Elements: 1 (44) | Complexity: 3 | Complexity Density: 0.07 | 1
    PASS |  | 
  
  
    | 96 | 1 |  @Test... | 
            
  
    | 97 |  | public void testMetadataFormatter() { | 
            
  
    | 98 |  |  | 
            
  
    | 99 | 1 | Set<String> startingObjects = new LinkedHashSet<String>(); | 
            
  
    | 100 | 1 | Map<String, Set<String>> types = new LinkedHashMap<String, Set<String>>(); | 
            
  
    | 101 |  |  | 
            
  
    | 102 | 1 | startingObjects.add(SIMPLE_STUDENT); | 
            
  
    | 103 | 1 | startingObjects.add(ADDRESS_INFO); | 
            
  
    | 104 |  |  | 
            
  
    | 105 | 1 | MockDictionaryService mockDictionaryService = new MockDictionaryService(); | 
            
  
    | 106 | 1 | mockDictionaryService.setDictionaryServiceDelegate(dictionaryDelegate); | 
            
  
    | 107 |  |  | 
            
  
    | 108 | 1 | MetadataServiceImpl metadataService = new MetadataServiceImpl(mockDictionaryService); | 
            
  
    | 109 | 1 | metadataService.setUiLookupContext("classpath:test-lookup-context.xml"); | 
            
  
    | 110 |  |  | 
            
  
    | 111 |  |  | 
            
  
    | 112 | 1 | Set<String> typesForClass = new LinkedHashSet<String>(); | 
            
  
    | 113 | 1 | types.put(ADDRESS_INFO, typesForClass); | 
            
  
    | 114 | 1 | typesForClass.add(US_ADDRESS_TYPE); | 
            
  
    | 115 | 1 | typesForClass.add(CANADIAN_ADDRESS_TYPE); | 
            
  
    | 116 |  |  | 
            
  
    | 117 | 1 | String outFile = "target/test-metadata.txt"; | 
            
  
    | 118 | 1 | File file = new File(outFile); | 
            
  
    | 119 | 1 | OutputStream outputStream = null; | 
            
  
    | 120 | 1 | try { | 
            
  
    | 121 | 1 | outputStream = new FileOutputStream(file, false); | 
            
  
    | 122 |  | } catch (FileNotFoundException ex) { | 
            
  
    | 123 | 0 | throw new RuntimeException(ex); | 
            
  
    | 124 |  | } | 
            
  
    | 125 | 1 | PrintStream out = new PrintStream(outputStream); | 
            
  
    | 126 | 1 | out.println("(!) This page was automatically generated on "    + new Date()); | 
            
  
    | 127 | 1 | out.println("DO NOT UPDATE MANUALLY!"); | 
            
  
    | 128 | 1 | out.println(""); | 
            
  
    | 129 | 1 | out.println("This page represents a formatted view of the test dictionary"); | 
            
  
    | 130 | 1 | for (String objectKey : startingObjects) { | 
            
  
    | 131 | 2 | out.println("# " + objectKey); | 
            
  
    | 132 |  | } | 
            
  
    | 133 | 1 | out.println(""); | 
            
  
    | 134 | 1 | out.println("----"); | 
            
  
    | 135 | 1 | out.println("{toc}"); | 
            
  
    | 136 | 1 | out.println("----"); | 
            
  
    | 137 |  |  | 
            
  
    | 138 | 1 | for (String objectKey : startingObjects) { | 
            
  
    | 139 |  |  | 
            
  
    | 140 | 2 | Metadata metadata = metadataService.getMetadata(objectKey); | 
            
  
    | 141 | 2 | assertNotNull(metadata); | 
            
  
    | 142 | 2 | MetadataFormatter formatter = new MetadataFormatter(objectKey, | 
            
  
    | 143 |  | metadata, null, null, new HashSet<Metadata>(), 1); | 
            
  
    | 144 | 2 | out.println(formatter.formatForWiki()); | 
            
  
    | 145 | 2 | if (types.get(objectKey) == null) { | 
            
  
    | 146 | 1 | continue; | 
            
  
    | 147 |  | } | 
            
  
    | 148 | 1 | for (String type : types.get(objectKey)) { | 
            
  
    | 149 | 2 | System.out.println("*** Generating formatted version for " + type); | 
            
  
    | 150 | 2 | metadata = metadataService.getMetadata(objectKey, type, (String) null); | 
            
  
    | 151 | 2 | assertNotNull(metadata); | 
            
  
    | 152 | 2 | formatter = new MetadataFormatter(objectKey, metadata, type, null, new HashSet<Metadata>(), 1); | 
            
  
    | 153 | 2 | out.println(formatter.formatForWiki()); | 
            
  
    | 154 |  | } | 
            
  
    | 155 |  | } | 
            
  
    | 156 |  | } | 
            
  
    | 157 |  | } |