mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-07-02 19:40:08 +00:00
[AB-77] moving the templating module into the core module
adding a possibility to overlay specific templates for particular servers adding commands to configure templates adding file parameter support
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package dev.sheldan.abstracto.core.command.handler;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
|
||||
import dev.sheldan.abstracto.core.models.database.AChannel;
|
||||
import dev.sheldan.abstracto.core.service.ChannelService;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
@@ -14,7 +15,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class AChannelParameterHandlerImplTest {
|
||||
public class AChannelParameterHandlerImplTest extends AbstractParameterHandlerTest {
|
||||
|
||||
@InjectMocks
|
||||
private AChannelParameterHandlerImpl testUnit;
|
||||
@@ -49,10 +50,10 @@ public class AChannelParameterHandlerImplTest {
|
||||
|
||||
@Test
|
||||
public void testProperChannelMention() {
|
||||
String input = "test";
|
||||
when(textChannelParameterHandler.handle(input, iterators, TextChannel.class, message)).thenReturn(channel);
|
||||
UnparsedCommandParameterPiece piece = getPieceWithValue("input");
|
||||
when(textChannelParameterHandler.handle(piece, iterators, TextChannel.class, message)).thenReturn(channel);
|
||||
when(channelService.getFakeChannelFromTextChannel(channel)).thenReturn(aChannel);
|
||||
AChannel parsed = (AChannel) testUnit.handle(input, iterators, TextChannel.class, message);
|
||||
AChannel parsed = (AChannel) testUnit.handle(piece, iterators, TextChannel.class, message);
|
||||
Assert.assertEquals(aChannel, parsed);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package dev.sheldan.abstracto.core.command.handler;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
|
||||
import dev.sheldan.abstracto.core.models.database.AEmote;
|
||||
import dev.sheldan.abstracto.core.service.EmoteService;
|
||||
import net.dv8tion.jda.api.entities.Emote;
|
||||
@@ -14,8 +15,9 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class AEmoteParameterHandlerImplImplTest {
|
||||
public class AEmoteParameterHandlerImplImplTest extends AbstractParameterHandlerTest {
|
||||
|
||||
public static final String INPUT = "input";
|
||||
@InjectMocks
|
||||
private AEmoteParameterHandlerImpl testUnit;
|
||||
|
||||
@@ -49,19 +51,19 @@ public class AEmoteParameterHandlerImplImplTest {
|
||||
|
||||
@Test
|
||||
public void testProperEmoteMention() {
|
||||
String input = "test";
|
||||
when(emoteParameterHandler.handle(input, iterators, Emote.class, message)).thenReturn(emote);
|
||||
UnparsedCommandParameterPiece piece = getPieceWithValue(INPUT);
|
||||
when(emoteParameterHandler.handle(piece, iterators, Emote.class, message)).thenReturn(emote);
|
||||
when(emoteService.getFakeEmoteFromEmote(emote)).thenReturn(aEmote);
|
||||
AEmote parsed = (AEmote) testUnit.handle(input, iterators, AEmote.class, message);
|
||||
AEmote parsed = (AEmote) testUnit.handle(piece, iterators, AEmote.class, message);
|
||||
Assert.assertEquals(aEmote, parsed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultEmoteHandling() {
|
||||
String input = "test";
|
||||
when(emoteParameterHandler.handle(input, iterators, Emote.class, message)).thenReturn(null);
|
||||
when(emoteService.getFakeEmote(input)).thenReturn(aEmote);
|
||||
AEmote parsed = (AEmote) testUnit.handle(input, iterators, AEmote.class, message);
|
||||
UnparsedCommandParameterPiece piece = getPieceWithValue(INPUT);
|
||||
when(emoteParameterHandler.handle(piece, iterators, Emote.class, message)).thenReturn(null);
|
||||
when(emoteService.getFakeEmote(INPUT)).thenReturn(aEmote);
|
||||
AEmote parsed = (AEmote) testUnit.handle(piece, iterators, AEmote.class, message);
|
||||
Assert.assertEquals(aEmote, parsed);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package dev.sheldan.abstracto.core.command.handler;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
|
||||
import dev.sheldan.abstracto.core.models.database.AEmote;
|
||||
import dev.sheldan.abstracto.core.models.database.ARole;
|
||||
import dev.sheldan.abstracto.core.service.RoleService;
|
||||
@@ -15,7 +16,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ARoleParameterHandlerImplImplTest {
|
||||
public class ARoleParameterHandlerImplImplTest extends AbstractParameterHandlerTest {
|
||||
|
||||
@InjectMocks
|
||||
private ARoleParameterHandlerImpl testUnit;
|
||||
@@ -50,10 +51,10 @@ public class ARoleParameterHandlerImplImplTest {
|
||||
|
||||
@Test
|
||||
public void testProperRoleMention() {
|
||||
String input = "test";
|
||||
when(roleParameterHandler.handle(input, iterators, Role.class, message)).thenReturn(role);
|
||||
UnparsedCommandParameterPiece piece = getPieceWithValue("test");
|
||||
when(roleParameterHandler.handle(piece, iterators, Role.class, message)).thenReturn(role);
|
||||
when(roleService.getFakeRoleFromRole(role)).thenReturn(aRole);
|
||||
ARole parsed = (ARole) testUnit.handle(input, iterators, AEmote.class, message);
|
||||
ARole parsed = (ARole) testUnit.handle(piece, iterators, AEmote.class, message);
|
||||
Assert.assertEquals(aRole, parsed);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package dev.sheldan.abstracto.core.command.handler;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
|
||||
|
||||
public abstract class AbstractParameterHandlerTest {
|
||||
protected UnparsedCommandParameterPiece getPieceWithValue(String value) {
|
||||
return UnparsedCommandParameterPiece.builder().value(value).build();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package dev.sheldan.abstracto.core.command.handler;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -7,7 +8,7 @@ import org.mockito.InjectMocks;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class BooleanParameterHandlerImplTest {
|
||||
public class BooleanParameterHandlerImplTest extends AbstractParameterHandlerTest {
|
||||
|
||||
@InjectMocks
|
||||
private BooleanParameterHandlerImpl testUnit;
|
||||
@@ -24,22 +25,26 @@ public class BooleanParameterHandlerImplTest {
|
||||
|
||||
@Test
|
||||
public void testTrueParsing() {
|
||||
Assert.assertTrue((Boolean)testUnit.handle("true", null, null, null));
|
||||
UnparsedCommandParameterPiece piece = getPieceWithValue("true");
|
||||
Assert.assertTrue((Boolean)testUnit.handle(piece, null, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnyOtherText() {
|
||||
Assert.assertFalse((Boolean)testUnit.handle("test", null, null, null));
|
||||
UnparsedCommandParameterPiece piece = getPieceWithValue("test");
|
||||
Assert.assertFalse((Boolean)testUnit.handle(piece, null, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullInput() {
|
||||
Assert.assertFalse((Boolean)testUnit.handle(null, null, null, null));
|
||||
UnparsedCommandParameterPiece piece = getPieceWithValue(null);
|
||||
Assert.assertFalse((Boolean)testUnit.handle(piece, null, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyStringAsInput() {
|
||||
Assert.assertFalse((Boolean)testUnit.handle("", null, null, null));
|
||||
UnparsedCommandParameterPiece piece = getPieceWithValue("");
|
||||
Assert.assertFalse((Boolean)testUnit.handle(piece, null, null, null));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.mockito.InjectMocks;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DoubleParameterHandlerImplTest {
|
||||
public class DoubleParameterHandlerImplTest extends AbstractParameterHandlerTest {
|
||||
|
||||
@InjectMocks
|
||||
private DoubleParameterHandlerImpl testUnit;
|
||||
@@ -24,32 +24,32 @@ public class DoubleParameterHandlerImplTest {
|
||||
|
||||
@Test
|
||||
public void testSuccessfulParse() {
|
||||
Assert.assertEquals(5D, testUnit.handle("5", null, null, null));
|
||||
Assert.assertEquals(5D, testUnit.handle(getPieceWithValue("5"), null, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeNumber() {
|
||||
Assert.assertEquals(-5D, testUnit.handle("-5", null, null, null));
|
||||
Assert.assertEquals(-5D, testUnit.handle(getPieceWithValue("-5"), null, null, null));
|
||||
}
|
||||
|
||||
|
||||
public void testDecimal() {
|
||||
Assert.assertEquals(3.14D, testUnit.handle("3.14", null, null, null));
|
||||
Assert.assertEquals(3.14D, testUnit.handle(getPieceWithValue("3.14"), null, null, null));
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testTextAsInput() {
|
||||
testUnit.handle("someText", null, null, null);
|
||||
testUnit.handle(getPieceWithValue("someText"), null, null, null);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void testNullInput() {
|
||||
testUnit.handle(null, null, null, null);
|
||||
testUnit.handle(getPieceWithValue(null), null, null, null);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testEmptyStringAsInput() {
|
||||
testUnit.handle("", null, null, null);
|
||||
testUnit.handle(getPieceWithValue(""), null, null, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DurationParameterHandlerImplTest {
|
||||
public class DurationParameterHandlerImplTest extends AbstractParameterHandlerTest {
|
||||
|
||||
@InjectMocks
|
||||
private DurationParameterHandlerImpl testUnit;
|
||||
@@ -28,23 +28,23 @@ public class DurationParameterHandlerImplTest {
|
||||
|
||||
@Test
|
||||
public void testSimpleParsing() {
|
||||
Assert.assertEquals(Duration.ofMinutes(1), testUnit.handle("1m", null, null, null));
|
||||
Assert.assertEquals(Duration.ofMinutes(1), testUnit.handle(getPieceWithValue("1m"), null, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMoreComplicatedParsing() {
|
||||
Duration targetDuration = Duration.ofDays(4).plus(5, ChronoUnit.HOURS).plus(5, ChronoUnit.MINUTES);
|
||||
Assert.assertEquals(targetDuration, testUnit.handle("5m5h4d", null, null, null));
|
||||
Assert.assertEquals(targetDuration, testUnit.handle(getPieceWithValue("5h5m4d"), null, null, null));
|
||||
}
|
||||
|
||||
@Test(expected = DurationFormatException.class)
|
||||
public void testNullInput() {
|
||||
testUnit.handle(null, null, null, null);
|
||||
testUnit.handle(getPieceWithValue(null), null, null, null);
|
||||
}
|
||||
|
||||
@Test(expected = DurationFormatException.class)
|
||||
public void testEmptyStringAsInput() {
|
||||
testUnit.handle("", null, null, null);
|
||||
testUnit.handle(getPieceWithValue(""), null, null, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ import java.util.List;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class EmoteParameterHandlerImplTest {
|
||||
public class EmoteParameterHandlerImplTest extends AbstractParameterHandlerTest {
|
||||
|
||||
@InjectMocks
|
||||
private EmoteParameterHandlerImpl testUnit;
|
||||
@@ -50,7 +50,7 @@ public class EmoteParameterHandlerImplTest {
|
||||
public void testProperEmoteMention() {
|
||||
oneEmoteInIterator();
|
||||
String input = getEmoteMention();
|
||||
Emote parsed = (Emote) testUnit.handle(input, iterators, Emote.class, null);
|
||||
Emote parsed = (Emote) testUnit.handle(getPieceWithValue(input), iterators, Emote.class, null);
|
||||
Assert.assertEquals(parsed, emote);
|
||||
}
|
||||
|
||||
@@ -58,13 +58,13 @@ public class EmoteParameterHandlerImplTest {
|
||||
public void testEmoteById() {
|
||||
setupMessage();
|
||||
String input = EMOTE_ID.toString();
|
||||
Emote parsed = (Emote) testUnit.handle(input, null, Emote.class, message);
|
||||
Emote parsed = (Emote) testUnit.handle(getPieceWithValue(input), null, Emote.class, message);
|
||||
Assert.assertEquals(parsed, emote);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidEmoteMention() {
|
||||
Assert.assertNull(testUnit.handle("test", null, Emote.class, null));
|
||||
Assert.assertNull(testUnit.handle(getPieceWithValue("test"), null, Emote.class, null));
|
||||
}
|
||||
|
||||
private String getEmoteMention() {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package dev.sheldan.abstracto.core.command.handler;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
|
||||
import dev.sheldan.abstracto.core.models.FullEmote;
|
||||
import dev.sheldan.abstracto.core.models.database.AEmote;
|
||||
import dev.sheldan.abstracto.core.service.EmoteService;
|
||||
@@ -15,7 +16,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class FullEmoteParameterHandlerImplImplTest {
|
||||
public class FullEmoteParameterHandlerImplImplTest extends AbstractParameterHandlerTest {
|
||||
|
||||
@InjectMocks
|
||||
private FullEmoteParameterHandlerImpl testUnit;
|
||||
@@ -51,9 +52,10 @@ public class FullEmoteParameterHandlerImplImplTest {
|
||||
@Test
|
||||
public void testProperEmoteMention() {
|
||||
String input = "test";
|
||||
when(emoteParameterHandler.handle(input, iterators, Emote.class, message)).thenReturn(emote);
|
||||
UnparsedCommandParameterPiece piece = getPieceWithValue(input);
|
||||
when(emoteParameterHandler.handle(piece, iterators, Emote.class, message)).thenReturn(emote);
|
||||
when(emoteService.getFakeEmoteFromEmote(emote)).thenReturn(aEmote);
|
||||
FullEmote parsed = (FullEmote) testUnit.handle(input, iterators, FullEmote.class, message);
|
||||
FullEmote parsed = (FullEmote) testUnit.handle(piece, iterators, FullEmote.class, message);
|
||||
Assert.assertEquals(aEmote, parsed.getFakeEmote());
|
||||
Assert.assertEquals(emote, parsed.getEmote());
|
||||
}
|
||||
@@ -62,9 +64,10 @@ public class FullEmoteParameterHandlerImplImplTest {
|
||||
@Test
|
||||
public void testDefaultEmoteHandling() {
|
||||
String input = "test";
|
||||
when(emoteParameterHandler.handle(input, iterators, Emote.class, message)).thenReturn(null);
|
||||
UnparsedCommandParameterPiece piece = getPieceWithValue(input);
|
||||
when(emoteParameterHandler.handle(piece, iterators, Emote.class, message)).thenReturn(null);
|
||||
when(emoteService.getFakeEmote(input)).thenReturn(aEmote);
|
||||
FullEmote parsed = (FullEmote) testUnit.handle(input, iterators, AEmote.class, message);
|
||||
FullEmote parsed = (FullEmote) testUnit.handle(piece, iterators, AEmote.class, message);
|
||||
Assert.assertNull(parsed.getEmote());
|
||||
Assert.assertEquals(aEmote, parsed.getFakeEmote());
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package dev.sheldan.abstracto.core.command.handler;
|
||||
|
||||
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
|
||||
import dev.sheldan.abstracto.core.models.FullRole;
|
||||
import dev.sheldan.abstracto.core.models.database.ARole;
|
||||
import dev.sheldan.abstracto.core.service.RoleService;
|
||||
@@ -15,7 +16,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class FullRoleParameterHandlerImplImplTest {
|
||||
public class FullRoleParameterHandlerImplImplTest extends AbstractParameterHandlerTest {
|
||||
|
||||
@InjectMocks
|
||||
private FullRoleParameterHandlerImpl testUnit;
|
||||
@@ -51,9 +52,10 @@ public class FullRoleParameterHandlerImplImplTest {
|
||||
@Test
|
||||
public void testProperEmoteMention() {
|
||||
String input = "test";
|
||||
when(roleParameterHandler.handle(input, iterators, Role.class, message)).thenReturn(role);
|
||||
UnparsedCommandParameterPiece piece = getPieceWithValue(input);
|
||||
when(roleParameterHandler.handle(piece, iterators, Role.class, message)).thenReturn(role);
|
||||
when(roleService.getFakeRoleFromRole(role)).thenReturn(aRole);
|
||||
FullRole parsed = (FullRole) testUnit.handle(input, iterators, FullRole.class, message);
|
||||
FullRole parsed = (FullRole) testUnit.handle(piece, iterators, FullRole.class, message);
|
||||
Assert.assertEquals(aRole, parsed.getRole());
|
||||
Assert.assertEquals(role, parsed.getServerRole());
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.mockito.InjectMocks;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class IntegerParameterHandlerImplTest {
|
||||
public class IntegerParameterHandlerImplTest extends AbstractParameterHandlerTest {
|
||||
|
||||
@InjectMocks
|
||||
private IntegerParameterHandlerImpl testUnit;
|
||||
@@ -24,33 +24,33 @@ public class IntegerParameterHandlerImplTest {
|
||||
|
||||
@Test
|
||||
public void testSuccessfulParse() {
|
||||
Assert.assertEquals(5, testUnit.handle("5", null, null, null));
|
||||
Assert.assertEquals(5, testUnit.handle(getPieceWithValue("5"), null, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeNumber() {
|
||||
Assert.assertEquals(-5, testUnit.handle("-5", null, null, null));
|
||||
Assert.assertEquals(-5, testUnit.handle(getPieceWithValue("-5"), null, null, null));
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testDecimal() {
|
||||
testUnit.handle("3.14", null, null, null);
|
||||
testUnit.handle(getPieceWithValue("3.14"), null, null, null);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testTextAsInput() {
|
||||
testUnit.handle("someText", null, null, null);
|
||||
testUnit.handle(getPieceWithValue("someText"), null, null, null);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testNullInput() {
|
||||
testUnit.handle(null, null, null, null);
|
||||
testUnit.handle(getPieceWithValue(null), null, null, null);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testEmptyStringAsInput() {
|
||||
testUnit.handle("", null, null, null);
|
||||
testUnit.handle(getPieceWithValue(""), null, null, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.mockito.InjectMocks;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class LongParameterHandlerImplTest {
|
||||
public class LongParameterHandlerImplTest extends AbstractParameterHandlerTest {
|
||||
|
||||
@InjectMocks
|
||||
private LongParameterHandlerImpl testUnit;
|
||||
@@ -24,32 +24,32 @@ public class LongParameterHandlerImplTest {
|
||||
|
||||
@Test
|
||||
public void testSuccessfulParse() {
|
||||
Assert.assertEquals(5L, testUnit.handle("5", null, null, null));
|
||||
Assert.assertEquals(5L, testUnit.handle(getPieceWithValue("5"), null, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeNumber() {
|
||||
Assert.assertEquals(-5L, testUnit.handle("-5", null, null, null));
|
||||
Assert.assertEquals(-5L, testUnit.handle(getPieceWithValue("-5"), null, null, null));
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testDecimal() {
|
||||
testUnit.handle("3.14", null, null, null);
|
||||
testUnit.handle(getPieceWithValue("3.14"), null, null, null);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testTextAsInput() {
|
||||
testUnit.handle("someText", null, null, null);
|
||||
testUnit.handle(getPieceWithValue("someText"), null, null, null);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testNullInput() {
|
||||
testUnit.handle(null, null, null, null);
|
||||
testUnit.handle(getPieceWithValue(null), null, null, null);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testEmptyStringAsInput() {
|
||||
testUnit.handle("", null, null, null);
|
||||
testUnit.handle(getPieceWithValue(""), null, null, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ import java.util.concurrent.CompletableFuture;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class MemberParameterHandlerImplTest {
|
||||
public class MemberParameterHandlerImplTest extends AbstractParameterHandlerTest {
|
||||
|
||||
@InjectMocks
|
||||
private MemberParameterHandlerImpl testUnit;
|
||||
@@ -53,7 +53,7 @@ public class MemberParameterHandlerImplTest {
|
||||
public void testProperMemberMention() {
|
||||
oneMemberInIterator();
|
||||
String input = getUserMention();
|
||||
CompletableFuture<Member> parsed = (CompletableFuture) testUnit.handleAsync(input, iterators, Member.class, null);
|
||||
CompletableFuture<Member> parsed = (CompletableFuture) testUnit.handleAsync(getPieceWithValue(input), iterators, Member.class, null);
|
||||
Assert.assertEquals(parsed.join(), member);
|
||||
}
|
||||
|
||||
@@ -62,14 +62,14 @@ public class MemberParameterHandlerImplTest {
|
||||
public void testMemberById() {
|
||||
setupMessage();
|
||||
String input = USER_ID.toString();
|
||||
CompletableFuture<Member> parsed = (CompletableFuture) testUnit.handleAsync(input, null, Member.class, message);
|
||||
CompletableFuture<Member> parsed = (CompletableFuture) testUnit.handleAsync(getPieceWithValue(input), null, Member.class, message);
|
||||
Assert.assertEquals(parsed.join(), member);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testInvalidMemberMention() {
|
||||
String input = "test";
|
||||
testUnit.handleAsync(input, null, Member.class, null);
|
||||
testUnit.handleAsync(getPieceWithValue(input), null, Member.class, null);
|
||||
}
|
||||
|
||||
private String getUserMention() {
|
||||
|
||||
@@ -16,7 +16,7 @@ import java.util.List;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class RoleParameterHandlerImplTest {
|
||||
public class RoleParameterHandlerImplTest extends AbstractParameterHandlerTest {
|
||||
|
||||
@InjectMocks
|
||||
private RoleParameterHandlerImpl testUnit;
|
||||
@@ -49,7 +49,7 @@ public class RoleParameterHandlerImplTest {
|
||||
public void testProperRoleMention() {
|
||||
oneRoleIterator();
|
||||
String input = getRoleMention();
|
||||
Role parsed = (Role) testUnit.handle(input, iterators, Role.class, null);
|
||||
Role parsed = (Role) testUnit.handle(getPieceWithValue(input), iterators, Role.class, null);
|
||||
Assert.assertEquals(parsed, role);
|
||||
}
|
||||
|
||||
@@ -57,14 +57,14 @@ public class RoleParameterHandlerImplTest {
|
||||
public void testRoleById() {
|
||||
setupMessage();
|
||||
String input = ROLE_ID.toString();
|
||||
Role parsed = (Role) testUnit.handle(input, null, Role.class, message);
|
||||
Role parsed = (Role) testUnit.handle(getPieceWithValue(input), null, Role.class, message);
|
||||
Assert.assertEquals(parsed, role);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testInvalidRoleMention() {
|
||||
String input = "test";
|
||||
testUnit.handle(input, null, Role.class, null);
|
||||
testUnit.handle(getPieceWithValue(input), null, Role.class, null);
|
||||
}
|
||||
|
||||
private String getRoleMention() {
|
||||
|
||||
@@ -16,7 +16,7 @@ import java.util.List;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class TextChannelParameterHandlerImplTest {
|
||||
public class TextChannelParameterHandlerImplTest extends AbstractParameterHandlerTest {
|
||||
|
||||
@InjectMocks
|
||||
private TextChannelParameterHandlerImpl testUnit;
|
||||
@@ -49,7 +49,7 @@ public class TextChannelParameterHandlerImplTest {
|
||||
public void testProperChannelMention() {
|
||||
oneChannelInIterator();
|
||||
String input = getChannelMention();
|
||||
TextChannel parsed = (TextChannel) testUnit.handle(input, iterators, TextChannel.class, null);
|
||||
TextChannel parsed = (TextChannel) testUnit.handle(getPieceWithValue(input), iterators, TextChannel.class, null);
|
||||
Assert.assertEquals(parsed, channel);
|
||||
}
|
||||
|
||||
@@ -57,14 +57,14 @@ public class TextChannelParameterHandlerImplTest {
|
||||
public void testChannelMentionById() {
|
||||
setupMessage();
|
||||
String input = CHANNEL_ID.toString();
|
||||
TextChannel parsed = (TextChannel) testUnit.handle(input, null, TextChannel.class, message);
|
||||
TextChannel parsed = (TextChannel) testUnit.handle(getPieceWithValue(input), null, TextChannel.class, message);
|
||||
Assert.assertEquals(parsed, channel);
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
public void testInvalidChannelMention() {
|
||||
String input = "test";
|
||||
testUnit.handle(input, null, TextChannel.class, null);
|
||||
testUnit.handle(getPieceWithValue(input), null, TextChannel.class, null);
|
||||
}
|
||||
|
||||
private String getChannelMention() {
|
||||
|
||||
@@ -79,8 +79,8 @@ public class CommandServiceBeanTest {
|
||||
}
|
||||
|
||||
private CommandConfiguration getMixedParameterConfig() {
|
||||
Parameter param1 = Parameter.builder().name(PARAMETER_1_NAME).optional(false).build();
|
||||
Parameter param2 = Parameter.builder().name(PARAMETER_2_NAME).optional(true).build();
|
||||
Parameter param1 = Parameter.builder().name(PARAMETER_1_NAME).type(Object.class).optional(false).build();
|
||||
Parameter param2 = Parameter.builder().name(PARAMETER_2_NAME).type(Object.class).optional(true).build();
|
||||
return CommandConfiguration
|
||||
.builder()
|
||||
.name(COMMAND_NAME)
|
||||
@@ -89,7 +89,7 @@ public class CommandServiceBeanTest {
|
||||
}
|
||||
|
||||
private Parameter getOptionalParameter(boolean b) {
|
||||
return Parameter.builder().name(PARAMETER_1_NAME).optional(b).build();
|
||||
return Parameter.builder().name(PARAMETER_1_NAME).type(Object.class).optional(b).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public class FeatureModesTest {
|
||||
AServer server = Mockito.mock(AServer.class);
|
||||
when(serverManagementService.loadServer(noParameters.getGuild())).thenReturn(server);
|
||||
when(featureModeService.getEffectiveFeatureModes(server)).thenReturn(featureModeDisplays);
|
||||
when(channelService.sendEmbedTemplateInChannel(eq(FeatureModes.FEATURE_MODES_RESPONSE_TEMPLATE_KEY), modelCaptor.capture(), eq(noParameters.getChannel()))).thenReturn(new ArrayList<>());
|
||||
when(channelService.sendEmbedTemplateInTextChannelList(eq(FeatureModes.FEATURE_MODES_RESPONSE_TEMPLATE_KEY), modelCaptor.capture(), eq(noParameters.getChannel()))).thenReturn(new ArrayList<>());
|
||||
CompletableFuture<CommandResult> commandResultCompletableFuture = testUnit.executeAsync(noParameters);
|
||||
CommandTestUtilities.checkSuccessfulCompletionAsync(commandResultCompletableFuture);
|
||||
List<FeatureModeDisplay> usedDisplays = modelCaptor.getValue().getFeatureModes();
|
||||
@@ -86,7 +86,7 @@ public class FeatureModesTest {
|
||||
AServer server = Mockito.mock(AServer.class);
|
||||
when(serverManagementService.loadServer(noParameters.getGuild())).thenReturn(server);
|
||||
when(featureModeService.getEffectiveFeatureModes(server, feature)).thenReturn(featureModeDisplays);
|
||||
when(channelService.sendEmbedTemplateInChannel(eq(FeatureModes.FEATURE_MODES_RESPONSE_TEMPLATE_KEY), modelCaptor.capture(), eq(noParameters.getChannel()))).thenReturn(new ArrayList<>());
|
||||
when(channelService.sendEmbedTemplateInTextChannelList(eq(FeatureModes.FEATURE_MODES_RESPONSE_TEMPLATE_KEY), modelCaptor.capture(), eq(noParameters.getChannel()))).thenReturn(new ArrayList<>());
|
||||
CompletableFuture<CommandResult> commandResultCompletableFuture = testUnit.executeAsync(noParameters);
|
||||
CommandTestUtilities.checkSuccessfulCompletionAsync(commandResultCompletableFuture);
|
||||
List<FeatureModeDisplay> usedDisplays = modelCaptor.getValue().getFeatureModes();
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package dev.sheldan.abstracto.core.templating.loading;
|
||||
|
||||
import dev.sheldan.abstracto.core.config.ServerContext;
|
||||
import dev.sheldan.abstracto.core.templating.model.EffectiveTemplate;
|
||||
import dev.sheldan.abstracto.core.templating.service.management.EffectiveTemplateManagementService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.core.IsEqual.equalTo;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class TemplateLoaderTest {
|
||||
|
||||
public static final String TEST_CONTENT = "test";
|
||||
public static final String TEMPLATE_KEY = "key";
|
||||
|
||||
@InjectMocks
|
||||
private DatabaseTemplateLoader loader;
|
||||
|
||||
@Mock
|
||||
private EffectiveTemplateManagementService templateManagementService;
|
||||
|
||||
@Mock
|
||||
private ServerContext serverContext;
|
||||
|
||||
@Test
|
||||
public void testProperLoading() throws IOException {
|
||||
EffectiveTemplate mocked = Mockito.mock(EffectiveTemplate.class);
|
||||
when(mocked.getContent()).thenReturn(TEST_CONTENT);
|
||||
when(templateManagementService.getTemplateByKey(TEMPLATE_KEY)).thenReturn(Optional.of(mocked));
|
||||
EffectiveTemplate templateSource = (EffectiveTemplate) loader.findTemplateSource(TEMPLATE_KEY);
|
||||
assertThat(TEST_CONTENT, equalTo(templateSource.getContent()));
|
||||
}
|
||||
|
||||
@Test(expected = IOException.class)
|
||||
public void testMissingTemplate() throws IOException {
|
||||
loader.findTemplateSource(TEMPLATE_KEY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReader() throws IOException {
|
||||
EffectiveTemplate template = Mockito.mock(EffectiveTemplate.class);
|
||||
when(template.getContent()).thenReturn(TEST_CONTENT);
|
||||
Reader reader = loader.getReader(template, null);
|
||||
char[] chars = new char[4];
|
||||
reader.read(chars, 0, 4);
|
||||
assertThat(TEST_CONTENT, equalTo(new String(chars)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package dev.sheldan.abstracto.core.templating.method;
|
||||
|
||||
import freemarker.ext.beans.StringModel;
|
||||
import freemarker.template.*;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DateMethodTest {
|
||||
|
||||
public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||
public static final Instant DATE = Instant.ofEpochSecond(1590615937);
|
||||
|
||||
@InjectMocks
|
||||
private DateMethod dateMethod;
|
||||
|
||||
@Test
|
||||
public void testInstantFormat() throws TemplateModelException {
|
||||
String exec = (String) dateMethod.exec(getCorrectParametersInstant());
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)
|
||||
.withZone(ZoneId.systemDefault());
|
||||
Assert.assertEquals(exec, formatter.format(DATE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOffsetDateTimeObject() throws TemplateModelException {
|
||||
String exec = (String) dateMethod.exec(getCorrectParametersOffsetDateTime());
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)
|
||||
.withZone(ZoneId.systemDefault());
|
||||
Assert.assertEquals(exec, formatter.format(DATE));
|
||||
}
|
||||
|
||||
@Test(expected = TemplateModelException.class)
|
||||
public void incorrectParameterCount() throws TemplateModelException {
|
||||
dateMethod.exec(new ArrayList<Object>());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testIncorrectDateFormat() throws TemplateModelException {
|
||||
dateMethod.exec(getIncorrectDateFormat());
|
||||
}
|
||||
|
||||
@Test(expected = TemplateModelException.class)
|
||||
public void incorrectPassedObject() throws TemplateModelException {
|
||||
dateMethod.exec(getIncorrectTimeParameter());
|
||||
}
|
||||
|
||||
private List<Object> getIncorrectDateFormat() {
|
||||
List<Object> params = new ArrayList<>();
|
||||
params.add(getInstantDateObject());
|
||||
params.add(incorrectDateFormat());
|
||||
return params;
|
||||
}
|
||||
|
||||
private List<Object> getIncorrectTimeParameter() {
|
||||
List<Object> params = new ArrayList<>();
|
||||
params.add(getNotCompatibleObject());
|
||||
params.add(incorrectDateFormat());
|
||||
return params;
|
||||
}
|
||||
|
||||
|
||||
private List<Object> getCorrectParametersInstant() {
|
||||
List<Object> params = new ArrayList<>();
|
||||
params.add(getInstantDateObject());
|
||||
params.add(simpleDateFormat());
|
||||
return params;
|
||||
}
|
||||
|
||||
private List<Object> getCorrectParametersOffsetDateTime() {
|
||||
List<Object> params = new ArrayList<>();
|
||||
params.add(getOffsetDateTimeObject());
|
||||
params.add(simpleDateFormat());
|
||||
return params;
|
||||
}
|
||||
|
||||
private SimpleScalar simpleDateFormat() {
|
||||
return new SimpleScalar(DATE_TIME_FORMAT);
|
||||
}
|
||||
|
||||
private SimpleScalar incorrectDateFormat() {
|
||||
return new SimpleScalar("INCORRECT");
|
||||
}
|
||||
|
||||
private StringModel getInstantDateObject() {
|
||||
DefaultObjectWrapper wrapper = getWrapper();
|
||||
return new StringModel(DATE, wrapper);
|
||||
}
|
||||
|
||||
private DefaultObjectWrapper getWrapper() {
|
||||
return new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_0).build();
|
||||
}
|
||||
|
||||
private StringModel getOffsetDateTimeObject() {
|
||||
DefaultObjectWrapper wrapper = getWrapper();
|
||||
return new StringModel(OffsetDateTime.ofInstant(DATE, ZoneId.systemDefault()), wrapper);
|
||||
}
|
||||
|
||||
private StringModel getNotCompatibleObject() {
|
||||
DefaultObjectWrapper wrapper = getWrapper();
|
||||
return new StringModel("", wrapper);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package dev.sheldan.abstracto.core.templating.method;
|
||||
|
||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||
import freemarker.ext.beans.StringModel;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.DefaultObjectWrapper;
|
||||
import freemarker.template.DefaultObjectWrapperBuilder;
|
||||
import freemarker.template.TemplateModelException;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DurationMethodTest {
|
||||
public static final long SECOND_AMOUNT = 10L;
|
||||
public static final long MINUTES_AMOUNT = 2L;
|
||||
public static final long HOURS_AMOUNT = 3L;
|
||||
public static final long DAYS_AMOUNT = 5L;
|
||||
public static final String DURATION_TEMPLATE = "duration_formatting";
|
||||
public static final String SECONDS = "seconds";
|
||||
public static final String HOURS = "hours";
|
||||
public static final String MINUTES = "minutes";
|
||||
public static final String DAYS = "days";
|
||||
|
||||
@InjectMocks
|
||||
private DurationMethod durationMethod;
|
||||
|
||||
@Mock
|
||||
private TemplateService templateService;
|
||||
|
||||
|
||||
@Test
|
||||
public void testSeconds() throws TemplateModelException {
|
||||
durationMethod.exec(getSecondParameters());
|
||||
verify(templateService, times(1)).renderTemplateWithMap(DURATION_TEMPLATE, getHashMap(0, 0, 0, SECOND_AMOUNT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMinutes() throws TemplateModelException {
|
||||
durationMethod.exec(getMinuteParameter());
|
||||
verify(templateService, times(1)).renderTemplateWithMap(DURATION_TEMPLATE, getHashMap(0, 0, MINUTES_AMOUNT, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHours() throws TemplateModelException {
|
||||
durationMethod.exec(getHourParameter());
|
||||
verify(templateService, times(1)).renderTemplateWithMap(DURATION_TEMPLATE, getHashMap(0, HOURS_AMOUNT, 0, 0));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDays() throws TemplateModelException {
|
||||
durationMethod.exec(getDayParameter());
|
||||
verify(templateService, times(1)).renderTemplateWithMap(DURATION_TEMPLATE, getHashMap(DAYS_AMOUNT, 0, 0, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllTime() throws TemplateModelException {
|
||||
durationMethod.exec(getMixedParameter());
|
||||
verify(templateService, times(1)).renderTemplateWithMap(DURATION_TEMPLATE, getHashMap(DAYS_AMOUNT, HOURS_AMOUNT, MINUTES_AMOUNT, SECOND_AMOUNT));
|
||||
}
|
||||
|
||||
@Test(expected = TemplateModelException.class)
|
||||
public void testNoParamGiven() throws TemplateModelException {
|
||||
durationMethod.exec(Collections.emptyList());
|
||||
}
|
||||
|
||||
@Test(expected = TemplateModelException.class)
|
||||
public void testNoDurationObject() throws TemplateModelException {
|
||||
durationMethod.exec(Arrays.asList(new StringModel("", getWrapper())));
|
||||
}
|
||||
|
||||
@Test(expected = TemplateModelException.class)
|
||||
public void testNoStringModelObject() throws TemplateModelException {
|
||||
durationMethod.exec(Arrays.asList(""));
|
||||
}
|
||||
|
||||
|
||||
private List<Object> getSecondParameters() {
|
||||
return Arrays.asList(new StringModel(Duration.ofSeconds(SECOND_AMOUNT), getWrapper()));
|
||||
}
|
||||
|
||||
private List<Object> getMinuteParameter() {
|
||||
return Arrays.asList(new StringModel(Duration.ofMinutes(MINUTES_AMOUNT), getWrapper()));
|
||||
}
|
||||
|
||||
private List<Object> getHourParameter() {
|
||||
return Arrays.asList(new StringModel(Duration.ofHours(HOURS_AMOUNT), getWrapper()));
|
||||
}
|
||||
|
||||
private List<Object> getDayParameter() {
|
||||
return Arrays.asList(new StringModel(Duration.ofDays(DAYS_AMOUNT), getWrapper()));
|
||||
}
|
||||
|
||||
private List<Object> getMixedParameter() {
|
||||
return Arrays.asList(new StringModel(Duration.ofSeconds(SECOND_AMOUNT)
|
||||
.plus(Duration.ofMinutes(MINUTES_AMOUNT))
|
||||
.plus(Duration.ofHours(HOURS_AMOUNT))
|
||||
.plus(Duration.ofDays(DAYS_AMOUNT)), getWrapper()));
|
||||
}
|
||||
|
||||
private DefaultObjectWrapper getWrapper() {
|
||||
return new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_0).build();
|
||||
}
|
||||
|
||||
private HashMap<String, Object> getHashMap(long days, long hours, long minutes, long seconds) {
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
map.put(DAYS, days);
|
||||
map.put(HOURS, hours);
|
||||
map.put(MINUTES, minutes);
|
||||
map.put(SECONDS, seconds);
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package dev.sheldan.abstracto.core.templating.method;
|
||||
|
||||
import dev.sheldan.abstracto.core.templating.service.TemplateService;
|
||||
import freemarker.template.*;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SafeFieldIterationsTest {
|
||||
|
||||
public static final String TEMPLATE_KEY = "template";
|
||||
public static final String FIELD_NAME_TEMPLATE = "fieldName";
|
||||
public static final String FIELD_NAME_VALUE = "fieldName";
|
||||
public static final String EXPECTED_START_PART = "{ \"name\": \"" + FIELD_NAME_VALUE + "\", \"inline\": \"true\", \"value\": \"";
|
||||
public static final String INLINE_VALUE = "true";
|
||||
public static final String FIRST_LIST_ENTRY = "text";
|
||||
public static final String SIX_HUNDRED_CHARACTERS = RandomStringUtils.randomAlphabetic(600);
|
||||
|
||||
@InjectMocks
|
||||
private SafeFieldIterations safeFieldIterations;
|
||||
|
||||
@Mock
|
||||
private TemplateService templateService;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<String> templateKeyCaptor;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
when(templateService.renderTemplateWithMap(eq(FIELD_NAME_TEMPLATE), any())).thenReturn(FIELD_NAME_VALUE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyList() throws TemplateModelException {
|
||||
String resultingValue = (String) safeFieldIterations.exec(getSimpleParameters());
|
||||
verify(templateService, times(1)).renderTemplateWithMap(templateKeyCaptor.capture(), any());
|
||||
List<String> usedTemplateKeys = templateKeyCaptor.getAllValues();
|
||||
assertEquals(FIELD_NAME_TEMPLATE, usedTemplateKeys.get(0));
|
||||
assertEquals(EXPECTED_START_PART + "\"}", resultingValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneElement() throws TemplateModelException {
|
||||
when(templateService.renderTemplateWithMap(eq(TEMPLATE_KEY), any())).thenReturn(FIRST_LIST_ENTRY);
|
||||
String resultingValue = (String) safeFieldIterations.exec(oneListEntryParameter());
|
||||
assertEquals(EXPECTED_START_PART + FIRST_LIST_ENTRY + "\"}", resultingValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoElements() throws TemplateModelException {
|
||||
when(templateService.renderTemplateWithMap(eq(TEMPLATE_KEY), any())).thenReturn(FIRST_LIST_ENTRY);
|
||||
String resultingValue = (String) safeFieldIterations.exec(twoListEntryParameter());
|
||||
assertEquals(EXPECTED_START_PART + FIRST_LIST_ENTRY + FIRST_LIST_ENTRY + "\"}", resultingValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testElementsStaySolo() throws TemplateModelException {
|
||||
when(templateService.renderTemplateWithMap(eq(TEMPLATE_KEY), any())).thenReturn(SIX_HUNDRED_CHARACTERS);
|
||||
String resultingValue = (String) safeFieldIterations.exec(twoListEntryParameter());
|
||||
assertEquals(EXPECTED_START_PART + SIX_HUNDRED_CHARACTERS + "\"}," + EXPECTED_START_PART + SIX_HUNDRED_CHARACTERS + "\"}", resultingValue);
|
||||
}
|
||||
|
||||
@Test(expected = TemplateModelException.class)
|
||||
public void testTooLittleParameters() throws TemplateModelException {
|
||||
safeFieldIterations.exec(Arrays.asList(""));
|
||||
}
|
||||
|
||||
@Test(expected = TemplateModelException.class)
|
||||
public void testWrongListAdapterType() throws TemplateModelException {
|
||||
safeFieldIterations.exec(wrongListAdapter());
|
||||
}
|
||||
|
||||
@Test(expected = TemplateModelException.class)
|
||||
public void testWrongTemplateKeyParameterType() throws TemplateModelException {
|
||||
safeFieldIterations.exec(wrongTemplateKeyParameterType());
|
||||
}
|
||||
|
||||
@Test(expected = TemplateModelException.class)
|
||||
public void testWrongFieldNameTemplateKeyParameterType() throws TemplateModelException {
|
||||
safeFieldIterations.exec(wrongFieldNameTemplateKeyParameterType());
|
||||
}
|
||||
|
||||
@Test(expected = TemplateModelException.class)
|
||||
public void testWrongInlineParameterType() throws TemplateModelException {
|
||||
safeFieldIterations.exec(wrongInLineValueParameterType());
|
||||
}
|
||||
|
||||
|
||||
public List<Object> wrongListAdapter() {
|
||||
return Arrays.asList(new Object(), new Object(), new SimpleScalar(FIELD_NAME_TEMPLATE), new SimpleScalar(INLINE_VALUE));
|
||||
}
|
||||
|
||||
public List<Object> wrongTemplateKeyParameterType() {
|
||||
return Arrays.asList(validEmptyList(), new Object(), new SimpleScalar(FIELD_NAME_TEMPLATE), new SimpleScalar(INLINE_VALUE));
|
||||
}
|
||||
|
||||
public List<Object> wrongFieldNameTemplateKeyParameterType() {
|
||||
return Arrays.asList(validEmptyList(), new SimpleScalar(TEMPLATE_KEY), new Object(), new SimpleScalar(INLINE_VALUE));
|
||||
}
|
||||
|
||||
public List<Object> wrongInLineValueParameterType() {
|
||||
return Arrays.asList(validEmptyList(), new SimpleScalar(TEMPLATE_KEY), new SimpleScalar(FIELD_NAME_TEMPLATE), new Object());
|
||||
}
|
||||
|
||||
public List<Object> getSimpleParameters() {
|
||||
return Arrays.asList(validEmptyList(), new SimpleScalar(TEMPLATE_KEY), new SimpleScalar(FIELD_NAME_TEMPLATE), new SimpleScalar(INLINE_VALUE));
|
||||
}
|
||||
|
||||
private DefaultListAdapter validEmptyList() {
|
||||
return DefaultListAdapter.adapt(new ArrayList<Object>(), getWrapper());
|
||||
}
|
||||
|
||||
public List<Object> oneListEntryParameter() {
|
||||
return Arrays.asList(DefaultListAdapter.adapt(Arrays.asList("testing"), getWrapper()), new SimpleScalar(TEMPLATE_KEY), new SimpleScalar(FIELD_NAME_TEMPLATE), new SimpleScalar(INLINE_VALUE));
|
||||
}
|
||||
|
||||
public List<Object> twoListEntryParameter() {
|
||||
return Arrays.asList(DefaultListAdapter.adapt(Arrays.asList("testing", "otherText"), getWrapper()), new SimpleScalar(TEMPLATE_KEY), new SimpleScalar(FIELD_NAME_TEMPLATE), new SimpleScalar(INLINE_VALUE));
|
||||
}
|
||||
|
||||
private DefaultObjectWrapper getWrapper() {
|
||||
return new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_0).build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,340 @@
|
||||
package dev.sheldan.abstracto.core.templating.service;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import dev.sheldan.abstracto.core.config.ServerContext;
|
||||
import dev.sheldan.abstracto.core.templating.Templatable;
|
||||
import dev.sheldan.abstracto.core.templating.exception.TemplatingException;
|
||||
import dev.sheldan.abstracto.core.templating.model.*;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
import freemarker.template.TemplateNotFoundException;
|
||||
import io.micrometer.core.instrument.util.IOUtils;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class TemplateServiceBeanTest {
|
||||
|
||||
public static final String EXAMPLE_URL = "https://example.com";
|
||||
public static final String FIELD_VALUE = "value";
|
||||
public static final String EMBED_PAGE_COUNT_TEMPLATE = "embed_page_count";
|
||||
public static final String FIELD_TEMPLATE = "{\n" +
|
||||
"\"name\": \"name\",\n" +
|
||||
"\"value\": \"value\",\n" +
|
||||
"\"inline\": \"true\"\n" +
|
||||
"}";
|
||||
|
||||
public static final String FIELD_TEMPLATE_WITH_VALUE = "{\n" +
|
||||
"\"name\": \"name\",\n" +
|
||||
"\"value\": \"%s\",\n" +
|
||||
"\"inline\": \"true\"\n" +
|
||||
"}";
|
||||
@InjectMocks
|
||||
private TemplateServiceBean templateServiceBean;
|
||||
|
||||
@Mock
|
||||
private Configuration configuration;
|
||||
|
||||
@Mock
|
||||
private Gson gson;
|
||||
|
||||
@Mock
|
||||
private ServerContext serverContext;
|
||||
|
||||
private static final String SIMPLE_TEMPLATE_SOURCE = "source";
|
||||
private static final String TEMPLATE_KEY = "template";
|
||||
private static final Long SERVER_ID = 1L;
|
||||
|
||||
@Test
|
||||
public void testSimpleTemplate() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
when(configuration.getTemplate(TEMPLATE_KEY, null, SERVER_ID, null, true, false)).thenReturn(getSimpleTemplate());
|
||||
String rendered = templateServiceBean.renderSimpleTemplate(TEMPLATE_KEY);
|
||||
Assert.assertEquals(SIMPLE_TEMPLATE_SOURCE, rendered);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void renderTemplatable() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
when(configuration.getTemplate(TEMPLATE_KEY, null, SERVER_ID, null, true, false)).thenReturn(getSimpleTemplate());
|
||||
Templatable templatable = getTemplatableWithSimpleTemplate();
|
||||
String rendered = templateServiceBean.renderTemplatable(templatable);
|
||||
Assert.assertEquals(SIMPLE_TEMPLATE_SOURCE, rendered);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTemplateWithMapParameter() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
when(configuration.getTemplate(TEMPLATE_KEY, null, SERVER_ID, null, true, false)).thenReturn(getSimpleTemplate());
|
||||
String rendered = templateServiceBean.renderTemplateWithMap(TEMPLATE_KEY, new HashMap<>());
|
||||
Assert.assertEquals(SIMPLE_TEMPLATE_SOURCE, rendered);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmbedWithDescription() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
String descriptionText = "test";
|
||||
String fullEmbedTemplateKey = getEmbedTemplateKey();
|
||||
when(configuration.getTemplate(fullEmbedTemplateKey, null, SERVER_ID, null, true, false)).thenReturn(getEmbedTemplateWithDescription(descriptionText));
|
||||
when(gson.fromJson(embedTemplateWithDescription(descriptionText), EmbedConfiguration.class)).thenReturn(embedConfigWithDescription(descriptionText));
|
||||
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new HashMap<>());
|
||||
Assert.assertEquals(descriptionText, messageToSend.getEmbeds().get(0).getDescription());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmbedWithAllUsableAttributes() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(getEmbedTemplateWithFallFieldsUsedOnce());
|
||||
when(gson.fromJson(getFullEmbedConfigString(), EmbedConfiguration.class)).thenReturn(getFullEmbedConfiguration());
|
||||
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new HashMap<>());
|
||||
Assert.assertEquals("additionalMessage", messageToSend.getMessage());
|
||||
MessageEmbed onlyEmbed = messageToSend.getEmbeds().get(0);
|
||||
Assert.assertEquals(EXAMPLE_URL, onlyEmbed.getAuthor().getIconUrl());
|
||||
Assert.assertEquals("name", onlyEmbed.getAuthor().getName());
|
||||
Assert.assertEquals("description", onlyEmbed.getDescription());
|
||||
MessageEmbed.Field onlyField = onlyEmbed.getFields().get(0);
|
||||
Assert.assertEquals("name", onlyField.getName());
|
||||
Assert.assertEquals(FIELD_VALUE, onlyField.getValue());
|
||||
Assert.assertTrue(onlyField.isInline());
|
||||
Color color = onlyEmbed.getColor();
|
||||
Assert.assertEquals(255, color.getBlue());
|
||||
Assert.assertEquals(255, color.getRed());
|
||||
Assert.assertEquals(255, color.getGreen());
|
||||
Assert.assertEquals(EXAMPLE_URL, onlyEmbed.getUrl());
|
||||
Assert.assertEquals("text", onlyEmbed.getFooter().getText());
|
||||
Assert.assertEquals(EXAMPLE_URL, onlyEmbed.getFooter().getIconUrl());
|
||||
Assert.assertEquals(EXAMPLE_URL, onlyEmbed.getImage().getUrl());
|
||||
Assert.assertEquals(EXAMPLE_URL, onlyEmbed.getThumbnail().getUrl());
|
||||
Assert.assertEquals(1, messageToSend.getEmbeds().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmbedWithTooLongDescription() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
int tooMuchCharacterCount = 1024;
|
||||
String descriptionText = RandomStringUtils.randomAlphabetic(MessageEmbed.TEXT_MAX_LENGTH + tooMuchCharacterCount);
|
||||
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(getEmbedTemplateWithDescription(descriptionText));
|
||||
when(configuration.getTemplate(EMBED_PAGE_COUNT_TEMPLATE, null, SERVER_ID, null, true, false)).thenReturn(getPageCountTemplate(1));
|
||||
when(gson.fromJson(embedTemplateWithDescription(descriptionText), EmbedConfiguration.class)).thenReturn(embedConfigWithDescription(descriptionText));
|
||||
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new HashMap<>());
|
||||
MessageEmbed firstEmbed = messageToSend.getEmbeds().get(0);
|
||||
Assert.assertEquals(MessageEmbed.TEXT_MAX_LENGTH, firstEmbed.getDescription().length());
|
||||
Assert.assertEquals(descriptionText.substring(0, MessageEmbed.TEXT_MAX_LENGTH), firstEmbed.getDescription());
|
||||
MessageEmbed secondEmbed = messageToSend.getEmbeds().get(1);
|
||||
Assert.assertEquals(tooMuchCharacterCount, secondEmbed.getDescription().length());
|
||||
Assert.assertEquals(descriptionText.substring(MessageEmbed.TEXT_MAX_LENGTH, MessageEmbed.TEXT_MAX_LENGTH + tooMuchCharacterCount), secondEmbed.getDescription());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmbedWithTooManyFields() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
int totalFieldCount = 30;
|
||||
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(getEmbedTemplateWithFieldCount(totalFieldCount));
|
||||
when(configuration.getTemplate(EMBED_PAGE_COUNT_TEMPLATE, null, SERVER_ID, null, true, false)).thenReturn(getPageCountTemplate(1));
|
||||
when(gson.fromJson(getFieldsEmbedConfigAsString(totalFieldCount), EmbedConfiguration.class)).thenReturn(getTooManyFieldsEmbedConfiguration());
|
||||
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new HashMap<>());
|
||||
MessageEmbed firstEmbed = messageToSend.getEmbeds().get(0);
|
||||
Assert.assertEquals(25, firstEmbed.getFields().size());
|
||||
MessageEmbed secondEmbed = messageToSend.getEmbeds().get(1);
|
||||
Assert.assertEquals(totalFieldCount % 25, secondEmbed.getFields().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmbedWithTooLongField() throws IOException, TemplateException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
String fieldValue = RandomStringUtils.randomAlphabetic(1500);
|
||||
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenReturn(getEmbedTemplateWithTooLongField(fieldValue));
|
||||
when(gson.fromJson(getSingleFieldWithValue(fieldValue), EmbedConfiguration.class)).thenReturn(getEmbedWithSingleFieldOfValue(fieldValue));
|
||||
MessageToSend messageToSend = templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new HashMap<>());
|
||||
MessageEmbed firstEmbed = messageToSend.getEmbeds().get(0);
|
||||
Assert.assertEquals(2, firstEmbed.getFields().size());
|
||||
Assert.assertEquals(fieldValue.substring(0, MessageEmbed.VALUE_MAX_LENGTH), firstEmbed.getFields().get(0).getValue());
|
||||
Assert.assertEquals(fieldValue.substring(MessageEmbed.VALUE_MAX_LENGTH), firstEmbed.getFields().get(1).getValue());
|
||||
}
|
||||
|
||||
@Test(expected = TemplatingException.class)
|
||||
public void tryToRenderMissingTemplate() throws IOException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
when(configuration.getTemplate(TEMPLATE_KEY, null, SERVER_ID, null, true, false)).thenThrow(new TemplateNotFoundException(TEMPLATE_KEY, new Object(), ""));
|
||||
templateServiceBean.renderSimpleTemplate(TEMPLATE_KEY);
|
||||
}
|
||||
|
||||
@Test(expected = TemplatingException.class)
|
||||
public void tryToRenderMissingEmbedTemplate() throws IOException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
when(configuration.getTemplate(getEmbedTemplateKey(), null, SERVER_ID, null, true, false)).thenThrow(new TemplateNotFoundException(TEMPLATE_KEY, new Object(), ""));
|
||||
templateServiceBean.renderEmbedTemplate(TEMPLATE_KEY, new Object());
|
||||
}
|
||||
|
||||
private String getEmbedTemplateKey() {
|
||||
return TEMPLATE_KEY + "_embed";
|
||||
}
|
||||
|
||||
@Test(expected = TemplatingException.class)
|
||||
public void tryToRenderMissingTemplateWithMap() throws IOException {
|
||||
when(serverContext.getServerId()).thenReturn(SERVER_ID);
|
||||
when(configuration.getTemplate(TEMPLATE_KEY, null, SERVER_ID, null, true, false)).thenThrow(new TemplateNotFoundException(TEMPLATE_KEY, new Object(), ""));
|
||||
templateServiceBean.renderTemplateWithMap(TEMPLATE_KEY, new HashMap<>());
|
||||
}
|
||||
|
||||
private EmbedConfiguration embedConfigWithDescription(String descriptionText) {
|
||||
return EmbedConfiguration.builder().description(descriptionText).build();
|
||||
}
|
||||
|
||||
private EmbedConfiguration getEmbedWithSingleFieldOfValue(String value) {
|
||||
List<EmbedField> fields = new ArrayList<>();
|
||||
fields.add(EmbedField.builder().name("name").value(value).build());
|
||||
return EmbedConfiguration.builder().fields(fields).build();
|
||||
}
|
||||
|
||||
private EmbedConfiguration getTooManyFieldsEmbedConfiguration() {
|
||||
List<EmbedField> fields = new ArrayList<>();
|
||||
for (int i = 0; i < 30; i++) {
|
||||
EmbedField field = EmbedField
|
||||
.builder()
|
||||
.value(FIELD_VALUE)
|
||||
.inline(true)
|
||||
.name("name")
|
||||
.build();
|
||||
fields.add(field);
|
||||
}
|
||||
return EmbedConfiguration.builder().fields(fields).build();
|
||||
}
|
||||
|
||||
private Templatable getTemplatableWithSimpleTemplate() {
|
||||
return new Templatable() {
|
||||
@Override
|
||||
public String getTemplateName() {
|
||||
return TEMPLATE_KEY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getTemplateModel() {
|
||||
return new Object();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private EmbedConfiguration getFullEmbedConfiguration() {
|
||||
|
||||
EmbedAuthor author = EmbedAuthor
|
||||
.builder()
|
||||
.avatar(EXAMPLE_URL)
|
||||
.name("name")
|
||||
.url(EXAMPLE_URL)
|
||||
.build();
|
||||
|
||||
EmbedColor color = EmbedColor
|
||||
.builder()
|
||||
.r(255)
|
||||
.g(255)
|
||||
.b(255)
|
||||
.build();
|
||||
|
||||
EmbedField field = EmbedField
|
||||
.builder()
|
||||
.value(FIELD_VALUE)
|
||||
.inline(true)
|
||||
.name("name")
|
||||
.build();
|
||||
|
||||
EmbedFooter footer = EmbedFooter
|
||||
.builder()
|
||||
.icon(EXAMPLE_URL)
|
||||
.text("text")
|
||||
.build();
|
||||
|
||||
EmbedTitle title = EmbedTitle
|
||||
.builder()
|
||||
.title("title")
|
||||
.url(EXAMPLE_URL)
|
||||
.build();
|
||||
return EmbedConfiguration
|
||||
.builder()
|
||||
.fields(Arrays.asList(field))
|
||||
.footer(footer)
|
||||
.author(author)
|
||||
.title(title)
|
||||
.color(color)
|
||||
.description("description")
|
||||
.additionalMessage("additionalMessage")
|
||||
.imageUrl(EXAMPLE_URL)
|
||||
.thumbnail(EXAMPLE_URL)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Template getSimpleTemplate() throws IOException, TemplateException {
|
||||
return new Template(TEMPLATE_KEY, SIMPLE_TEMPLATE_SOURCE, getNonMockedConfiguration());
|
||||
}
|
||||
|
||||
private Template getEmbedTemplateWithDescription(String description) throws IOException, TemplateException {
|
||||
return new Template(getEmbedTemplateKey(), embedTemplateWithDescription(description), getNonMockedConfiguration());
|
||||
}
|
||||
|
||||
private Template getPageCountTemplate(Integer page) throws IOException, TemplateException {
|
||||
return new Template(EMBED_PAGE_COUNT_TEMPLATE, getEmbedPageCount(page), getNonMockedConfiguration());
|
||||
}
|
||||
|
||||
private Template getEmbedTemplateWithFallFieldsUsedOnce() throws IOException, TemplateException {
|
||||
return new Template(getEmbedTemplateKey(), getFullEmbedConfigString(), getNonMockedConfiguration());
|
||||
}
|
||||
|
||||
private Template getEmbedTemplateWithFieldCount(Integer count) throws IOException, TemplateException {
|
||||
return new Template(getEmbedTemplateKey(), getFieldsEmbedConfigAsString(count), getNonMockedConfiguration());
|
||||
}
|
||||
|
||||
private Template getEmbedTemplateWithTooLongField(String value) throws IOException, TemplateException {
|
||||
return new Template(getEmbedTemplateKey(), getSingleFieldWithValue(value), getNonMockedConfiguration());
|
||||
}
|
||||
|
||||
private String getFullEmbedConfigString() throws IOException {
|
||||
return IOUtils.toString(this.getClass().getResourceAsStream("/src/test/resources/full_embed.json"), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private String getFieldsEmbedConfigAsString(Integer count) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("{\"fields\": [");
|
||||
for (int i = 0; i < count - 1; i++) {
|
||||
sb.append(FIELD_TEMPLATE + ",");
|
||||
}
|
||||
sb.append(FIELD_TEMPLATE +
|
||||
"]\n" +
|
||||
"}");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private String getSingleFieldWithValue(String value) {
|
||||
return String.format("{\"fields\": [" + FIELD_TEMPLATE_WITH_VALUE + "]\n}",value);
|
||||
}
|
||||
|
||||
private String embedTemplateWithDescription(String description) {
|
||||
return String.format("{ \"description\": \"%s\"}", description);
|
||||
}
|
||||
|
||||
private String getEmbedPageCount(Integer page) {
|
||||
return String.format("Page %d", page);
|
||||
}
|
||||
|
||||
private Configuration getNonMockedConfiguration() throws IOException, TemplateException {
|
||||
return new FreeMarkerConfigurationFactory().createConfiguration();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package dev.sheldan.abstracto.core.templating.service.management;
|
||||
|
||||
import dev.sheldan.abstracto.core.templating.model.database.Template;
|
||||
import dev.sheldan.abstracto.core.templating.repository.TemplateRepository;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class TemplateManagementServiceBeanTest {
|
||||
|
||||
@InjectMocks
|
||||
private TemplateManagementServiceBean templateManagementServiceBean;
|
||||
|
||||
@Mock
|
||||
private TemplateRepository repository;
|
||||
|
||||
private static final String TEMPLATE_KEY = "templateKey";
|
||||
private static final String TEMPLATE_SOURCE = "source";
|
||||
|
||||
@Test
|
||||
public void testFindByKey() {
|
||||
when(repository.findById(TEMPLATE_KEY)).thenReturn(Optional.of(getTemplate()));
|
||||
Optional<Template> templateByKey = templateManagementServiceBean.getTemplateByKey(TEMPLATE_KEY);
|
||||
templateByKey.ifPresent(template -> {
|
||||
Assert.assertEquals(TEMPLATE_KEY, template.getKey());
|
||||
Assert.assertEquals(TEMPLATE_SOURCE, template.getContent());
|
||||
});
|
||||
Assert.assertTrue(templateByKey.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotFindTemplate() {
|
||||
when(repository.findById(TEMPLATE_KEY)).thenReturn(Optional.empty());
|
||||
Optional<Template> templateByKey = templateManagementServiceBean.getTemplateByKey(TEMPLATE_KEY);
|
||||
Assert.assertFalse(templateByKey.isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTemplateExists() {
|
||||
when(repository.existsById(TEMPLATE_KEY)).thenReturn(true);
|
||||
Assert.assertTrue(templateManagementServiceBean.templateExists(TEMPLATE_KEY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateTemplate() {
|
||||
Template template = templateManagementServiceBean.createTemplate(TEMPLATE_KEY, TEMPLATE_SOURCE);
|
||||
Assert.assertEquals(template.getContent(), TEMPLATE_SOURCE);
|
||||
Assert.assertEquals(template.getKey(), TEMPLATE_KEY);
|
||||
verify(repository, times(1)).save(any(Template.class));
|
||||
Assert.assertTrue(Duration.between(template.getLastModified(), Instant.now()).getSeconds() < 1);
|
||||
}
|
||||
|
||||
private Template getTemplate() {
|
||||
return Template.builder().content(TEMPLATE_SOURCE).key(TEMPLATE_KEY).build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"author": {
|
||||
"name": "name",
|
||||
"avatar": "https://example.com",
|
||||
"url": "https://example.com"
|
||||
},
|
||||
"color" : {
|
||||
"r": 255,
|
||||
"g": 255,
|
||||
"b": 255
|
||||
},
|
||||
"description": "description",
|
||||
"thumbnail": "https://example.com",
|
||||
"imageURL": "https://example.com",
|
||||
"fields": [
|
||||
{
|
||||
"name": "name",
|
||||
"value": "value",
|
||||
"inline": "true"
|
||||
}
|
||||
],
|
||||
"footer": {
|
||||
"text": "text",
|
||||
"icon": "https://example.com"
|
||||
},
|
||||
"timeStamp": "timestamp",
|
||||
"additionalMessage": "additionalMessage"
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
mock-maker-inline
|
||||
Reference in New Issue
Block a user