001    package org.kuali.student.common.assembly.dictionary;
002    
003    import java.io.File;
004    import java.io.FileNotFoundException;
005    import java.io.FileOutputStream;
006    import java.io.OutputStream;
007    import java.io.PrintStream;
008    import java.util.Date;
009    import java.util.HashSet;
010    import java.util.LinkedHashMap;
011    import java.util.LinkedHashSet;
012    import static org.junit.Assert.assertTrue;
013    import static org.junit.Assert.assertEquals;
014    import static org.junit.Assert.assertFalse;
015    import static org.junit.Assert.assertNotNull;
016    import static org.junit.Assert.assertNull;
017    
018    import java.util.Map;
019    import java.util.Set;
020    
021    import org.junit.Test;
022    import org.kuali.student.common.assembly.data.ConstraintMetadata;
023    import org.kuali.student.common.assembly.data.Metadata;
024    import org.kuali.student.common.assembly.dictionary.MetadataFormatter;
025    import org.kuali.student.common.assembly.dictionary.MetadataServiceImpl;
026    import org.kuali.student.common.dictionary.service.impl.DictionaryServiceImpl;
027    
028    public class TestMetadataServiceImpl {
029    
030            DictionaryServiceImpl dictionaryDelegate = new DictionaryServiceImpl("classpath:test-validator-context.xml");
031            private static final String SIMPLE_STUDENT = "simpleStudent";
032            private static final String ADDRESS_INFO = "addressInfo";
033            private static final String US_ADDRESS_TYPE = "US";
034            private static final String CANADIAN_ADDRESS_TYPE = "CAN";
035    
036            @Test
037            public void testMetadataService() {
038                    MockDictionaryService mockDictionaryService = new MockDictionaryService();
039                    mockDictionaryService.setDictionaryServiceDelegate(dictionaryDelegate);
040    
041                    MetadataServiceImpl metadataService = new MetadataServiceImpl(
042                                    mockDictionaryService);
043                    metadataService.setUiLookupContext("classpath:test-lookup-context.xml");
044                    Metadata metadata = metadataService.getMetadata(SIMPLE_STUDENT);
045    
046                    Map<String, Metadata> properties = metadata.getProperties();
047                    assertTrue(properties.containsKey("firstName"));
048                    assertTrue(properties.containsKey("dob"));
049                    assertTrue(properties.containsKey("gpa"));
050    
051                    assertEquals(1, properties.get("firstName").getConstraints().size());
052                    assertEquals(1, properties.get("dob").getConstraints().size());
053                    assertEquals(1, properties.get("gpa").getConstraints().size());
054    
055                    ConstraintMetadata nameConstraints = properties.get("firstName").getConstraints().get(0);
056                    assertTrue(nameConstraints.getMinLength() == 2);
057                    assertTrue(nameConstraints.getMaxLength() == 20);
058    
059                    // Check requiredness for default state of draft
060                    ConstraintMetadata gpaConstraints = properties.get("gpa").getConstraints().get(0);
061                    assertTrue(gpaConstraints.isRequiredForNextState());
062                    assertEquals("ACTIVE", gpaConstraints.getNextState());
063    
064                    // Check requiredness for state of RETIRED (there should be no next state)
065                    metadata = metadataService.getMetadata(SIMPLE_STUDENT, "RETIRED");
066                    gpaConstraints = metadata.getProperties().get("gpa").getConstraints().get(0);
067                    assertFalse(gpaConstraints.isRequiredForNextState());
068                    assertNull(gpaConstraints.getNextState());
069    
070    
071                    //Check requiredness by workflow Node
072                    metadata = metadataService.getMetadataByWorkflowNode(SIMPLE_STUDENT, "NODE A", null);
073                    gpaConstraints = metadata.getProperties().get("gpa").getConstraints().get(0);
074                    assertFalse(gpaConstraints.isRequiredForNextState());
075                    assertNull(gpaConstraints.getNextState());
076                    assertEquals(0, gpaConstraints.getMinOccurs().intValue());
077                    
078                    metadata = metadataService.getMetadataByWorkflowNode(SIMPLE_STUDENT, "NODE B", null);
079                    gpaConstraints = metadata.getProperties().get("gpa").getConstraints().get(0);
080                    assertTrue(gpaConstraints.isRequiredForNextState());
081                    assertEquals("APPROVED", gpaConstraints.getNextState());
082                    assertTrue(gpaConstraints.getMinOccurs()==0);
083    
084                    metadata = metadataService.getMetadataByWorkflowNode(SIMPLE_STUDENT, "NODE C", null);
085                    gpaConstraints = metadata.getProperties().get("gpa").getConstraints().get(0);
086                    assertFalse(gpaConstraints.isRequiredForNextState());
087                    assertNull(gpaConstraints.getNextState());
088                    assertTrue(gpaConstraints.getMinOccurs()==1);
089                    
090                    
091                    // Check type and nested state
092                    metadata = metadataService.getMetadata(ADDRESS_INFO);
093                    ConstraintMetadata addrLineConstraint = metadata.getProperties().get("line1").getConstraints().get(0);
094                    assertEquals(2, addrLineConstraint.getMinLength().intValue());
095                    assertEquals(1, addrLineConstraint.getMinOccurs().intValue());
096                    assertEquals(30, addrLineConstraint.getMaxLength().intValue());
097    
098                    metadata = metadataService.getMetadata(ADDRESS_INFO, US_ADDRESS_TYPE, "ACTIVE");
099                    addrLineConstraint = metadata.getProperties().get("line1").getConstraints().get(0);
100                    assertEquals(2, addrLineConstraint.getMinLength().intValue());
101                    assertEquals(1, addrLineConstraint.getMinOccurs().intValue());
102                    assertEquals(30, addrLineConstraint.getMaxLength().intValue());
103    
104                    metadata = metadataService.getMetadata(ADDRESS_INFO, US_ADDRESS_TYPE, "DRAFT");
105                    addrLineConstraint = metadata.getProperties().get("line1").getConstraints().get(0);
106                    assertEquals(2, addrLineConstraint.getMinLength().intValue());
107                    assertEquals(1, addrLineConstraint.getMinOccurs().intValue());
108                    assertEquals(30, addrLineConstraint.getMaxLength().intValue());
109    
110                    metadata = metadataService.getMetadata(ADDRESS_INFO, CANADIAN_ADDRESS_TYPE, "DRAFT");
111                    addrLineConstraint = metadata.getProperties().get("line1").getConstraints().get(0);
112                    assertEquals(2, addrLineConstraint.getMinLength().intValue());
113                    assertEquals(1, addrLineConstraint.getMinOccurs().intValue());
114                    assertEquals(30, addrLineConstraint.getMaxLength().intValue());
115            }
116    
117            @Test
118            public void testMetadataFormatter() {
119    
120                    Set<String> startingObjects = new LinkedHashSet<String>();
121                    Map<String, Set<String>> types = new LinkedHashMap<String, Set<String>>();
122    
123                    startingObjects.add(SIMPLE_STUDENT);
124                    startingObjects.add(ADDRESS_INFO);
125    
126                    MockDictionaryService mockDictionaryService = new MockDictionaryService();
127                    mockDictionaryService.setDictionaryServiceDelegate(dictionaryDelegate);
128    
129                    MetadataServiceImpl metadataService = new MetadataServiceImpl(mockDictionaryService);
130                    metadataService.setUiLookupContext("classpath:test-lookup-context.xml");
131                    // startingClasses.add (StatementTreeViewInfo.class);
132    
133                    Set<String> typesForClass = new LinkedHashSet<String>();
134                    types.put(ADDRESS_INFO, typesForClass);
135                    typesForClass.add(US_ADDRESS_TYPE);
136                    typesForClass.add(CANADIAN_ADDRESS_TYPE);
137    
138                    String outFile = "target/test-metadata.txt";
139                    File file = new File(outFile);
140                    OutputStream outputStream = null;
141                    try {
142                            outputStream = new FileOutputStream(file, false);
143                    } catch (FileNotFoundException ex) {
144                            throw new RuntimeException(ex);
145                    }
146                    PrintStream out = new PrintStream(outputStream);
147                    out.println("(!) This page was automatically generated on "     + new Date());
148                    out.println("DO NOT UPDATE MANUALLY!");
149                    out.println("");
150                    out.println("This page represents a formatted view of the test dictionary");
151                    for (String objectKey : startingObjects) {
152                            out.println("# " + objectKey);
153                    }
154                    out.println("");
155                    out.println("----");
156                    out.println("{toc}");
157                    out.println("----");
158    
159                    for (String objectKey : startingObjects) {
160                            // out.println ("getting meta data for " + clazz.getName ());
161                            Metadata metadata = metadataService.getMetadata(objectKey);
162                            assertNotNull(metadata);
163                            MetadataFormatter formatter = new MetadataFormatter(objectKey,
164                                            metadata, null, null, new HashSet<Metadata>(), 1);
165                            out.println(formatter.formatForWiki());
166                            if (types.get(objectKey) == null) {
167                                    continue;
168                            }
169                            for (String type : types.get(objectKey)) {
170                                    System.out.println("*** Generating formatted version for " + type);
171                                    metadata = metadataService.getMetadata(objectKey, type, (String) null);
172                                    assertNotNull(metadata);
173                                    formatter = new MetadataFormatter(objectKey, metadata, type, null, new HashSet<Metadata>(), 1);
174                                    out.println(formatter.formatForWiki());
175                            }
176                    }
177            }
178    }