Skip to content

Commit

Permalink
increased tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
car031 committed Nov 8, 2024
1 parent 56d6875 commit 76a1ee5
Show file tree
Hide file tree
Showing 44 changed files with 2,159 additions and 726 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ public void sendAsync(EMail email, String templateName, Map<String, Object> dict
public void send(EMail email, String templateName, Map<String, Object> dictionary)
throws MessagingException, AutomationException {
if (!RunLevel.current().aspectEnabled("sendingMessages")) {
log.error("Aspect not enabled");
throw new MessagingException("Aspect sendingMessages not enabled");
log.error("Aspect sendingMessages not enabled");
return;
}

MessageTemplateDAO templateDao = (MessageTemplateDAO) Context.get().getBean(MessageTemplateDAO.class);
Expand Down Expand Up @@ -306,6 +306,11 @@ public void sendAsync(EMail email) {
* @throws MessagingException raised if the email cannot be sent
*/
public void send(EMail email) throws MessagingException {
if (!RunLevel.current().aspectEnabled("sendingMessages")) {
log.error("Aspect sendingMessages not enabled");
return;
}

cleanAuthorAddress(email);

Session session = newMailSession();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ public Template clone(long id, String cloneName) throws PersistenceException {
clonedTemplate.setAttributes(originalTemplate.getAttributes().entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
store(clonedTemplate);
jdbcUpdate("insert into ld_template_acl(ld_templateid, ld_groupid, ld_write) select " + clonedTemplate.getId()
+ ", ld_groupid, ld_write from ld_template_acl where ld_templateid=" + id);
jdbcUpdate("insert into ld_template_acl(ld_templateid, ld_groupid, ld_read, ld_write) select " + clonedTemplate.getId()
+ ", ld_groupid, ld_read, ld_write from ld_template_acl where ld_templateid=" + id);
initialize(clonedTemplate);
return clonedTemplate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public class StatsCollector extends Task {

private static String productName = "LogicalDOC Community";

private boolean uploadStatistics = true;

private List<NameValuePair> statistics = new ArrayList<>();

public StatsCollector() {
super(NAME);
log = LoggerFactory.getLogger(StatsCollector.class);
Expand Down Expand Up @@ -240,48 +244,48 @@ protected void runTask() throws TaskException {
log.debug("Package collected statistics");

// Prepare the post parameters
List<NameValuePair> postParams = new ArrayList<>();
statistics.clear();

// Add all statistics as parameters
postParams.add(new BasicNameValuePair("id", StringUtils.defaultString(id)));
postParams.add(new BasicNameValuePair("userno", StringUtils.defaultString(userno)));
postParams.add(new BasicNameValuePair("sid", StringUtils.defaultString(sid)));

postParams.add(new BasicNameValuePair("product_release", StringUtils.defaultString(release)));
postParams.add(new BasicNameValuePair("email", StringUtils.defaultString(email)));
postParams.add(new BasicNameValuePair("product", StringUtils.defaultString(StatsCollector.product)));
postParams
statistics.add(new BasicNameValuePair("id", StringUtils.defaultString(id)));
statistics.add(new BasicNameValuePair("userno", StringUtils.defaultString(userno)));
statistics.add(new BasicNameValuePair("sid", StringUtils.defaultString(sid)));

statistics.add(new BasicNameValuePair("product_release", StringUtils.defaultString(release)));
statistics.add(new BasicNameValuePair("email", StringUtils.defaultString(email)));
statistics.add(new BasicNameValuePair("product", StringUtils.defaultString(StatsCollector.product)));
statistics
.add(new BasicNameValuePair("product_name", StringUtils.defaultString(StatsCollector.productName)));

postParams.add(new BasicNameValuePair("java_version", StringUtils.defaultString(javaversion)));
postParams.add(new BasicNameValuePair("java_vendor", StringUtils.defaultString(javavendor)));
postParams.add(new BasicNameValuePair("java_arch", StringUtils.defaultString(javaarch)));
postParams.add(new BasicNameValuePair("dbms", StringUtils.defaultString(dbms)));
statistics.add(new BasicNameValuePair("java_version", StringUtils.defaultString(javaversion)));
statistics.add(new BasicNameValuePair("java_vendor", StringUtils.defaultString(javavendor)));
statistics.add(new BasicNameValuePair("java_arch", StringUtils.defaultString(javaarch)));
statistics.add(new BasicNameValuePair("dbms", StringUtils.defaultString(dbms)));

postParams.add(new BasicNameValuePair("os_name", StringUtils.defaultString(osname)));
postParams.add(new BasicNameValuePair("os_version", StringUtils.defaultString(osversion)));
postParams.add(new BasicNameValuePair("file_encoding", StringUtils.defaultString(fileencoding)));
statistics.add(new BasicNameValuePair("os_name", StringUtils.defaultString(osname)));
statistics.add(new BasicNameValuePair("os_version", StringUtils.defaultString(osversion)));
statistics.add(new BasicNameValuePair("file_encoding", StringUtils.defaultString(fileencoding)));

postParams.add(new BasicNameValuePair("user_language", StringUtils.defaultString(userlanguage)));
postParams.add(new BasicNameValuePair("user_country", StringUtils.defaultString(usercountry)));
statistics.add(new BasicNameValuePair("user_language", StringUtils.defaultString(userlanguage)));
statistics.add(new BasicNameValuePair("user_country", StringUtils.defaultString(usercountry)));

// Sizing
postParams.add(new BasicNameValuePair("users", Integer.toString(users)));
postParams.add(new BasicNameValuePair("guests", Integer.toString(guests)));
postParams.add(new BasicNameValuePair("groups", Integer.toString(groups)));
postParams.add(new BasicNameValuePair("docs", Long.toString(totaldocs)));
postParams.add(new BasicNameValuePair("pages", Long.toString(totalpages)));

postParams.add(new BasicNameValuePair("archived_docs", Long.toString(archiveddocs)));
postParams.add(new BasicNameValuePair("folders", Long.toString(withdocs + empty + deletedfolders)));
postParams.add(new BasicNameValuePair("tags", Long.toString(tags)));
postParams.add(new BasicNameValuePair("versions", Long.toString(versions)));
postParams.add(new BasicNameValuePair("histories", Long.toString(histories)));
postParams.add(new BasicNameValuePair("user_histories", Long.toString(userHistories)));
postParams.add(new BasicNameValuePair("votes", Long.toString(votes)));
postParams.add(new BasicNameValuePair("wscalls", Long.toString(wsCalls)));

collectFeatureUsageStats(postParams);
statistics.add(new BasicNameValuePair("users", Integer.toString(users)));
statistics.add(new BasicNameValuePair("guests", Integer.toString(guests)));
statistics.add(new BasicNameValuePair("groups", Integer.toString(groups)));
statistics.add(new BasicNameValuePair("docs", Long.toString(totaldocs)));
statistics.add(new BasicNameValuePair("pages", Long.toString(totalpages)));

statistics.add(new BasicNameValuePair("archived_docs", Long.toString(archiveddocs)));
statistics.add(new BasicNameValuePair("folders", Long.toString(withdocs + empty + deletedfolders)));
statistics.add(new BasicNameValuePair("tags", Long.toString(tags)));
statistics.add(new BasicNameValuePair("versions", Long.toString(versions)));
statistics.add(new BasicNameValuePair("histories", Long.toString(histories)));
statistics.add(new BasicNameValuePair("user_histories", Long.toString(userHistories)));
statistics.add(new BasicNameValuePair("votes", Long.toString(votes)));
statistics.add(new BasicNameValuePair("wscalls", Long.toString(wsCalls)));

collectFeatureUsageStats(statistics);
next();
if (interruptRequested)
return;
Expand All @@ -290,29 +294,29 @@ protected void runTask() throws TaskException {
* General usage
*/
Date lastLogin = findLastLogin();
postParams.add(new BasicNameValuePair("last_login", formatDate(lastLogin)));
statistics.add(new BasicNameValuePair("last_login", formatDate(lastLogin)));

Date lastCreation = findLastCreation();
postParams.add(new BasicNameValuePair("last_creation", formatDate(lastCreation)));
statistics.add(new BasicNameValuePair("last_creation", formatDate(lastCreation)));

/*
* Quotas
*/
postParams.add(new BasicNameValuePair("docdir", Long.toString(docdir)));
postParams.add(new BasicNameValuePair("trash", Long.toString(trash)));
postParams.add(new BasicNameValuePair("indexdir", Long.toString(indexdir)));
postParams.add(new BasicNameValuePair("quota",
statistics.add(new BasicNameValuePair("docdir", Long.toString(docdir)));
statistics.add(new BasicNameValuePair("trash", Long.toString(trash)));
statistics.add(new BasicNameValuePair("indexdir", Long.toString(indexdir)));
statistics.add(new BasicNameValuePair("quota",
Long.toString(docdir + indexdir + userdir + importdir + exportdir + plugindir + dbdir + logdir)));

/*
* Registration
*/
postParams.add(new BasicNameValuePair("reg_name", regName != null ? regName : ""));
postParams.add(new BasicNameValuePair("reg_email", regEmail != null ? regEmail : ""));
postParams.add(new BasicNameValuePair("reg_organization", regOrganization != null ? regOrganization : ""));
postParams.add(new BasicNameValuePair("reg_website", regWebsite != null ? regWebsite : ""));
statistics.add(new BasicNameValuePair("reg_name", regName != null ? regName : ""));
statistics.add(new BasicNameValuePair("reg_email", regEmail != null ? regEmail : ""));
statistics.add(new BasicNameValuePair("reg_organization", regOrganization != null ? regOrganization : ""));
statistics.add(new BasicNameValuePair("reg_website", regWebsite != null ? regWebsite : ""));

postStatistics(postParams);
uploadStatistics(statistics);

next();
} catch (PersistenceException e) {
Expand All @@ -325,14 +329,16 @@ private String formatDate(Date lastLogin) {
return lastLogin != null ? isoDf.format(lastLogin) : "";
}

private void postStatistics(List<NameValuePair> postParams) {
private void uploadStatistics(List<NameValuePair> postParams) {
try {
// Execute request
try (CloseableHttpClient httpClient = HttpUtil.getNotValidatingClient(60)) {
HttpPost post = new HttpPost("http://stat.logicaldoc.com/stats/collect");
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams, StandardCharsets.UTF_8);
post.setEntity(entity);
httpClient.execute(post, new StringHttpClientResponseHandler());

if (uploadStatistics)
httpClient.execute(post, new StringHttpClientResponseHandler());
}
log.info("Statistics packaged");
} catch (IOException e) {
Expand Down Expand Up @@ -851,4 +857,12 @@ public static void setProduct(String product) {
public static void setProductName(String productName) {
StatsCollector.productName = productName;
}

void setUploadStatistics(boolean uploadStatistics) {
this.uploadStatistics = uploadStatistics;
}

List<NameValuePair> getStatistics() {
return statistics;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -38,7 +37,7 @@
import com.talanlabs.avatargenerator.IdenticonAvatar;

public class CoreWorkbench {

/**
* Test sending e-mail with attachments
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void testLock() {
assertNotNull(doc);
testSubject.lock(doc.getId(), "admin");
doc = testSubject.findById(1);
assertEquals(2, doc.getStatus());
assertEquals(1, doc.getStatus());
assertEquals(1L, doc.getLockUserId().longValue());

// double lock with same user just to check that no exceptions are
Expand All @@ -200,7 +200,7 @@ public void testUnlock() {
testSubject.lock(1L, "admin");

Document doc = testSubject.findById(1L);
assertEquals(2, doc.getStatus());
assertEquals(1, doc.getStatus());
assertEquals(1L, doc.getLockUserId().longValue());

// Locked by a different user
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.logicaldoc.core.dashlet;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import org.junit.Before;
import org.junit.Test;
import org.springframework.security.core.context.SecurityContextHolder;

import com.logicaldoc.core.AbstractCoreTestCase;
import com.logicaldoc.core.PersistenceException;
import com.logicaldoc.core.security.Client;
import com.logicaldoc.core.security.Device;
import com.logicaldoc.core.security.Session;
import com.logicaldoc.core.security.SessionManager;
import com.logicaldoc.core.security.spring.LDAuthenticationToken;
import com.logicaldoc.core.security.spring.LDSecurityContextRepository;
import com.logicaldoc.core.security.user.User;
import com.logicaldoc.core.security.user.UserDAO;
import com.logicaldoc.util.Context;
import com.logicaldoc.util.io.FileUtil;
import com.logicaldoc.util.plugin.PluginException;
import com.logicaldoc.util.servlet.MockServletRequest;
import com.logicaldoc.util.servlet.MockServletResponse;
import com.logicaldoc.util.servlet.MockServletSession;

/**
* Test case for {@link DashletContent}
*
* @author Marco Meschieri - LogicalDOC
* @since 9.0.1
*/
public class DashletContentTest extends AbstractCoreTestCase {

// Instance under test
private DashletContent testSubject = new DashletContent();

protected Session session;

protected MockServletSession servletSession = new MockServletSession();

protected final File responseFile = new File("target/documents.xml");

@Before
public void setUp() throws IOException, SQLException, PluginException {
super.setUp();
FileUtil.delete(responseFile);
prepareSession("admin", "admin");
}

@Override
public void tearDown() throws SQLException {
super.tearDown();
FileUtil.delete(responseFile);
}

@Test
public void testService() throws IOException {
MockServletRequest mockRequest = new MockServletRequest(servletSession);
mockRequest.setParameter("locale", "en");
mockRequest.setParameter("dashletId", "21");

MockServletResponse response = new MockServletResponse(responseFile);
testSubject.service(mockRequest, response);
response.flushBuffer();
assertTrue(FileUtil.readFile(responseFile).contains("<id>1</id>"));

FileUtil.delete(responseFile);
mockRequest.setParameter("dashletId", "22");
response = new MockServletResponse(responseFile);
testSubject.service(mockRequest, response);
response.flushBuffer();
assertTrue(FileUtil.readFile(responseFile).contains("<id>1</id>"));

FileUtil.delete(responseFile);
mockRequest.setParameter("dashletId", "23");
response = new MockServletResponse(responseFile);
testSubject.service(mockRequest, response);
response.flushBuffer();
System.out.println(FileUtil.readFile(responseFile));
assertEquals("sample content", FileUtil.readFile(responseFile).trim());

FileUtil.delete(responseFile);
mockRequest.setParameter("dashletId", "6");
response = new MockServletResponse(responseFile);
testSubject.service(mockRequest, response);
response.flushBuffer();
assertTrue(FileUtil.readFile(responseFile).contains("<![CDATA[message for note 1]]>"));

FileUtil.delete(responseFile);
mockRequest.setParameter("dashletId", "9");
response = new MockServletResponse(responseFile);
testSubject.service(mockRequest, response);
response.flushBuffer();
System.out.println(FileUtil.readFile(responseFile));
assertEquals("", FileUtil.readFile(responseFile));

}

protected void prepareSession(String username, String password) throws PersistenceException {
UserDAO userDao = (UserDAO) Context.get().getBean(UserDAO.class);

Client client = new Client("xyz", "192.168.2.231", "ghost");
Device device = new Device();
device.setBrowser("Firefox");
device.setBrowserVersion("18");
device.setOperativeSystem("Windows");
client.setDevice(device);
session = SessionManager.get().newSession(username, password, null, client);
if (session != null) {
User user = userDao.findByUsernameIgnoreCase(username);
userDao.initialize(user);
LDAuthenticationToken token = new LDAuthenticationToken(username);
token.setSid(session.getSid());
SecurityContextHolder.getContext().setAuthentication(token);
LDSecurityContextRepository.bindServletSession(session.getSid(), servletSession);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@
public class HibernateDashletDAOTest extends AbstractCoreTestCase {

// Instance under test
private DashletDAO dao;
private DashletDAO testSubject;

@Before
public void setUp() throws IOException, SQLException, PluginException {
super.setUp();
// Retrieve the instance under test from spring context. Make sure that
// it is an HibernateDashletDAO
dao = (DashletDAO) context.getBean("DashletDAO");
testSubject = (DashletDAO) context.getBean("DashletDAO");
}

@Test
public void testFindByName() throws PersistenceException {
Dashlet dashlet = dao.findByName("checkout", 1L);
Dashlet dashlet = testSubject.findByName("checkout", 1L);
assertNotNull(dashlet);
assertEquals(1L, dashlet.getId());

dashlet = dao.findByName("xxxx", 1L);
dashlet = testSubject.findByName("xxxx", 1L);
assertNull(dashlet);
}
}
Loading

0 comments on commit 76a1ee5

Please sign in to comment.