Skip to content
Merged
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
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ jobs:
image: greenmail/standalone:2.1.2
ports:
- 3025:3025
- 3110:3110

steps:
- name: Checkout code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public AbstractMailServerResourceImpl(
@Override
public Response getMailServerSmtp() {
final MailServerSmtpModel smtpModel = mailServerService.getMailServerSmtp();

if (smtpModel == null) {
return Response.noContent().build();
}

return Response.ok(smtpModel).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ void testGetApplicationLinks() throws Exception {
void testSetApplicationLinks() throws Exception {
final ApplicationLinkModel applicationLinkModel = getExampleModel();

final HttpResponse<String> applicationLinksResponse = HttpRequestHelper.builder(BootstrAPI.APPLICATION_LINKS + "?" + "ignoreSetupErrors")
final HttpResponse<String> applicationLinksResponse = HttpRequestHelper.builder(BootstrAPI.APPLICATION_LINKS + "?" + "ignore-setup-errors=true")
.request(HttpMethod.PUT, Collections.singletonList(applicationLinkModel));
assertEquals(Response.Status.OK.getStatusCode(), applicationLinksResponse.statusCode());
assertEquals(Response.Status.OK.getStatusCode(), applicationLinksResponse.statusCode(), applicationLinksResponse.body());

final List<ApplicationLinkModel> applicationLinkModels = objectMapper.readValue(applicationLinksResponse.body(), new TypeReference<List<ApplicationLinkModel>>(){});
assertEquals(1, applicationLinkModels.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.deftdevs.bootstrapi.commons.constants.BootstrAPI;
import com.deftdevs.bootstrapi.commons.model.MailServerPopModel;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

import javax.ws.rs.HttpMethod;
import javax.ws.rs.core.Response;
Expand All @@ -12,30 +15,41 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public abstract class AbstractMailServerPopResourceFuncTest {

private final ObjectMapper objectMapper = new ObjectMapper();

@Test
void testGetMailServerPop() throws Exception {
@Order(1)
protected void testGetMailServerPopNotConfigured() throws Exception {
final HttpResponse<String> mailServerPopResponse = HttpRequestHelper.builder(BootstrAPI.MAIL_SERVER + "/" + BootstrAPI.MAIL_SERVER_POP)
.request();
assertEquals(Response.Status.OK.getStatusCode(), mailServerPopResponse.statusCode());

final MailServerPopModel mailServerPopModel = objectMapper.readValue(mailServerPopResponse.body(), MailServerPopModel.class);
assertNotNull(mailServerPopModel);
assertEquals(Response.Status.NO_CONTENT.getStatusCode(), mailServerPopResponse.statusCode());
}

@Test
@Order(2)
void testSetMailServerPop() throws Exception {
final HttpResponse<String> mailServerPopResponse = HttpRequestHelper.builder(BootstrAPI.MAIL_SERVER + "/" + BootstrAPI.MAIL_SERVER_POP)
.request(HttpMethod.PUT, getExampleModel());
assertEquals(Response.Status.OK.getStatusCode(), mailServerPopResponse.statusCode());
assertEquals(Response.Status.OK.getStatusCode(), mailServerPopResponse.statusCode(), mailServerPopResponse.body());

final MailServerPopModel mailServerPopModel = objectMapper.readValue(mailServerPopResponse.body(), MailServerPopModel.class);
assertMailServerModelAgainstExample(mailServerPopModel);
}

@Test
@Order(3)
void testGetMailServerPop() throws Exception {
final HttpResponse<String> mailServerPopResponse = HttpRequestHelper.builder(BootstrAPI.MAIL_SERVER + "/" + BootstrAPI.MAIL_SERVER_POP)
.request();
assertEquals(Response.Status.OK.getStatusCode(), mailServerPopResponse.statusCode(), mailServerPopResponse.body());

final MailServerPopModel mailServerPopModel = objectMapper.readValue(mailServerPopResponse.body(), MailServerPopModel.class);
assertNotNull(mailServerPopModel);
}

@Test
public void testGetMailServerPopUnauthenticated() throws Exception {
final HttpResponse<String> mailServerPopResponse = HttpRequestHelper.builder(BootstrAPI.MAIL_SERVER + "/" + BootstrAPI.MAIL_SERVER_POP)
Expand All @@ -57,7 +71,7 @@ public void testSetMailServerPopUnauthenticated() throws Exception {
}

@Test
void testGetMailServerPopUnauthorized() throws Exception {
public void testGetMailServerPopUnauthorized() throws Exception {
final HttpResponse<String> mailServerPopResponse = HttpRequestHelper.builder(BootstrAPI.MAIL_SERVER + "/" + BootstrAPI.MAIL_SERVER_POP)
.username("user")
.password("user")
Expand All @@ -67,7 +81,7 @@ void testGetMailServerPopUnauthorized() throws Exception {
}

@Test
void testSetMailServerPopUnauthorized() throws Exception {
public void testSetMailServerPopUnauthorized() throws Exception {
final HttpResponse<String> mailServerPopResponse = HttpRequestHelper.builder(BootstrAPI.MAIL_SERVER + "/" + BootstrAPI.MAIL_SERVER_POP)
.username("user")
.password("user")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract class AbstractMailServerSmtpResourceFuncTest {

@Test
@Order(1)
void testGetMailServerSmtpNotConfigured() throws Exception {
protected void testGetMailServerSmtpNotConfigured() throws Exception {
final HttpResponse<String> mailServerSmtpResponse = HttpRequestHelper.builder(BootstrAPI.MAIL_SERVER + "/" + BootstrAPI.MAIL_SERVER_SMTP)
.request();
assertEquals(Response.Status.NO_CONTENT.getStatusCode(), mailServerSmtpResponse.statusCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.atlassian.cache.ManagedCache;
import com.deftdevs.bootstrapi.confluence.model.CacheModel;

import java.util.SortedMap;
import java.util.function.Supplier;

public class CacheModelUtil {

/**
Expand All @@ -16,35 +19,44 @@ public static CacheModel toCacheModel(

return CacheModel.builder()
.name(managedCache.getName())
.currentHeapSizeInByte(managedCache.getStatistics().get(CacheStatisticsKey.HEAP_SIZE).get())
.currentHeapSizeInByte(getStatistic(managedCache, CacheStatisticsKey.HEAP_SIZE))
.effectivenessInPercent(getEffectiveness(managedCache))
.maxObjectCount(managedCache.currentMaxEntries())
.utilisationInPercent(getUtilization(managedCache))
.flushable(managedCache.isFlushable())
.build();
}

private static double getEffectiveness(
private static Long getStatistic(
final ManagedCache cache,
final CacheStatisticsKey key) {

final SortedMap<CacheStatisticsKey, Supplier<Long>> statistics = cache.getStatistics();
final Supplier<Long> supplier = statistics.get(key);
return supplier != null ? supplier.get() : null;
}

private static Double getEffectiveness(
final ManagedCache cache) {

long hit = cache.getStatistics().get(CacheStatisticsKey.HIT_COUNT).get();
long miss = cache.getStatistics().get(CacheStatisticsKey.MISS_COUNT).get();
final Long hit = getStatistic(cache, CacheStatisticsKey.HIT_COUNT);
final Long miss = getStatistic(cache, CacheStatisticsKey.MISS_COUNT);
if (hit == null || miss == null || hit + miss == 0) {
return null;
}
return (double) hit * 100 / (hit + miss);
}


private static Double getUtilization(
final ManagedCache cache) {

// currentMaxEntries can be null so check this first

long objects = cache.getStatistics().get(CacheStatisticsKey.SIZE).get();
Integer size = cache.currentMaxEntries();
final Long objects = getStatistic(cache, CacheStatisticsKey.SIZE);
final Integer size = cache.currentMaxEntries();

if (size != null) {
return (double) objects * 100 / size;
if (objects == null || size == null) {
return null;
}
return null;
return (double) objects * 100 / size;
}

private CacheModelUtil() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
import com.deftdevs.bootstrapi.confluence.model.util.CacheModelUtil;
import com.deftdevs.bootstrapi.confluence.service.api.CachesService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class CachesServiceImpl implements CachesService {

private static final Logger log = LoggerFactory.getLogger(CachesServiceImpl.class);

private final CacheManager cacheManager;

public CachesServiceImpl(
Expand All @@ -23,9 +29,16 @@ public CachesServiceImpl(

@Override
public List<CacheModel> getAllCaches() {
return cacheManager.getManagedCaches().stream()
.map(CacheModelUtil::toCacheModel)
.collect(Collectors.toList());
final Collection<ManagedCache> managedCaches = cacheManager.getManagedCaches();
final List<CacheModel> cacheModels = new ArrayList<>();
for (ManagedCache managedCache : managedCaches) {
try {
cacheModels.add(CacheModelUtil.toCacheModel(managedCache));
} catch (Exception e) {
log.warn("Failed to convert cache '{}': {}", managedCache.getName(), e.getMessage());
}
}
return cacheModels;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.deftdevs.bootstrapi.confluence.service;

import com.atlassian.confluence.jmx.JmxSMTPMailServer;
import com.atlassian.confluence.mail.ConfluencePopMailServer;
import com.atlassian.mail.MailException;
import com.atlassian.mail.MailProtocol;
import com.atlassian.mail.server.MailServerManager;
import com.atlassian.mail.server.PopMailServer;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.mail.server.impl.PopMailServerImpl;
import com.atlassian.mail.server.impl.SMTPMailServerImpl;
import com.deftdevs.bootstrapi.commons.exception.web.BadRequestException;
import com.deftdevs.bootstrapi.commons.model.MailServerPopModel;
import com.deftdevs.bootstrapi.commons.model.MailServerSmtpModel;
Expand Down Expand Up @@ -35,7 +35,7 @@ public MailServerSmtpModel getMailServerSmtp() {
public MailServerSmtpModel setMailServerSmtp(MailServerSmtpModel mailServerSmtpModel) {
final SMTPMailServer smtpMailServer = mailServerManager.isDefaultSMTPMailServerDefined()
? mailServerManager.getDefaultSMTPMailServer()
: new SMTPMailServerImpl.Builder<>().build();
: new JmxSMTPMailServer();

assert smtpMailServer != null;

Expand Down Expand Up @@ -100,7 +100,7 @@ public MailServerPopModel setMailServerPop(

final PopMailServer popMailServer = mailServerManager.getDefaultPopMailServer() != null
? mailServerManager.getDefaultPopMailServer()
: new PopMailServerImpl.Builder<>().build();
: new ConfluencePopMailServer(null, null, null, null, null, null, null, null, null);

assert popMailServer != null;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.atlassian.mail.server;

import com.atlassian.mail.server.impl.PopMailServerImpl;
import com.atlassian.confluence.mail.ConfluencePopMailServer;

public class DefaultTestPopMailServerImpl extends PopMailServerImpl implements DefaultTestPopMailServer {
public class DefaultTestPopMailServerImpl extends ConfluencePopMailServer implements DefaultTestPopMailServer {

public DefaultTestPopMailServerImpl() {
super(
Expand All @@ -14,8 +14,9 @@ public DefaultTestPopMailServerImpl() {
PORT,
USERNAME,
PASSWORD,
TIMEOUT
null
);
setTimeout(TIMEOUT);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.atlassian.mail.server;

import com.atlassian.mail.server.impl.SMTPMailServerImpl;
import com.atlassian.confluence.jmx.JmxSMTPMailServer;

public class DefaultTestSmtpMailServerImpl extends SMTPMailServerImpl implements DefaultTestSmtpMailServer {
public class DefaultTestSmtpMailServerImpl extends JmxSMTPMailServer implements DefaultTestSmtpMailServer {

public DefaultTestSmtpMailServerImpl() {
super(
Expand All @@ -12,6 +12,7 @@ public DefaultTestSmtpMailServerImpl() {
FROM,
PREFIX,
false,
false,
PROTOCOL,
HOST,
PORT,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.atlassian.mail.server;

import com.atlassian.mail.server.impl.PopMailServerImpl;
import com.atlassian.confluence.mail.ConfluencePopMailServer;

public class OtherTestPopMailServerImpl extends PopMailServerImpl implements OtherTestPopMailServer {
public class OtherTestPopMailServerImpl extends ConfluencePopMailServer implements OtherTestPopMailServer {

public OtherTestPopMailServerImpl() {
super(
Expand All @@ -14,10 +14,9 @@ public OtherTestPopMailServerImpl() {
PORT,
USERNAME,
PASSWORD,
TIMEOUT
null
);
setTimeout(TIMEOUT);
}



}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.atlassian.mail.server;

import com.atlassian.mail.server.impl.SMTPMailServerImpl;
import com.atlassian.confluence.jmx.JmxSMTPMailServer;

public class OtherTestSmtpMailServerImpl extends SMTPMailServerImpl implements OtherTestSmtpMailServer {
public class OtherTestSmtpMailServerImpl extends JmxSMTPMailServer implements OtherTestSmtpMailServer {

public OtherTestSmtpMailServerImpl() {
super(
Expand All @@ -12,6 +12,7 @@ public OtherTestSmtpMailServerImpl() {
FROM,
PREFIX,
false,
false,
PROTOCOL,
HOST,
PORT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

import it.com.deftdevs.bootstrapi.commons.rest.AbstractApplicationLinksResourceFuncTest;

// TODO: Make them run by fixing test and then removing 'abstract'
public abstract class ApplicationLinksResourceFuncTest extends AbstractApplicationLinksResourceFuncTest { }
public class ApplicationLinksResourceFuncTest extends AbstractApplicationLinksResourceFuncTest { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package it.com.deftdevs.bootstrapi.confluence.rest;

import com.deftdevs.bootstrapi.commons.constants.BootstrAPI;
import com.deftdevs.bootstrapi.confluence.model.CacheModel;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import it.com.deftdevs.bootstrapi.commons.rest.HttpRequestHelper;
import org.junit.jupiter.api.Test;

import javax.ws.rs.core.Response;
import java.net.http.HttpResponse;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class CachesResourceFuncTest {

private final ObjectMapper objectMapper = new ObjectMapper();

@Test
void testGetCaches() throws Exception {
final HttpResponse<String> response = HttpRequestHelper.builder(BootstrAPI.CACHES)
.request();
assertEquals(Response.Status.OK.getStatusCode(), response.statusCode(), response.body());

final List<CacheModel> caches = objectMapper.readValue(response.body(), new TypeReference<List<CacheModel>>(){});
assertNotNull(caches);
assertFalse(caches.isEmpty());
}

@Test
void testGetCachesUnauthenticated() throws Exception {
final HttpResponse<String> response = HttpRequestHelper.builder(BootstrAPI.CACHES)
.username("wrong")
.password("password")
.request();

assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), response.statusCode());
}

@Test
void testGetCachesUnauthorized() throws Exception {
final HttpResponse<String> response = HttpRequestHelper.builder(BootstrAPI.CACHES)
.username("user")
.password("user")
.request();

assertEquals(Response.Status.FORBIDDEN.getStatusCode(), response.statusCode());
}
}
Loading