View Javadoc

1   /**
2    * Copyright 2013 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   * Created by Charles on 5/10/13
16   */
17  package org.kuali.student.enrollment.class2.courseoffering.service.util;
18  
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  /**
25   * Creates a matrix to determine whether going from a fromState to a toState is valid.
26   * Both states are valid
27   * If the value is null, then it wasn't tested.
28   *
29   * @author Kuali Student Team
30   */
31  public class PseudoUnitTestStateTransitionGrid {
32      List<String> stateKeys;
33      List<String> allowedValues;
34      List<List<String>> expectedTransitions;
35      List<List<String>> actualTransitions;
36      String socStateKey;
37      String entityType;  // ao, fo, co
38  
39      public PseudoUnitTestStateTransitionGrid(List<String> stateNames, List<String> allowedValues, String entityType) {
40          this.entityType = entityType;
41          this.stateKeys = new ArrayList<String>();
42          this.stateKeys.addAll(stateNames); // make a deep copy
43          this.allowedValues = new ArrayList<String>();
44          this.allowedValues.addAll(allowedValues);
45          // Create the grid
46          expectedTransitions = _createTransitionsGrid();
47          actualTransitions = _createTransitionsGrid();
48      }
49  
50      private List<List<String>> _createTransitionsGrid() {
51          List<List<String>> grid = new ArrayList<List<String>>();  // Set size to 0
52          int size = stateKeys.size();
53          for (int i = 0; i < size; i++) {
54              List<String> row = new ArrayList<String>();
55              for (int j = 0; j < size; j++) {
56                  row.add(TransitionGridYesNoEnum.INVALID.getName());
57              }
58              grid.add(row);
59          }
60          return grid;
61      }
62  
63      public String getEntityType() {
64          return entityType;
65      }
66  
67      public void setSocStateKey(String socStateKey) {
68          this.socStateKey = socStateKey;
69      }
70  
71      public String getSocStateKey() {
72          return socStateKey;
73      }
74  
75      public void setTransition(AFUTTypeEnum actualOrExpected, String fromState, String toState, String value) {
76          if (!allowedValues.contains(value)) {
77              throw new RuntimeException(value + " is in invalid value for grid: " + entityType);
78          }
79          if (!stateKeys.contains(fromState) || !stateKeys.contains(toState)) {
80              throw new RuntimeException("Invalid fromState (" + fromState + ") or toState (" + toState + ")");
81          }
82          int fromIndex = stateKeys.indexOf(fromState);
83          if (fromIndex < 0) {
84              throw new IndexOutOfBoundsException("fromIndex out of bounds: " + fromIndex);
85          }
86          int toIndex = stateKeys.indexOf(toState);
87          if (toIndex < 0) {
88              throw new IndexOutOfBoundsException("toIndex out of bounds: " + toIndex);
89          }
90          if (actualOrExpected == AFUTTypeEnum.EXPECTED) {
91              expectedTransitions.get(fromIndex).set(toIndex, value);
92          } else {
93              actualTransitions.get(fromIndex).set(toIndex, value);
94          }
95      }
96  
97      public String getTransition(AFUTTypeEnum actualOrExpected, String fromState, String toState) {
98          int fromIndex = stateKeys.indexOf(fromState);
99          if (fromIndex < 0) {
100             throw new IndexOutOfBoundsException("fromIndex out of bounds: " + fromIndex);
101         }
102         int toIndex = stateKeys.indexOf(toState);
103         if (toIndex < 0) {
104             throw new IndexOutOfBoundsException("toIndex out of bounds: " + toIndex);
105         }
106         if (actualOrExpected == AFUTTypeEnum.EXPECTED) {
107             return expectedTransitions.get(fromIndex).get(toIndex);
108         } else {
109             return actualTransitions.get(fromIndex).get(toIndex);
110         }
111     }
112 
113     public String getStateKeyAt(int index) {
114         return stateKeys.get(index);
115     }
116 
117     public int size() {
118         return stateKeys.size();
119     }
120 
121     public String getTransition(AFUTTypeEnum actualOrExpected, int row, int col) {
122         if (actualOrExpected == AFUTTypeEnum.EXPECTED) {
123             return expectedTransitions.get(row).get(col);
124         } else {
125             return actualTransitions.get(row).get(col);
126         }
127     }
128 
129     public String setTransition(AFUTTypeEnum actualOrExpected, int row, int col, String val) {
130         if (actualOrExpected == AFUTTypeEnum.EXPECTED) {
131             return expectedTransitions.get(row).set(col, val);
132         } else {
133             return actualTransitions.get(row).get(col);
134         }
135     }
136 
137     // Keys
138     public static final String SOC_STATE = "socState";
139     public static final String AO_STATE_FROM = "aoStateFrom";
140     public static final String AO_STATE_TO = "aoStateTo";
141     public static final String EXPECTED = "expected";
142     public static final String ACTUAL = "actual";
143     public static final String PASS_FAIL = "passFail";
144     // Values
145     public static final String PASS_VAL = "pass";
146     public static final String FAIL_VAL = "fail";
147 
148     public List<Map<String, String>> compare() {
149         List<Map<String, String>> results = new ArrayList<Map<String, String>>();
150         for (int row = 0; row < size(); row++) {
151             for (int col = 0; col < size(); col++) {
152                 String expectedVal = getTransition(AFUTTypeEnum.EXPECTED, row, col);
153                 String actualVal = getTransition(AFUTTypeEnum.ACTUAL, row, col);
154                 Map<String, String> keyValue = new HashMap<String, String>();
155                 String fromState = getStateKeyAt(row);
156                 String toState = getStateKeyAt(col);
157                 keyValue.put(AO_STATE_FROM, fromState);
158                 keyValue.put(AO_STATE_TO, toState);
159                 keyValue.put(EXPECTED, expectedVal);
160                 keyValue.put(ACTUAL, actualVal);
161                 keyValue.put(SOC_STATE, getSocStateKey());
162                 keyValue.put(PASS_FAIL, expectedVal.equals(actualVal) ? PASS_VAL : FAIL_VAL);
163                 if (expectedVal.equals(TransitionGridYesNoEnum.INVALID.getName())) {
164                     keyValue.put(PASS_FAIL, TransitionGridYesNoEnum.INVALID.getName());
165                 }
166                 results.add(keyValue);
167             }
168         }
169         return results;
170     }
171 }