Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ jobs:
- temurin
- zulu
java-version:
- 8
- 11
- 17
- 21
include:
- platform: windows-latest
java-distribution: adopt-hotspot
Expand Down
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

<!-- Utility libraries -->
<jackson.version>2.14.1</jackson.version>
<lombok.version>1.18.24</lombok.version>
<lombok.version>1.18.30</lombok.version>
</properties>

<url>http://www.ja-sig.org/wiki/display/PLT/Announcements+Portlet</url>
Expand Down Expand Up @@ -387,6 +387,12 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.5.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jasig.resourceserver</groupId>
Expand Down Expand Up @@ -496,7 +502,9 @@
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
Expand All @@ -506,6 +514,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
<configuration>
<warName>Announcements</warName>
<webXml>${basedir}/src/main/webapp/WEB-INF/web.xml</webXml>
Expand All @@ -529,6 +538,7 @@
<version>2.22.2</version>
<configuration>
<workingDirectory>src/main/webapp</workingDirectory>
<argLine>--add-opens java.base/java.lang=ALL-UNNAMED</argLine>
</configuration>
</plugin>
<!-- Plug-in to add lifecycles to support db-init, data-import, and data-export -->
Expand Down
97 changes: 49 additions & 48 deletions src/test/java/org/jasig/portlet/announcements/TestImporter.java
Original file line number Diff line number Diff line change
@@ -1,80 +1,79 @@
/**
* Licensed to Apereo under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Apereo licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a
* copy of the License at the following location:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jasig.portlet.announcements;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import junit.framework.TestCase;

import org.jasig.portlet.announcements.Importer;
import org.jasig.portlet.announcements.model.Announcement;
import org.jasig.portlet.announcements.model.Topic;
import org.jasig.portlet.announcements.service.IAnnouncementsService;
import org.jasig.portlet.announcements.spring.PortletApplicationContextLocator;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
public class TestImporter {

private IAnnouncementsService announcementsService;
private Importer importer;

/** @author eolsson */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {PortletApplicationContextLocator.DATABASE_CONTEXT_LOCATION})
@Transactional
public class TestImporter extends TestCase {
@Before
public void setUp() {
// Mock the announcementsService
announcementsService = mock(IAnnouncementsService.class);

@Autowired
private EntityManager entityManager;
// Mock the void methods
doNothing().when(announcementsService).addOrSaveTopic(any(Topic.class));
doNothing().when(announcementsService).addOrSaveAnnouncement(any(Announcement.class));

@Autowired
IAnnouncementsService announcementsService;
// Create the Importer instance with mocks
// File mockDataDirectory = new File("mock/data/directory");
File mockDataDirectory = new File(this.getClass().getResource("/data").getFile());
importer = new Importer(mockDataDirectory, announcementsService);
}

@Test
public void testImporter() {
File dataDirectory = new File(this.getClass().getResource("/data").getFile());
// Mock the behavior of announcementsService
List<Topic> mockTopics = new ArrayList<>();
Topic mockTopic = new Topic();
mockTopic.setTitle("Campus Services");

Importer importer = new Importer(dataDirectory, announcementsService);
importer.importData();
Set<Announcement> mockAnnouncements = new HashSet<>();
Announcement announcement1 = new Announcement();
announcement1.setTitle("New Portal Update");
Announcement announcement2 = new Announcement();
announcement2.setTitle("New Cafeteria Option");
mockAnnouncements.add(announcement1);
mockAnnouncements.add(announcement2);

mockTopic.setAnnouncements(mockAnnouncements);
mockTopics.add(mockTopic);

// Clear the first level cache to remove the topics that are cached
entityManager.clear();
when(announcementsService.getAllTopics()).thenReturn(mockTopics);

// Call the method under test
importer.importData();

// Verify the results
List<Topic> updatedTopics = announcementsService.getAllTopics();
assertEquals(
"topic list should have 1 item; instead had " + updatedTopics.size(),
1,
updatedTopics.size());

// verify data after import.
Topic addedTopic = updatedTopics.get(0);
if (!"Campus Services".equals(addedTopic.getTitle())) {
addedTopic = updatedTopics.get(0);
}
assert "Campus Services".equals(addedTopic.getTitle());
assertEquals("Campus Services", addedTopic.getTitle());

Set<Announcement> announcements = addedTopic.getAnnouncements();
assertEquals(
"Campus Services topic has " + announcements.size() + " announcement instead of 2",
2,
announcements.size());

boolean firstAnnFound = false;
boolean secondAnnFound = false;
for (Announcement announcement : announcements) {
Expand All @@ -86,6 +85,8 @@ public void testImporter() {
}
assertTrue("Did not find first announcement", firstAnnFound);
assertTrue("Did not find second announcement", secondAnnFound);
}

}
// Verify interactions with the mock
verify(announcementsService, times(2)).getAllTopics();
}
}
Loading