Skip to content

Commit

Permalink
Issue #539
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-stastny committed Jun 16, 2024
1 parent f4de309 commit 266f8b2
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 29 deletions.
69 changes: 48 additions & 21 deletions sdnnt/src/main/java/cz/inovatika/sdnnt/index/Indexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,27 +215,7 @@ public static JSONObject add(String collection, List<SolrInputDocument> recs, bo

public static boolean historyCase(Object historieStavu, Case cs) {
if (historieStavu != null) {
List<Pair<String,Date>> comments = new ArrayList<>();
try {
JSONArray jsonArray = new JSONArray(historieStavu.toString());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String comment = jsonObject.optString("comment");
if (comment != null) {
String date = jsonObject.getString("date");
if (comment.contains("scheduler/")) {
Date parsed = EUIPOCancelServiceImpl.HISTORY_DATE_INPUTFORMAT.parse(date);
comments.add(Pair.of(comment, parsed));
}

}
}
Collections.sort(comments,(left,right) ->{
return left.getRight().compareTo(right.getRight());
});
} catch (JSONException | ParseException e) {
LOGGER.log(Level.SEVERE,e.getMessage(),e);
}
List<Pair<String, Date>> comments = sortedHistorySKCCase(historieStavu);

if (!comments.isEmpty()) {
Pair<String, Date> last = comments.get(comments.size() - 1);
Expand All @@ -245,6 +225,53 @@ public static boolean historyCase(Object historieStavu, Case cs) {
return false;
}

public static List<Pair<String, Date>> sortedHistorySKCCase(Object historieStavu) {
List<Pair<String,Date>> comments = new ArrayList<>();
try {
JSONArray jsonArray = new JSONArray(historieStavu.toString());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String comment = jsonObject.optString("comment");
if (comment != null) {
String date = jsonObject.getString("date");
if (comment.contains("scheduler/")) {
Date parsed = EUIPOCancelServiceImpl.HISTORY_DATE_INPUTFORMAT.parse(date);
comments.add(Pair.of(comment, parsed));
}

}
}
Collections.sort(comments,(left,right) ->{
return left.getRight().compareTo(right.getRight());
});
} catch (JSONException | ParseException e) {
LOGGER.log(Level.SEVERE,e.getMessage(),e);
}
return comments;
}

public static List<Pair<String, Date>> sortedHistory(Object historieStavu) {
List<Pair<String,Date>> comments = new ArrayList<>();
try {
JSONArray jsonArray = new JSONArray(historieStavu.toString());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String comment = jsonObject.optString("comment");
if (comment != null) {
String date = jsonObject.getString("date");
Date parsed = EUIPOCancelServiceImpl.HISTORY_DATE_INPUTFORMAT.parse(date);
comments.add(Pair.of(comment, parsed));
}
}
Collections.sort(comments,(left,right) ->{
return left.getRight().compareTo(right.getRight());
});
} catch (JSONException | ParseException e) {
LOGGER.log(Level.SEVERE,e.getMessage(),e);
}
return comments;
}

// keepDNTFields = true => zmena vsech poli krome DNT (990, 992)
// keepDNTFields = false => zmena pouze DNT (990, 992) poli
static SolrInputDocument mergeWithHistory(String jsTarget,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cz.inovatika.sdnnt.services.impl;

import cz.inovatika.sdnnt.Options;
import cz.inovatika.sdnnt.index.Indexer;
import cz.inovatika.sdnnt.indexer.models.NotificationInterval;
import cz.inovatika.sdnnt.indexer.models.notifications.AbstractNotification;
import cz.inovatika.sdnnt.indexer.models.notifications.AbstractNotification.TYPE;
Expand All @@ -10,6 +11,7 @@
import cz.inovatika.sdnnt.model.CuratorItemState;
import cz.inovatika.sdnnt.model.PublicItemState;
import cz.inovatika.sdnnt.model.User;
import cz.inovatika.sdnnt.model.workflow.duplicate.Case;
import cz.inovatika.sdnnt.rights.Role;
import cz.inovatika.sdnnt.services.MailService;
import cz.inovatika.sdnnt.services.NotificationsService;
Expand Down Expand Up @@ -410,7 +412,15 @@ protected List<Map<String, String>> processRuleBasedNotification(User user, Noti
if (dntStavStr.equals(PublicItemState.D.name()) || kuratorStavStr.equals(CuratorItemState.DX.name()) || kuratorStavStr.equals(CuratorItemState.PX.name())) {
// ommiting
} else {
documents.add(map);
if (historieStavu != null) {
List<Pair<String,Date>> sortedHistory = Indexer.sortedHistory(historieStavu);
String comment = sortedHistory.size() > 0 ? sortedHistory.get(sortedHistory.size() -1).getLeft() : "";
if (StringUtils.isAnyString(comment) && comment.contains("SKC_")) {
// ommit
} else {
documents.add(map);
}
}
}
}
}
Expand Down Expand Up @@ -452,6 +462,7 @@ protected List<Map<String,String>> processSimpleNotification(User user, Notifica
String dntStavStr = dntstav.size() == 1 ? (String) new ArrayList<>(dntstav).get(0) : dntstav.toString();
String kuratorStavStr = kuratorstav.size() == 1 ? (String) new ArrayList<>(kuratorstav).get(0) : kuratorstav.toString();

String historieStavu = doc.containsKey("historie_stavu") ? (String) doc.getFieldValue("historie_stavu") : null;

Map<String, String> map = new HashMap<>();
map.put("nazev", (String) doc.getFirstValue("nazev"));
Expand All @@ -466,11 +477,17 @@ protected List<Map<String,String>> processSimpleNotification(User user, Notifica

if (dntStavStr.equals(PublicItemState.D.name()) || kuratorStavStr.equals(CuratorItemState.DX.name())|| kuratorStavStr.equals(CuratorItemState.PX.name())) {
// ommiting ; or previous state was d or dx

} else {
documents.add(map);
if (historieStavu != null) {
List<Pair<String,Date>> sortedHistory = Indexer.sortedHistory(historieStavu);
String comment = sortedHistory.size() > 0 ? sortedHistory.get(sortedHistory.size() -1).getLeft() : "";
if (StringUtils.isAnyString(comment) && comment.contains("SKC_")) {
// ommit
} else {
documents.add(map);
}
}
}

}

return doc;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package cz.inovatika.sdnnt.index;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.tuple.Pair;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -50,17 +54,16 @@ public class IndexerUtilsTests {
+ " \"user\": \"scheduler\"\r\n"
+ " }\r\n"
+ " ]";




public static final String CASE4a_FROM_SOLR = "[{\"stav\":\"PA\",\"date\":\"20200731\",\"license\":\"dnnto\",\"user\":\"batch\"},{\"stav\":\"A\",\"date\":\"20210201\",\"license\":\"dnnto\",\"user\":\"batch\"},{\"stav\":\"A\",\"date\":\"20220726\",\"license\":\"dnnto\",\"comment\":\"scheduler/SKC_4b\",\"user\":\"scheduler\"},{\"stav\":\"D\",\"date\":\"20220726\",\"zadost\":\"cd1676e3-4ffb-4e68-8ec8-01d8e2141528\",\"comment\":\"Schváleno, jde o hudebninu, viz SKC 009568289. Špatné odůvodnění má být SKC_4a.\",\"user\":\"Kurator-DC\"}]";
public static final String CASE4a_FROM_SOLR2 ="[{\"stav\":\"PA\",\"date\":\"20210131\",\"license\":\"dnnto\",\"user\":\"batch\"},{\"stav\":\"A\",\"date\":\"20210801\",\"license\":\"dnnto\",\"user\":\"batch\"},{\"stav\":\"A\",\"date\":\"20230123\",\"license\":\"dnnto\",\"comment\":\"scheduler/SKC_4a\",\"user\":\"scheduler\"},{\"stav\":\"D\",\"date\":\"20230123\",\"zadost\":\"1bff1f20-09ed-4c43-8955-a6a1479932dd\",\"comment\":\"Změna země vydání - vydáno mimo území ČR\",\"user\":\"Kurator-DC\"}]";

public static final String CASE1_FROM_SOLR3 = "[{\"stav\":\"A\",\"date\":\"20200101\",\"license\":\"dnnto\",\"user\":\"batch\"},{\"stav\":\"A\",\"date\":\"20230125\",\"license\":\"dnnto\",\"comment\":\"scheduler/SKC_1\",\"user\":\"scheduler\"},{\"stav\":\"A\",\"date\":\"20230127\",\"license\":\"dnnto\",\"comment\":\"scheduler/SKC_2a\",\"user\":\"scheduler\"}]";


@Test
public void testCase() {


Assert.assertTrue(Indexer.historyCase(CASE4ARAW, Case.SKC_4a));
Assert.assertTrue(Indexer.historyCase(CASE4BRAW, Case.SKC_4b));

Expand All @@ -73,4 +76,15 @@ public void testCase4a() {
Assert.assertTrue(Indexer.historyCase(CASE4a_FROM_SOLR, Case.SKC_4b));
Assert.assertTrue(Indexer.historyCase(CASE4a_FROM_SOLR2, Case.SKC_4a));
}

@Test
public void testSortedHistory() {
List<Pair<String,Date>> sortedHistoryCase = Indexer.sortedHistory(CASE1_FROM_SOLR3);

Pair<String, Date> pair1 = sortedHistoryCase.get(sortedHistoryCase.size() -1);
Assert.assertTrue(pair1.getLeft().equals("scheduler/SKC_2a"));

Pair<String, Date> pair2 = sortedHistoryCase.get(sortedHistoryCase.size() -2);
Assert.assertTrue(pair2.getLeft().equals("scheduler/SKC_1"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,142 @@ public void testSendNotifications_SIMPLE_RULE_STATE_PX() throws IOException, Sol



mailService.sendNotificationEmail(
EasyMock.isA(Pair.class),
EasyMock.isA(List.class)
);

EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

@Override
public Object answer() throws Throwable {
Pair<String,String> pair = (Pair<String, String>) EasyMock.getCurrentArguments()[0];
List<Map<String,String>> documents = (List<Map<String, String>>) EasyMock.getCurrentArguments()[1];
Assert.assertTrue(pair.getLeft().equals("test@testovic.cz") || pair.getLeft().equals("shib_test@testovic.cz"));

List<String> stavy = documents.stream().map(m-> {
return m.get("dntstav");
}).collect(Collectors.toList());

Assert.assertFalse(stavy.contains("D"));

documents.stream().forEach(d-> {
Assert.assertTrue(d.containsKey("license"));
});
return null;
}
}).times(2);


EasyMock.expect(service.buildClient()).andDelegateTo(
new BuildSolrClientSupport()
).anyTimes();


EasyMock.replay(mailService, controler, shibController, service);

service.saveSimpleNotification(simpleNotification("test1", "notification_knihovna_oai_aleph-nkp.cz_SKC01-000057930.json"));
service.saveSimpleNotification(simpleNotification("test1", "notification_knihovna_oai_aleph-nkp.cz_SKC01-000057932.json"));
service.saveNotificationRule(ruleNotification("test1", "notification_knihovna_rulebased_dntstavA.json"));

service.saveSimpleNotification(simpleNotification("shibtest1", "notification_testshib_oai_aleph-nkp.cz_SKC01-57931.json"));

List<AbstractNotification> notificationsByInterval = service.findNotificationsByInterval(NotificationInterval.den);
Assert.assertTrue(notificationsByInterval.size() == 4);

service.processNotifications(NotificationInterval.den);
}

/** Posilani rule notifikaci - vynechani stavu pokud v komentari bylo SKC_ */
@Test
public void testSendNotifications_SKC_Incomments() throws IOException, SolrServerException, NotificationsException, UserControlerException, EmailException {

if (!SolrTestServer.TEST_SERVER_IS_RUNNING) {
LOGGER.warning(String.format("%s is skipping", this.getClass().getSimpleName()));
return;
}

MarcRecord marcRecord1 = catalogDoc("notifications/oai:aleph-nkp.cz:DNT01-000057932");
Assert.assertNotNull(marcRecord1);

MarcRecord marcRecord2 = catalogDoc("notifications/oai:aleph-nkp.cz:DNT01-000057930");
Assert.assertNotNull(marcRecord2);

prepare.getClient().add( "catalog", marcRecord1.toSolrDoc());
prepare.getClient().add( "catalog", marcRecord2.toSolrDoc());

SolrJUtilities.quietCommit(prepare.getClient(), "catalog");


SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery("*:*");
QueryResponse catalog = prepare.getClient().query("catalog", solrQuery);
long numFound = catalog.getResults().getNumFound();
Assert.assertTrue(numFound == 2);
// saved marc record
MarcRecord solrMarc1 = MarcRecord.fromDocDep(catalog.getResults().get(0));
MarcRecord solrMarc2 = MarcRecord.fromDocDep(catalog.getResults().get(1));


solrMarc1.setKuratorStav("A", "A", License.dnnto.name(), "testuser", "SKC_1", null);
solrMarc2.setKuratorStav("A", "A", License.dnntt.name(), "testuser", "poznamka", null);

Calendar calendar1 = Calendar.getInstance();
//calendar1.add(Calendar.DAY_OF_WEEK, -1);
solrMarc1.datum_stavu = calendar1.getTime();


Calendar calendar2 = Calendar.getInstance();
calendar2.add(Calendar.DAY_OF_WEEK, -1);
solrMarc2.datum_stavu = calendar2.getTime();

// save and commit
prepare.getClient().add( "catalog", solrMarc1.toSolrDoc());
prepare.getClient().add( "catalog", solrMarc2.toSolrDoc());
SolrJUtilities.quietCommit(prepare.getClient(), "catalog");

MailServiceImpl mailService = EasyMock.createMockBuilder(MailServiceImpl.class)
.addMockedMethod("sendNotificationEmail")
.createMock();

UserController controler = EasyMock.createMock(UserController.class);
UserController shibController = EasyMock.createMock(UserController.class);

EasyMock.expect(controler.findUsersByNotificationInterval(NotificationInterval.den.name()))
.andReturn(createNotificationSimpleUsers())
.anyTimes();

EasyMock.expect(shibController.findUsersByNotificationInterval(NotificationInterval.den.name()))
.andReturn(createNotificationShibUsers())
.anyTimes();

EasyMock.expect(controler.findUser("shibtest1"))
.andReturn(null)
.anyTimes();

EasyMock.expect(shibController.findUser("shibtest1"))
.andReturn(testShibUser())
.anyTimes();

EasyMock.expect(controler.findUser("test1"))
.andReturn(testUser())
.anyTimes();

EasyMock.expect(controler.getAll())
.andReturn(createNotificationSimpleUsers())
.anyTimes();

EasyMock.expect(shibController.getAll())
.andReturn(createNotificationShibUsers())
.anyTimes();


NotificationServiceImpl service = EasyMock.createMockBuilder(NotificationServiceImpl.class)
.withConstructor(controler, shibController, mailService)
.addMockedMethod("buildClient").createMock();



mailService.sendNotificationEmail(
EasyMock.isA(Pair.class),
EasyMock.isA(List.class)
Expand Down

0 comments on commit 266f8b2

Please sign in to comment.