2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.notification.workers;
23 import com.datastax.driver.core.utils.UUIDs;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.openecomp.core.utilities.json.JsonUtil;
27 import org.openecomp.sdc.notification.types.NotificationEntityDto;
28 import org.openecomp.sdc.notification.types.NotificationsStatusDto;
31 import java.text.DateFormat;
33 import java.util.function.Consumer;
34 import java.util.function.Function;
35 import java.util.stream.Collectors;
37 //import org.junit.Before;
40 public class NotificationWorkerTest {
42 private static final String OWNER = "owner_1";
44 private String fileName = null;
45 private NewNotificationsReader news = this.new SimNewNotificationsReader();
46 private NotificationWorker worker = new NotificationWorker(news);
49 public static void beforeClass() throws Exception {
53 public void testBasicResourceCreation() throws IOException, InterruptedException {
55 Consumer<NotificationsStatusDto> notesProcessor = this::notifyReceiver;
57 fileName = "notification_1.csv";
59 worker.register(OWNER, null, notesProcessor);
60 worker.register("owner_2", null, notesProcessor);
61 worker.register("owner_3", null, notesProcessor);
63 int pollInterval = 2000;
64 Thread.sleep(pollInterval);
66 worker.unregister("owner_2");
68 fileName = "notification_2.csv";
70 Thread.sleep(pollInterval);
76 private void notifyReceiver(NotificationsStatusDto notes) {
77 if (Objects.nonNull(notes)) {
78 System.out.println("Received notes:");
79 System.out.println(notes);
84 private class SimNewNotificationsReader implements NewNotificationsReader {
87 private String resourcesDir = "src/test/resources/";
89 public NotificationsStatusDto getNewNotifications(String ownerId, UUID eventId, int limit) {
90 if (fileName == null) {
96 return getNotifications(fn);
99 private NotificationsStatusDto getNotifications(String fn) {
100 NotificationsStatusDto notificationsStatusDto = new NotificationsStatusDto();
101 List<NotificationEntityDto> inputList = new ArrayList<>();
102 try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(resourcesDir + fn))))) {
104 inputList = br.lines().skip(1).limit(limit).map(mapToEntity).collect(Collectors.toCollection(ArrayList::new));
105 } catch (IOException e) {
106 System.err.println("getNotifications(): file " + resourcesDir + fn + " open exception: " + e.getMessage());
108 notificationsStatusDto.setNotifications(inputList);
109 notificationsStatusDto.setLastScanned(inputList.get(0).getEventId());
110 return notificationsStatusDto;
113 private Function<String, NotificationEntityDto> mapToEntity = (line) -> {
114 String[] p = line.split("\\|");
115 NotificationEntityDto entity = new NotificationEntityDto();
116 DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
117 entity.setRead(Boolean.parseBoolean(p[1]));
118 entity.setEventId(UUID.fromString(p[2]));
119 entity.setEventType(p[4]);
120 entity.setDateTime(formatter.format(UUIDs.unixTimestamp(entity.getEventId())));
121 entity.setEventAttributes(JsonUtil.json2Object(p[5], Map.class));