|
15 | 15 | */
|
16 | 16 | package org.sakaiproject.conversations.impl;
|
17 | 17 |
|
18 |
| -import org.junit.Assume; |
| 18 | +import org.sakaiproject.archive.api.ArchiveService; |
19 | 19 | import org.sakaiproject.authz.api.AuthzGroup;
|
20 | 20 | import org.sakaiproject.authz.api.AuthzGroupService;
|
21 | 21 | import org.sakaiproject.authz.api.SecurityService;
|
|
56 | 56 | import org.sakaiproject.user.api.UserDirectoryService;
|
57 | 57 | import org.sakaiproject.user.api.UserNotDefinedException;
|
58 | 58 | import org.sakaiproject.util.ResourceLoader;
|
| 59 | +import org.sakaiproject.util.Xml; |
59 | 60 |
|
60 | 61 | import org.springframework.beans.factory.annotation.Autowired;
|
61 | 62 | import org.springframework.test.context.ContextConfiguration;
|
|
76 | 77 | import java.util.Map;
|
77 | 78 | import java.util.Optional;
|
78 | 79 | import java.util.Set;
|
| 80 | +import java.util.Stack; |
79 | 81 | import java.util.stream.Collectors;
|
80 | 82 | import java.time.Instant;
|
81 | 83 | import java.time.temporal.ChronoUnit;
|
82 | 84 |
|
83 |
| -import static org.mockito.Mockito.*; |
84 |
| - |
| 85 | +import org.w3c.dom.Document; |
| 86 | +import org.w3c.dom.Element; |
| 87 | +import org.w3c.dom.NodeList; |
85 | 88 |
|
86 | 89 | import lombok.extern.slf4j.Slf4j;
|
87 | 90 |
|
| 91 | +import static org.mockito.Mockito.*; |
88 | 92 | import static org.junit.Assert.*;
|
| 93 | +import org.junit.Assume; |
89 | 94 | import org.junit.Before;
|
90 | 95 | import org.junit.Test;
|
91 | 96 | import org.junit.runner.RunWith;
|
@@ -1968,6 +1973,163 @@ public void grading() {
|
1968 | 1973 | assertNull(savedBean.gradingItemId);
|
1969 | 1974 | }
|
1970 | 1975 |
|
| 1976 | + @Test |
| 1977 | + public void archive() { |
| 1978 | + |
| 1979 | + switchToInstructor(null); |
| 1980 | + |
| 1981 | + String title1 = "Topic 1"; |
| 1982 | + TopicTransferBean topic1 = new TopicTransferBean(); |
| 1983 | + topic1.aboutReference = site1Ref; |
| 1984 | + topic1.title = title1; |
| 1985 | + topic1.message = "<strong>Something about topic1</strong>"; |
| 1986 | + topic1.siteId = site1Id; |
| 1987 | + topic1 = saveTopic(topic1); |
| 1988 | + |
| 1989 | + String title2 = "Topic 2"; |
| 1990 | + TopicTransferBean topic2 = new TopicTransferBean(); |
| 1991 | + topic2.aboutReference = site1Ref; |
| 1992 | + topic2.title = title2; |
| 1993 | + topic2.siteId = site1Id; |
| 1994 | + topic2 = saveTopic(topic2); |
| 1995 | + |
| 1996 | + String title3 = "Topic 3"; |
| 1997 | + TopicTransferBean topic3 = new TopicTransferBean(); |
| 1998 | + topic3.aboutReference = site1Ref; |
| 1999 | + topic3.title = title3; |
| 2000 | + topic3.siteId = site1Id; |
| 2001 | + topic3 = saveTopic(topic3); |
| 2002 | + |
| 2003 | + String title4 = "Topic 4"; |
| 2004 | + TopicTransferBean topic4 = new TopicTransferBean(); |
| 2005 | + topic4.aboutReference = site1Ref; |
| 2006 | + topic4.title = title4; |
| 2007 | + topic4.siteId = site1Id; |
| 2008 | + topic4 = saveTopic(topic4); |
| 2009 | + |
| 2010 | + TopicTransferBean[] topicBeans = new TopicTransferBean[] { topic1, topic2, topic3, topic4 }; |
| 2011 | + |
| 2012 | + Document doc = Xml.createDocument(); |
| 2013 | + Stack<Element> stack = new Stack<>(); |
| 2014 | + |
| 2015 | + Element root = doc.createElement("archive"); |
| 2016 | + doc.appendChild(root); |
| 2017 | + root.setAttribute("source", site1Id); |
| 2018 | + root.setAttribute("xmlns:sakai", ArchiveService.SAKAI_ARCHIVE_NS); |
| 2019 | + root.setAttribute("xmlns:CHEF", ArchiveService.SAKAI_ARCHIVE_NS.concat("CHEF")); |
| 2020 | + root.setAttribute("xmlns:DAV", ArchiveService.SAKAI_ARCHIVE_NS.concat("DAV")); |
| 2021 | + stack.push(root); |
| 2022 | + |
| 2023 | + assertEquals(1, stack.size()); |
| 2024 | + |
| 2025 | + String results = conversationsService.archive(site1Id, doc, stack, "", null); |
| 2026 | + |
| 2027 | + assertEquals(2, stack.size()); |
| 2028 | + |
| 2029 | + NodeList conversationsNode = root.getElementsByTagName(conversationsService.getLabel()); |
| 2030 | + assertEquals(1, conversationsNode.getLength()); |
| 2031 | + |
| 2032 | + NodeList topicsNode = ((Element) conversationsNode.item(0)).getElementsByTagName("topics"); |
| 2033 | + assertEquals(1, topicsNode.getLength()); |
| 2034 | + |
| 2035 | + NodeList topicNodes = ((Element) topicsNode.item(0)).getElementsByTagName("topic"); |
| 2036 | + assertEquals(topicBeans.length, topicNodes.getLength()); |
| 2037 | + |
| 2038 | + for (int i = 0; i < topicNodes.getLength(); i++) { |
| 2039 | + Element topicEl = (Element) topicNodes.item(i); |
| 2040 | + assertEquals(topicBeans[i].title, topicEl.getAttribute("title")); |
| 2041 | + assertEquals(topicBeans[i].type, topicEl.getAttribute("type")); |
| 2042 | + assertEquals(topicBeans[i].anonymous, Boolean.parseBoolean(topicEl.getAttribute("anonymous"))); |
| 2043 | + assertEquals(topicBeans[i].allowAnonymousPosts, Boolean.parseBoolean(topicEl.getAttribute("allow-anonymous-posts"))); |
| 2044 | + assertEquals(topicBeans[i].pinned, Boolean.parseBoolean(topicEl.getAttribute("pinned"))); |
| 2045 | + assertEquals(topicBeans[i].draft, Boolean.parseBoolean(topicEl.getAttribute("draft"))); |
| 2046 | + assertEquals(topicBeans[i].visibility, topicEl.getAttribute("visibility")); |
| 2047 | + assertEquals(topicBeans[i].creator, topicEl.getAttribute("creator")); |
| 2048 | + assertEquals(topicBeans[i].created.getEpochSecond(), Long.parseLong(topicEl.getAttribute("created"))); |
| 2049 | + |
| 2050 | + NodeList messageNodes = topicEl.getElementsByTagName("message"); |
| 2051 | + assertEquals(1, messageNodes.getLength()); |
| 2052 | + |
| 2053 | + assertEquals(topicBeans[i].message, ((Element) messageNodes.item(0)).getFirstChild().getNodeValue()); |
| 2054 | + } |
| 2055 | + } |
| 2056 | + |
| 2057 | + @Test |
| 2058 | + public void merge() { |
| 2059 | + |
| 2060 | + Document doc = Xml.readDocumentFromStream(this.getClass().getResourceAsStream("/archive/conversations.xml")); |
| 2061 | + |
| 2062 | + Element root = doc.getDocumentElement(); |
| 2063 | + |
| 2064 | + String fromSite = root.getAttribute("source"); |
| 2065 | + String toSite = "my-new-site"; |
| 2066 | + |
| 2067 | + String toSiteRef = "/site/" + toSite; |
| 2068 | + switchToInstructor(toSiteRef); |
| 2069 | + |
| 2070 | + when(siteService.siteReference(toSite)).thenReturn(toSiteRef); |
| 2071 | + |
| 2072 | + Element conversationsElement = doc.createElement("not-conversations"); |
| 2073 | + |
| 2074 | + conversationsService.merge(toSite, conversationsElement, "", fromSite, null, null, null); |
| 2075 | + |
| 2076 | + assertEquals("Invalid xml document", conversationsService.merge(toSite, conversationsElement, "", fromSite, null, null, null)); |
| 2077 | + |
| 2078 | + conversationsElement = (Element) root.getElementsByTagName(conversationsService.getLabel()).item(0); |
| 2079 | + |
| 2080 | + conversationsService.merge(toSite, conversationsElement, "", fromSite, null, null, null); |
| 2081 | + |
| 2082 | + NodeList topicNodes = ((Element) conversationsElement.getElementsByTagName("topics").item(0)).getElementsByTagName("topic"); |
| 2083 | + |
| 2084 | + List<ConversationsTopic> topics = topicRepository.findBySiteId(toSite); |
| 2085 | + |
| 2086 | + assertEquals(topics.size(), topicNodes.getLength()); |
| 2087 | + |
| 2088 | + for (int i = 0; i < topicNodes.getLength(); i++) { |
| 2089 | + |
| 2090 | + Element topicEl = (Element) topicNodes.item(i); |
| 2091 | + |
| 2092 | + String title = topicEl.getAttribute("title"); |
| 2093 | + Optional<ConversationsTopic> optTopic = topics.stream().filter(t -> t.getTitle().equals(title)).findAny(); |
| 2094 | + assertTrue(optTopic.isPresent()); |
| 2095 | + |
| 2096 | + ConversationsTopic topic = optTopic.get(); |
| 2097 | + |
| 2098 | + assertEquals(topic.getType().name(), topicEl.getAttribute("type")); |
| 2099 | + assertEquals(topic.getPinned(), Boolean.parseBoolean(topicEl.getAttribute("pinned"))); |
| 2100 | + assertEquals(topic.getAnonymous(), Boolean.parseBoolean(topicEl.getAttribute("anonymous"))); |
| 2101 | + assertEquals(topic.getDraft(), Boolean.parseBoolean(topicEl.getAttribute("draft"))); |
| 2102 | + assertEquals(topic.getMustPostBeforeViewing(), Boolean.parseBoolean(topicEl.getAttribute("post-before-viewing"))); |
| 2103 | + |
| 2104 | + NodeList messageNodes = topicEl.getElementsByTagName("message"); |
| 2105 | + assertEquals(1, messageNodes.getLength()); |
| 2106 | + |
| 2107 | + assertEquals(topic.getMessage(), messageNodes.item(0).getFirstChild().getNodeValue()); |
| 2108 | + } |
| 2109 | + |
| 2110 | + Set<String> oldTitles = topics.stream().map(ConversationsTopic::getTitle).collect(Collectors.toSet()); |
| 2111 | + |
| 2112 | + // Now let's try and merge this set of rubrics. It has one with a different title, but the |
| 2113 | + // rest the same, so we should end up with only one rubric being added. |
| 2114 | + Document doc2 = Xml.readDocumentFromStream(this.getClass().getResourceAsStream("/archive/conversations2.xml")); |
| 2115 | + |
| 2116 | + Element root2 = doc2.getDocumentElement(); |
| 2117 | + |
| 2118 | + conversationsElement = (Element) root2.getElementsByTagName(conversationsService.getLabel()).item(0); |
| 2119 | + |
| 2120 | + conversationsService.merge(toSite, conversationsElement, "", fromSite, null, null, null); |
| 2121 | + |
| 2122 | + String extraTitle = "Smurfs"; |
| 2123 | + |
| 2124 | + assertEquals(topics.size() + 1, topicRepository.findBySiteId(toSite).size()); |
| 2125 | + |
| 2126 | + Set<String> newTitles = topicRepository.findBySiteId(toSite) |
| 2127 | + .stream().map(ConversationsTopic::getTitle).collect(Collectors.toSet()); |
| 2128 | + |
| 2129 | + assertFalse(oldTitles.contains(extraTitle)); |
| 2130 | + assertTrue(newTitles.contains(extraTitle)); |
| 2131 | + } |
| 2132 | + |
1971 | 2133 | private TopicTransferBean saveTopic(TopicTransferBean topicBean) {
|
1972 | 2134 |
|
1973 | 2135 | try {
|
|
0 commit comments