Skip to content

Commit 44a0972

Browse files
committed
Added processing single a multiple comics [#4]
1 parent 5476288 commit 44a0972

3 files changed

Lines changed: 105 additions & 28 deletions

File tree

src/main/java/org/comixedproject/plugins/groovy/GroovyPluginRuntime.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import lombok.extern.log4j.Log4j2;
2828
import org.comixedproject.model.plugin.LibraryPlugin;
2929
import org.comixedproject.model.plugin.LibraryPluginProperty;
30+
import org.comixedproject.model.plugin.PluginType;
3031
import org.comixedproject.plugins.AbstractPluginRuntime;
3132
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
3233
import org.springframework.context.annotation.Scope;
@@ -68,6 +69,19 @@ public String getVersion(final String filename) {
6869
}
6970
}
7071

72+
@Override
73+
public PluginType getPluginType(final String filename) {
74+
final GroovyShell shell = doCreatePluginShell();
75+
try {
76+
log.trace("Loading plugin properties: {}", filename);
77+
final Script script = shell.parse(new File(filename));
78+
return (PluginType) script.invokeMethod("plugin_type", new Object[] {});
79+
} catch (Exception error) {
80+
log.error("Failed to load plugin properties", error);
81+
return PluginType.Undefined;
82+
}
83+
}
84+
7185
@Override
7286
public List<LibraryPluginProperty> getProperties(final String filename) {
7387
final GroovyShell shell = doCreatePluginShell();
@@ -83,20 +97,30 @@ public List<LibraryPluginProperty> getProperties(final String filename) {
8397
}
8498

8599
@Override
86-
public Boolean execute(final LibraryPlugin libraryPlugin) {
100+
public void execute(final LibraryPlugin libraryPlugin, final Long comicBookId) {
101+
final var shell = doCreatePluginShell();
102+
try {
103+
log.trace("Executing libraryPlugin: {}", libraryPlugin.getName());
104+
this.getProperties()
105+
.entrySet()
106+
.forEach(entry -> shell.setProperty(entry.getKey(), entry.getValue()));
107+
shell.parse(new File(libraryPlugin.getFilename())).invokeMethod("execute", comicBookId);
108+
} catch (Exception error) {
109+
log.error("Failed to execute libraryPlugin", error);
110+
}
111+
}
112+
113+
@Override
114+
public void execute(final LibraryPlugin libraryPlugin, final List<Long> comicBookIds) {
87115
final var shell = doCreatePluginShell();
88116
try {
89-
log.trace(
90-
"Executing libraryPlugin: {} v{}", libraryPlugin.getName(), libraryPlugin.getVersion());
117+
log.trace("Executing libraryPlugin: {}", libraryPlugin.getName());
91118
this.getProperties()
92119
.entrySet()
93120
.forEach(entry -> shell.setProperty(entry.getKey(), entry.getValue()));
94-
shell.evaluate(new File(libraryPlugin.getFilename()));
95-
log.trace("LibraryPlugin completed without error");
96-
return true;
121+
shell.parse(new File(libraryPlugin.getFilename())).invokeMethod("execute", comicBookIds);
97122
} catch (Exception error) {
98123
log.error("Failed to execute libraryPlugin", error);
99-
return false;
100124
}
101125
}
102126

src/test/java/org/comixedproject/plugins/GroovyLibraryPluginRuntimeTest.java

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020

2121
import static junit.framework.TestCase.*;
2222

23+
import java.util.ArrayList;
24+
import java.util.Arrays;
2325
import java.util.List;
2426
import org.comixedproject.model.plugin.LibraryPlugin;
2527
import org.comixedproject.model.plugin.LibraryPluginProperty;
28+
import org.comixedproject.model.plugin.PluginType;
2629
import org.comixedproject.plugins.groovy.GroovyPluginRuntime;
2730
import org.junit.jupiter.api.Test;
2831
import org.junit.jupiter.api.extension.ExtendWith;
@@ -41,87 +44,121 @@ class GroovyLibraryPluginRuntimeTest {
4144
private static final Integer TEST_PROPERTY_1_LENGTH = 32;
4245
private static final String TEST_PROPERTY_NAME_2 = "test_property_2";
4346
private static final Integer TEST_PROPERTY_2_LENGTH = 64;
47+
private static final Long TEST_COMIC_BOOK_ID = 717L;
48+
private static final List<Long> TEST_COMIC_BOOK_ID_LIST =
49+
new ArrayList<>(Arrays.asList(TEST_COMIC_BOOK_ID));
4450

4551
@InjectMocks private GroovyPluginRuntime runner;
4652
@Mock private LibraryPlugin libraryPlugin;
4753

4854
@Test
49-
void testExecuteHelloWorld() {
55+
void execute_single_helloWorld() {
5056
Mockito.when(libraryPlugin.getFilename()).thenReturn(TEST_GOOD_PLUGIN);
5157

52-
final Boolean result = runner.execute(libraryPlugin);
53-
54-
assertTrue(result);
58+
runner.execute(libraryPlugin, TEST_COMIC_BOOK_ID);
5559
}
5660

5761
@Test
58-
void testExecuteBadScript() {
62+
void execute_single_badScript() {
5963
Mockito.when(libraryPlugin.getFilename()).thenReturn(TEST_BROKEN_PLUGIN);
6064

61-
final Boolean result = runner.execute(libraryPlugin);
62-
63-
assertFalse(result);
65+
runner.execute(libraryPlugin, TEST_COMIC_BOOK_ID);
6466
}
6567

6668
@Test
67-
void testExecuteMissingScript() {
69+
void execute_single_missingScript() {
6870
Mockito.when(libraryPlugin.getFilename()).thenReturn(TEST_BROKEN_PLUGIN.substring(1));
6971

70-
final Boolean result = runner.execute(libraryPlugin);
72+
runner.execute(libraryPlugin, TEST_COMIC_BOOK_ID);
73+
}
74+
75+
@Test
76+
void execute_list_helloWorld() {
77+
Mockito.when(libraryPlugin.getFilename()).thenReturn(TEST_GOOD_PLUGIN);
78+
79+
runner.execute(libraryPlugin, TEST_COMIC_BOOK_ID_LIST);
80+
}
81+
82+
@Test
83+
void execute_list_badScript() {
84+
Mockito.when(libraryPlugin.getFilename()).thenReturn(TEST_BROKEN_PLUGIN);
85+
86+
runner.execute(libraryPlugin, TEST_COMIC_BOOK_ID_LIST);
87+
}
88+
89+
@Test
90+
void execute_list_missingScript() {
91+
Mockito.when(libraryPlugin.getFilename()).thenReturn(TEST_BROKEN_PLUGIN.substring(1));
7192

72-
assertFalse(result);
93+
runner.execute(libraryPlugin, TEST_COMIC_BOOK_ID_LIST);
7394
}
7495

7596
@Test
76-
void testGetName() {
97+
void getName() {
7798
final String result = runner.getName(TEST_GOOD_PLUGIN);
7899

79100
assertNotNull(result);
80101
assertEquals(TEST_PLUGIN_NAME, result);
81102
}
82103

83104
@Test
84-
void testGetNameBadScript() {
105+
void getName_badScript() {
85106
final String result = runner.getName(TEST_BROKEN_PLUGIN);
86107

87108
assertNotNull(result);
88109
assertEquals("", result);
89110
}
90111

91112
@Test
92-
void testGetNameMissingPlugin() {
113+
void getName_missingPlugin() {
93114
final String result = runner.getName(TEST_BROKEN_PLUGIN.substring(1));
94115

95116
assertNotNull(result);
96117
assertEquals("", result);
97118
}
98119

99120
@Test
100-
void testGetVersion() {
121+
void getVersion() {
101122
final String result = runner.getVersion(TEST_GOOD_PLUGIN);
102123

103124
assertNotNull(result);
104125
assertEquals(TEST_PLUGIN_VERSION, result);
105126
}
106127

107128
@Test
108-
void testGetVersionBadScript() {
129+
void getVersion_badScript() {
109130
final String result = runner.getVersion(TEST_BROKEN_PLUGIN);
110131

111132
assertNotNull(result);
112133
assertEquals("", result);
113134
}
114135

115136
@Test
116-
void testGetVersionMissingPlugin() {
137+
void getVersion_missingPlugin() {
117138
final String result = runner.getVersion(TEST_BROKEN_PLUGIN.substring(1));
118139

119140
assertNotNull(result);
120141
assertEquals("", result);
121142
}
122143

123144
@Test
124-
void testLoadProperties() {
145+
void getPluginType() {
146+
final PluginType result = runner.getPluginType(TEST_GOOD_PLUGIN);
147+
148+
assertNotNull(result);
149+
assertSame(PluginType.Single, result);
150+
}
151+
152+
@Test
153+
void getPluginType_missingScript() {
154+
final PluginType result = runner.getPluginType(TEST_BROKEN_PLUGIN.substring(1));
155+
156+
assertNotNull(result);
157+
assertSame(PluginType.Undefined, result);
158+
}
159+
160+
@Test
161+
void loadProperties() {
125162
final List<LibraryPluginProperty> result = runner.getProperties(TEST_GOOD_PLUGIN);
126163

127164
assertNotNull(result);
@@ -149,15 +186,15 @@ void testLoadProperties() {
149186
}
150187

151188
@Test
152-
void testLoadPropertiesBadScript() {
189+
void loadProperties_badScript() {
153190
final List<LibraryPluginProperty> result = runner.getProperties(TEST_BROKEN_PLUGIN);
154191

155192
assertNotNull(result);
156193
assertTrue(result.isEmpty());
157194
}
158195

159196
@Test
160-
void testLoadPropertiesMissingPlugin() {
197+
void loadProperties_missingPlugin() {
161198
final List<LibraryPluginProperty> result =
162199
runner.getProperties(TEST_BROKEN_PLUGIN.substring(1));
163200

src/test/resources/good.cxplugin

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
import org.comixedproject.model.plugin.LibraryPluginProperty
2+
import org.comixedproject.model.plugin.PluginType
23

4+
5+
// Returns the name of the plugin, displayed in the plugin menu and plugin tab
36
def plugin_name() { return "Good Plugin" }
7+
8+
// Returns the version of the plugin, displayed in the plugin tab
49
def plugin_version() { return "1.2.3.4" }
10+
11+
// Returns the type of plugin
12+
def plugin_type() { return PluginType.Single }
13+
14+
/* Returns the list of properties that will be populated when the plugin runs
15+
* The properties have a name, a length, and a default value. When the plugin
16+
* is configured, these default values can be overwritten with new values.
17+
*/
518
def plugin_properties() {
619
var property1 = LibraryPluginProperty.createProperty("test_property_1", 32, "")
720
var property2 = LibraryPluginProperty.createRequiredProperty("test_property_2", 64, "default value")
821

922
return [property1, property2]
1023
}
1124

12-
print "Hello World!"
25+
// here is where the actual plugin functionality is
26+
def execute(Long comicBookId) {
27+
print "Hello world! id=${comicBookId}"
28+
}

0 commit comments

Comments
 (0)