Skip to content

Commit

Permalink
Duplicates deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric Dutoit committed Mar 15, 2024
1 parent 1c52856 commit 89a3e3a
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 39 deletions.
13 changes: 9 additions & 4 deletions src/main/java/ch/mno/copper/stories/StoriesFacadeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import ch.mno.copper.stories.data.Story;
import ch.mno.copper.stories.data.StoryGrammar;
import ch.mno.copper.stories.data.StoryValidationResult;
import config.CopperMailProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
Expand All @@ -29,10 +31,13 @@ public class StoriesFacadeImpl implements StoriesFacade {
private final DiskHelper diskHelper;
private final StoryTaskBuilder storyTaskBuilder;

public StoriesFacadeImpl(DiskHelper diskHelper, StoryTaskBuilder storyTaskBuilder, StoryGrammar grammar) {
private final CopperMailProperties copperMailProperties;

public StoriesFacadeImpl(DiskHelper diskHelper, StoryTaskBuilder storyTaskBuilder, StoryGrammar grammar, CopperMailProperties copperMailProperties) {
this.diskHelper = diskHelper;
this.storyTaskBuilder = storyTaskBuilder;
this.grammar = grammar;
this.copperMailProperties = copperMailProperties;
}

@Override
Expand All @@ -48,7 +53,7 @@ public List<Story> getStories(boolean shouldRefreshFromDisk) {

@Override
public Story buildStory(FileInputStream fileInputStream, String storyName) throws IOException, ConnectorException {
return new Story(grammar, fileInputStream, storyName);
return new Story(grammar, fileInputStream, storyName, copperMailProperties);
}

@Override
Expand All @@ -58,7 +63,7 @@ public StoryTask buildStoryTask(Story story, ValuesStore valuesStore) {

@Override
public String saveNewStory(String storyName, String storyText) throws IOException {
Story story = new Story(grammar, storyName, storyText);
Story story = new Story(grammar, storyName, storyText, copperMailProperties);
diskHelper.saveNewStory(storyName, story.getStoryText());
storiesHolder.add(story); // TODO: update listeneres
return "Ok";
Expand All @@ -71,7 +76,7 @@ public String updateStory(String originalStoryName, String storyName, String sto
}

Story originalStory = getStoryByName(originalStoryName);
Story story = new Story(grammar, storyName, storyText);
Story story = new Story(grammar, storyName, storyText, copperMailProperties);

// Update
diskHelper.updateStory(story.getName(), story.getStoryText());
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/ch/mno/copper/stories/data/Story.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
public class Story {

private static final Logger LOG = LoggerFactory.getLogger(Story.class);
private final CopperMailProperties copperMailProperties;
private When when = null;
private String storyName;
private String storyText;
Expand All @@ -31,15 +32,14 @@ public class Story {
private boolean valid;
private final String given;
private transient AbstractReporterWrapper reporterWrapper;
private CopperMailProperties copperMailProperties;


public Story(StoryGrammar grammar, String storyName, String storyText) throws IOException {
this(grammar, new ByteArrayInputStream(storyText.getBytes()), storyName);
public Story(StoryGrammar grammar, String storyName, String storyText, CopperMailProperties copperMailProperties) throws IOException {
this(grammar, new ByteArrayInputStream(storyText.getBytes()), storyName, copperMailProperties);
}


public Story(StoryGrammar grammar, InputStream is, String storyName) throws IOException {
public Story(StoryGrammar grammar, InputStream is, String storyName, CopperMailProperties copperMailProperties) throws IOException {
this.copperMailProperties = copperMailProperties;
this.storyName = storyName;
storyText = IOUtils.toString(is);
storyText = Pattern.compile("#.*?\n", Pattern.DOTALL).matcher(storyText).replaceAll(""); // Remove comments lines
Expand Down Expand Up @@ -86,10 +86,6 @@ public Story(StoryGrammar grammar, InputStream is, String storyName) throws IOEx
this.reporterWrapper = new ReporterWrapperFactory(copperMailProperties).buildReporterWrapper(grammar, storyReporter);
}

public void setCopperMailProperties(CopperMailProperties copperMailProperties) {
this.copperMailProperties = copperMailProperties;
}

static boolean matchWhen(String storedValue, String operator, String expectedValue) {
if (expectedValue.contains(".")) {
float a = Float.parseFloat(storedValue);
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/ch/mno/copper/DataProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ch.mno.copper.stories.data.Story;
import ch.mno.copper.stories.data.StoryGrammar;
import ch.mno.copper.stories.data.StoryValidationResult;
import config.CopperMailProperties;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
Expand Down Expand Up @@ -36,7 +37,7 @@ void testAfterNewStory() throws IOException, ConnectorException {
storiesFacade.stories.put("story1", new Story(storyGrammar, "MyStory1Name", "RUN ON CRON */5 * * * *\n" +
"GIVEN COLLECTOR WEB WITH url=https://localhost/temp1\n" +
" KEEP body AS ELDORA_MENU_VALUES\n" +
"THEN STORE VALUES"));
"THEN STORE VALUES", new CopperMailProperties()));
assertEquals(1, dataprovider.getStories().size());

// Get story
Expand All @@ -46,7 +47,7 @@ void testAfterNewStory() throws IOException, ConnectorException {
storiesFacade.stories.put("story1", new Story(storyGrammar, "MyStory1Name", "RUN ON CRON */5 * * * *\n" +
"GIVEN COLLECTOR WEB WITH url=https://localhost/temp2\n" +
" KEEP body AS ELDORA_MENU_VALUES\n" +
"THEN STORE VALUES"));
"THEN STORE VALUES", new CopperMailProperties()));

// Get story new
assertTrue(dataprovider.getStories().get(0).getStoryText().contains("temp2"));
Expand Down
11 changes: 6 additions & 5 deletions src/test/java/ch/mno/copper/collect/StoryTaskTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ch.mno.copper.stories.StoryTaskBuilder;
import ch.mno.copper.stories.data.Story;
import ch.mno.copper.stories.data.StoryGrammar;
import config.CopperMailProperties;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -48,7 +49,7 @@ void testCronMinute2() {

@Test
void testBuildAndRunOk() throws IOException {
Story story = new Story(grammar, "StoryName", "RUN ON CRON 0 8,13 * * *\nGIVEN STORED VALUES\nTHEN\nSTORE VALUES");
Story story = new Story(grammar, "StoryName", "RUN ON CRON 0 8,13 * * *\nGIVEN STORED VALUES\nTHEN\nSTORE VALUES", new CopperMailProperties());

CollectorWrapperFactory collectorWrapperFactory = new CollectorWrapperFactory(Mockito.mock(PropertyResolver.class), grammar) {
public AbstractCollectorWrapper build(String storyGiven) {
Expand Down Expand Up @@ -80,7 +81,7 @@ public List<List<String>> execute2D() {

@Test
void testBuildAndRunForErrorShouldStoreEntriesAsError() throws IOException, ConnectorException {
Story story = new Story(grammar, "StoryName", "RUN ON CRON 0 8,13 * * *\nGIVEN STORED VALUES\nTHEN\nSTORE VALUES");
Story story = new Story(grammar, "StoryName", "RUN ON CRON 0 8,13 * * *\nGIVEN STORED VALUES\nTHEN\nSTORE VALUES", new CopperMailProperties());

ValuesStore vs = new MapValuesStore();
CollectorWrapperFactory collectorWrapperFactory = new CollectorWrapperFactory(Mockito.mock(PropertyResolver.class), grammar) {
Expand Down Expand Up @@ -116,7 +117,7 @@ void testAttributes() throws IOException {
"GIVEN COLLECTOR WEB WITH url=http://localhost:50400\n" +
" KEEP responseCode AS COPPER2_WEB_RETURN_CODE\n" +
"THEN STORE VALUES";
Story story = new Story(grammar, "storyName.txt", storyText);
Story story = new Story(grammar, "storyName.txt", storyText, new CopperMailProperties());
var task = new StoryTaskImpl(story, ()->{}, "* * * * *");
assertEquals("storyName.txt", task.storyName());
assertEquals("storyName", task.getTitle());
Expand All @@ -130,7 +131,7 @@ void lifecycle() throws IOException {
"GIVEN COLLECTOR WEB WITH url=http://localhost:50400\n" +
" KEEP responseCode AS COPPER2_WEB_RETURN_CODE\n" +
"THEN STORE VALUES";
Story story = new Story(grammar, "storyName.txt", storyText);
Story story = new Story(grammar, "storyName.txt", storyText, new CopperMailProperties());
var task = new StoryTaskImpl(story, ()->{}, "* * * * *");
long v = System.currentTimeMillis() - 1;
task.cronData.setNextRun4Test(v); // Don't wait a minute
Expand All @@ -147,7 +148,7 @@ void testNextRun() throws IOException {
"GIVEN COLLECTOR WEB WITH url=http://localhost:50400\n" +
" KEEP responseCode AS COPPER2_WEB_RETURN_CODE\n" +
"THEN STORE VALUES";
Story story = new Story(grammar, "storyName.txt", storyText);
Story story = new Story(grammar, "storyName.txt", storyText, new CopperMailProperties());
var task = new StoryTaskImpl(story, ()->{}, "* * 31 12 *");
assertTrue(task.getNextRun()>System.currentTimeMillis());
assertFalse(task.shouldRun());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public void report(String message, Map<String, String> values) {
valuesStore.put("TwoStatus", "status2");
wrapper.execute(new HashMap<>(), valuesStore);

assertEquals("<h3>Ref-Mon detected status changes:</h3><br/>OneStatus: status1<br/>TwoStatus: status2;{ROOM_ID=someroom, TOKEN=sometoken}", ret.toString());
assertEquals("<h3>Ref-Mon detected status changes:</h3><br/>- OneStatus: status1\n" +
"- TwoStatus: status2;{ROOM_ID=someroom, TOKEN=sometoken}", ret.toString());
}

}
2 changes: 1 addition & 1 deletion src/test/java/ch/mno/copper/report/WebexReporterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ HttpConnector buildConnector() {
};
reporter.report("aMessage with {{STATUS}}", values);

assertEquals("[Bearer: 1token, Content-Type: application/json]", Arrays.toString(post.getValue().getAllHeaders()));
assertEquals("[Authorization: Bearer 1token, Content-Type: application/json]", Arrays.toString(post.getValue().getAllHeaders()));
assertEquals("{\"roomId\": \"2room\", \"text\":\"aMessage with {{STATUS}}\"}", IOUtils.toString(post.getValue().getEntity().getContent(), StandardCharsets.UTF_8));
} catch (IOException e) {
Assert.fail(e.getMessage());
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/ch/mno/copper/stories/StoryGrammarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ch.mno.copper.helpers.SyntaxHelper;
import ch.mno.copper.stories.data.Story;
import ch.mno.copper.stories.data.StoryGrammar;
import config.CopperMailProperties;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -234,7 +235,7 @@ void testJdbcStory() throws IOException, ConnectorException, URISyntaxException


Path path = Paths.get(resource.toURI());
Story story = new Story(storyGrammar, new FileInputStream(path.toFile()), "OracleStory1.txt");
Story story = new Story(storyGrammar, new FileInputStream(path.toFile()), "OracleStory1.txt", new CopperMailProperties());
CollectorWrapperFactory collectorWrapperFactory = new CollectorWrapperFactory(Mockito.mock(PropertyResolver.class), storyGrammar);
JdbcCollectorWrapper wrapper = (JdbcCollectorWrapper) collectorWrapperFactory.build(story.getGiven());
assertEquals("jdbc:oracle:thin:@//myhost:1521/orcl", wrapper.getUrl());
Expand All @@ -253,7 +254,7 @@ void testJdbcStory2() throws IOException, ConnectorException, URISyntaxException


Path path = Paths.get(resource.toURI());
Story story = new Story(storyGrammar, new FileInputStream(path.toFile()), "OracleStory2.txt");
Story story = new Story(storyGrammar, new FileInputStream(path.toFile()), "OracleStory2.txt", new CopperMailProperties());
CollectorWrapperFactory collectorWrapperFactory = new CollectorWrapperFactory(Mockito.mock(PropertyResolver.class), storyGrammar);
JdbcCollectorWrapper wrapper = (JdbcCollectorWrapper) collectorWrapperFactory.build(story.getGiven());
assertEquals("jdbc:oracle:thin:@host:port/instance", wrapper.getUrl());
Expand Down
16 changes: 9 additions & 7 deletions src/test/java/ch/mno/copper/stories/data/StoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import ch.mno.copper.store.ValuesStore;
import ch.mno.copper.store.data.InstantValues;
import ch.mno.copper.stories.StoryException;
import config.CopperMailProperties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -23,6 +24,7 @@
*/
class StoryTest {
StoryGrammar grammar;
CopperMailProperties copperMailProperties = new CopperMailProperties();


@BeforeEach
Expand Down Expand Up @@ -60,7 +62,7 @@ void testStory() throws IOException {
"THEN REPORT BY MAIL to \"test@dummy\"\n" +
" WITH title=\"some title\"\n" +
" WITH message=\"Yo !\"\n";
Story story = new Story(grammar, "testStory", storyText);
Story story = new Story(grammar, "testStory", storyText,copperMailProperties);
ValuesStore store = buildValuesStore();

// Test values
Expand Down Expand Up @@ -101,7 +103,7 @@ void testStoryOK() throws IOException {
"WHEN AAA>0\n" +
"THEN REPORT BY MAIL to \"test@dummy\"\n" +
" WITH title=\"some title\"\n" +
" WITH message=\"Yo !\"\n");
" WITH message=\"Yo !\"\n", copperMailProperties);
assertTrue(story.isValid());
assertFalse(story.hasError());
assertTrue(story.getWhen()!=null);
Expand All @@ -114,7 +116,7 @@ void testStoryOK_Daily() throws IOException {
"WHEN AAA>0\n" +
"THEN REPORT BY MAIL to \"test@dummy\"\n" +
" WITH title=\"some title\"\n" +
" WITH message=\"Yo !\"\n");
" WITH message=\"Yo !\"\n", copperMailProperties);
assertEquals("34 12 * * *", story.getCron());
}

Expand All @@ -125,7 +127,7 @@ void testStoryWrongSyntaxMissingGiven() {
"WHEN AAA>0\n" +
"THEN REPORT BY MAIL to \"test@dummy\"\n" +
" WITH title=\"some title\"\n" +
" WITH message=\"Yo !\"\n"));
" WITH message=\"Yo !\"\n", copperMailProperties));
}

@Test()
Expand All @@ -135,15 +137,15 @@ void testStoryWrongSyntaxMissingRunOn() {
"WHEN AAA>0\n" +
"THEN REPORT BY MAIL to \"test@dummy\"\n" +
" WITH title=\"some title\"\n" +
" WITH message=\"Yo !\"\n"));
" WITH message=\"Yo !\"\n", copperMailProperties));
}

@Test()
void testStoryWrongSyntaxMissingReporter() {
assertThrows(StoryException.class, () -> new Story(grammar, "testStory", "RUN ON CRON 5 * * * *\n" +
"GIVEN STORED VALUES\n" +
"WHEN AAA>0\n" +
"THEN WRONG_SYNTAX_IN_STORY"));
"THEN WRONG_SYNTAX_IN_STORY", copperMailProperties));
}


Expand All @@ -155,7 +157,7 @@ void testStoryWrongSyntax() throws IOException {
"AND WRONG_SYNTAX_IN_QUERY\n" +
"THEN REPORT BY MAIL to \"test@dummy\"\n" +
" WITH title=\"some title\"\n" +
" WITH message=\"Yo !\"\n");
" WITH message=\"Yo !\"\n", copperMailProperties);
assertFalse(story.isValid());
assertTrue(story.hasError());
assertTrue(story.getError().contains("does not match"));
Expand Down
15 changes: 8 additions & 7 deletions src/test/java/ch/mno/copper/web/CopperAdminServicesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import ch.mno.copper.stories.data.Story;
import ch.mno.copper.stories.data.StoryGrammar;
import ch.mno.copper.web.dto.StoryPostDTO;
import config.CopperMailProperties;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -65,7 +66,7 @@ void postStory_update() throws IOException, ConnectorException {

StoriesFacade storiesFacade = Mockito.mock(StoriesFacade.class);
Mockito.when(storiesFacade.updateStory("oldStory", "newStory", newStoryText)).thenReturn("OK");
Story story = new Story(grammar, "oldStory", oldStoryText);
Story story = new Story(grammar, "oldStory", oldStoryText, new CopperMailProperties());
Mockito.when(storiesFacade.getStoryByName("oldStory")).thenReturn(story);
CopperAdminServices service = new CopperAdminServices(null, storiesFacade, null);
//
Expand All @@ -85,7 +86,7 @@ void postStory_update_exception() throws IOException, ConnectorException {

StoriesFacade storiesFacade = Mockito.mock(StoriesFacade.class);
Mockito.when(storiesFacade.updateStory("oldStory", "newStory", newStoryText)).thenThrow(new IOException("Exception for test"));
Story story = new Story(grammar, "oldStory", oldStoryText);
Story story = new Story(grammar, "oldStory", oldStoryText, new CopperMailProperties());
Mockito.when(storiesFacade.getStoryByName("oldStory")).thenReturn(story);
CopperAdminServices service = new CopperAdminServices(null, storiesFacade, null);
//
Expand Down Expand Up @@ -147,8 +148,8 @@ void getStories() throws IOException, ConnectorException {
"GIVEN COLLECTOR WEB WITH url=http://localhost:50400\n" +
" KEEP responseCode AS COPPER2_WEB_RETURN_CODE\n" +
"THEN STORE VALUES";
Story story1 = new Story(grammar, "story1", storyText1);
Story story2 = new Story(grammar, "story2", storyText2);
Story story1 = new Story(grammar, "story1", storyText1, new CopperMailProperties());
Story story2 = new Story(grammar, "story2", storyText2, new CopperMailProperties());
//
StoriesFacade storiesFacade = Mockito.mock(StoriesFacade.class);
Mockito.when(storiesFacade.getStories(true)).thenReturn(Arrays.asList(story1, story2));
Expand All @@ -165,7 +166,7 @@ void getStory() throws IOException, ConnectorException {
"GIVEN COLLECTOR WEB WITH url=http://localhost:30400\n" +
" KEEP responseCode AS COPPER_WEB_RETURN_CODE\n" +
"THEN STORE VALUES";
Story story1 = new Story(grammar, "story1", storyText1);
Story story1 = new Story(grammar, "story1", storyText1, new CopperMailProperties());
StoriesFacade storiesFacade = Mockito.mock(StoriesFacade.class);
Mockito.when(storiesFacade.getStoryByName("story1")).thenReturn(story1);
CopperAdminServices service = new CopperAdminServices(null, storiesFacade, null);
Expand Down Expand Up @@ -198,8 +199,8 @@ void getOverview() throws IOException, ConnectorException {
"GIVEN COLLECTOR WEB WITH url=http://localhost:50400\n" +
" KEEP responseCode AS COPPER2_WEB_RETURN_CODE\n" +
"THEN STORE VALUES";
Story story1 = new Story(grammar, "story1", storyText1);
Story story2 = new Story(grammar, "story2", storyText2);
Story story1 = new Story(grammar, "story1", storyText1, new CopperMailProperties());
Story story2 = new Story(grammar, "story2", storyText2, new CopperMailProperties());
//
StoriesFacade storiesFacade = Mockito.mock(StoriesFacade.class);
Mockito.when(storiesFacade.getStories(true)).thenReturn(Arrays.asList(story1, story2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ch.mno.copper.web.adapters.JsonStoryAdapter;
import ch.mno.copper.web.dto.StoryWEBDTO;
import com.google.gson.stream.JsonWriter;
import config.CopperMailProperties;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand All @@ -31,7 +32,7 @@ void testAll() throws IOException {
Story story = new Story(grammar, "storyName", "RUN ON CRON */5 * * * *\n" +
"GIVEN COLLECTOR WEB WITH url=http://localhost:30400\n" +
" KEEP responseCode AS COPPER_WEB_RETURN_CODE\n" +
"THEN STORE VALUES");
"THEN STORE VALUES", new CopperMailProperties());
story.setCronData4Test("1 2 3 4 5");
StoryWEBDTO dto = new StoryWEBDTO(story);

Expand Down

0 comments on commit 89a3e3a

Please sign in to comment.