View Javadoc

1   /**
2    * Copyright 2010 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  
16  package org.kuali.student.lum.lu.dao.impl;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  
21  import java.text.SimpleDateFormat;
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import javax.persistence.Query;
27  
28  import org.junit.Test;
29  import org.kuali.student.r1.common.dao.impl.AbstractSearchableCrudDaoImpl;
30  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
31  import org.kuali.student.r2.core.versionmanagement.dto.VersionDisplayInfo;
32  import org.kuali.student.common.test.spring.AbstractTransactionalDaoTest;
33  import org.kuali.student.common.test.spring.Dao;
34  import org.kuali.student.common.test.spring.PersistenceFileLocation;
35  import org.kuali.student.r2.lum.lu.dao.LuDao;
36  import org.kuali.student.r2.lum.lu.entity.CluLoRelationType;
37  import org.kuali.student.r2.lum.lu.entity.CluSet;
38  import org.kuali.student.r2.lum.lu.entity.Lui;
39  import org.kuali.student.r2.lum.lu.entity.MembershipQuery;
40  import org.kuali.student.r2.lum.lu.entity.SearchParameter;
41  import org.kuali.student.r2.lum.lu.entity.SearchParameterValue;
42  
43  @PersistenceFileLocation("classpath:META-INF/lu-persistence.xml")
44  public class TestLuDaoImpl extends AbstractTransactionalDaoTest {
45  	@Dao(value = "org.kuali.student.r2.lum.lu.dao.impl.LuDaoImpl", testSqlFile = "classpath:ks-lu.sql")
46  	public LuDao dao;
47  
48  	private static final SimpleDateFormat DF = new SimpleDateFormat("yyyyMMdd");
49  	
50  	@Test
51  	public void testJPQL(){
52  		
53  		Query query = ((AbstractSearchableCrudDaoImpl)dao).getEm().createQuery(
54  				"SELECT clu.id, clu.officialIdentifier.code, rel.loId " + 
55                  "FROM CluLoRelation rel " + 
56                  "JOIN rel.clu clu " +
57                  "LEFT JOIN clu.adminOrgs adminOrg " );                              
58  		
59  		query.getResultList();
60  	}
61  	
62  	@Test
63  	public void testGetLuLuRelationTypeInfo(){
64  		List<Lui> luis = dao.getLuisByRelationType("LUI-3", "luLuType.type1");
65  		assertEquals(1, luis.size());
66  		assertEquals("LUI-1", luis.get(0).getId());
67  	}
68  	
69  	@Test
70  	public void testGetCluLoRelationTypeInfo() throws DoesNotExistException{
71          CluLoRelationType relType = dao.fetch(CluLoRelationType.class, "kuali.lu.lo.relation.type.includes");
72  		assertEquals("Default Clu-Lo relation type", relType.getDescr());
73  	}
74  
75  	@Test
76  	public void testCreateCluLoRelation() throws Exception {
77  	}
78  
79  	@Test
80  	public void testCreateMembershipQuery_Simple() throws Exception {
81  		MembershipQuery mq = new MembershipQuery();
82  		mq.setSearchTypeKey("searchKey1");
83  		mq = dao.create(mq);
84  		
85  		mq = dao.fetch(MembershipQuery.class, mq.getId());
86  
87  		assertEquals("searchKey1", mq.getSearchTypeKey());
88  		assertNotNull(mq.getSearchParameters());
89  		assertEquals(0, mq.getSearchParameters().size());
90  	}
91  	
92  	@Test
93  	public void testCreateMembershipQuery_Complex() throws Exception {
94  		MembershipQuery mq = createMembershipQuery();
95  		mq = dao.fetch(MembershipQuery.class, mq.getId());
96  
97  		assertEquals("searchKey1", mq.getSearchTypeKey());
98  		assertEquals(1, mq.getSearchParameters().size());
99  		assertEquals("key-1", mq.getSearchParameters().get(0).getKey());
100 		assertEquals(2, mq.getSearchParameters().get(0).getValues().size());
101 		assertEquals("value-1", mq.getSearchParameters().get(0).getValues().get(0).getValue());
102 		assertEquals("value-2", mq.getSearchParameters().get(0).getValues().get(1).getValue());
103 	}
104 
105 	@Test
106 	public void testCreateClu() throws Exception {
107 		CluSet cluSet = new CluSet();
108 		MembershipQuery mq = createMembershipQuery();
109 		cluSet.setMembershipQuery(mq);
110 		cluSet = dao.create(cluSet);
111 
112 		assertEquals("searchKey1", cluSet.getMembershipQuery().getSearchTypeKey());
113 		assertNotNull(cluSet.getMembershipQuery().getSearchParameters());
114 		assertEquals(1, cluSet.getMembershipQuery().getSearchParameters().size());
115 		assertEquals("key-1", cluSet.getMembershipQuery().getSearchParameters().get(0).getKey());
116 		assertEquals(2, cluSet.getMembershipQuery().getSearchParameters().get(0).getValues().size());
117 		assertEquals("value-1", cluSet.getMembershipQuery().getSearchParameters().get(0).getValues().get(0).getValue());
118 		assertEquals("value-2", cluSet.getMembershipQuery().getSearchParameters().get(0).getValues().get(1).getValue());
119 	}
120 	
121 	@Test
122 	public void testVersionQueries() throws Exception{
123 		List<VersionDisplayInfo> versions;
124 		versions = dao.getVersions("CLU-VERSIONTEST-IND", "foo.com");
125 		assertEquals(6,versions.size());
126 		
127 		VersionDisplayInfo versionDisplay; 
128 		versionDisplay = dao.getCurrentCluVersionInfo("CLU-VERSIONTEST-IND", "foo.com");
129 		assertEquals("CLU-VERSIONTEST-V4", versionDisplay.getId());
130 		
131 		versionDisplay = dao.getCurrentVersionOnDate("CLU-VERSIONTEST-IND", "foo.com", DF.parse("20100110"));
132 		assertEquals("CLU-VERSIONTEST-V0", versionDisplay.getId());
133 
134 		versionDisplay = dao.getCurrentVersionOnDate("CLU-VERSIONTEST-IND", "foo.com", DF.parse("20110210"));
135 		assertEquals("CLU-VERSIONTEST-V4", versionDisplay.getId());
136 
137 		versionDisplay = dao.getFirstVersion("CLU-VERSIONTEST-IND", "foo.com");
138 		assertEquals("CLU-VERSIONTEST-V0", versionDisplay.getId());
139 		
140 		versionDisplay = dao.getLatestVersion("CLU-VERSIONTEST-IND", "foo.com");
141 		assertEquals("CLU-VERSIONTEST-V5", versionDisplay.getId());
142 
143 		versionDisplay = dao.getVersionBySequenceNumber("CLU-VERSIONTEST-IND", "foo.com", Long.valueOf(3));
144 		assertEquals("CLU-VERSIONTEST-V3", versionDisplay.getId());
145 		
146 		versions = dao.getVersionsInDateRange("CLU-VERSIONTEST-IND", "foo.com", DF.parse("20100210"), DF.parse("20100410"));
147 		assertEquals(3, versions.size());
148 		
149 		versions = dao.getVersionsInDateRange("CLU-VERSIONTEST-IND", "foo.com", DF.parse("20100210"), DF.parse("20100310"));
150 		assertEquals(2, versions.size());
151 		
152 		versions = dao.getVersionsInDateRange("CLU-VERSIONTEST-IND", "foo.com", DF.parse("20100310"), DF.parse("20100310"));
153 		assertEquals(1, versions.size());
154 		
155 		versions = dao.getVersionsInDateRange("CLU-VERSIONTEST-IND", "foo.com", DF.parse("20100310"), DF.parse("20100410"));
156 		assertEquals(2, versions.size());
157 		
158 		versions = dao.getVersionsInDateRange("CLU-VERSIONTEST-IND", "foo.com", DF.parse("20090310"), DF.parse("20090410"));
159 		assertEquals(0, versions.size());		
160 
161 	}
162 
163 	private MembershipQuery createMembershipQuery() {
164 		List<SearchParameterValue> list1  = new ArrayList<SearchParameterValue>();
165 		SearchParameterValue v1 = new SearchParameterValue();
166 		v1.setValue("value-1");
167 		list1.add(v1);
168 		SearchParameterValue v2 = new SearchParameterValue();
169 		v2.setValue("value-2");
170 		list1.add(v2);
171 
172 		SearchParameter sp = new SearchParameter();
173 		sp.setKey("key-1");
174 		sp.setValues(list1);
175 
176 		List<SearchParameter> list2 = Arrays.asList(new SearchParameter[] {sp});
177 		
178 		MembershipQuery mq = new MembershipQuery();
179 		mq.setSearchTypeKey("searchKey1");
180 		mq.setSearchParameters(list2);
181 		mq = dao.create(mq);
182 		
183 		return mq;
184 	}
185 	
186 }