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;
30 import java.io.BufferedReader;
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.io.InputStreamReader;
35 import java.text.DateFormat;
36 import java.util.ArrayList;
37 import java.util.List;
39 import java.util.Objects;
40 import java.util.UUID;
41 import java.util.function.Consumer;
42 import java.util.function.Function;
43 import java.util.stream.Collectors;
45 //import org.junit.Before;
48 public class NotificationWorkerTest {
50 private static final String OWNER = "owner_1";
52 private String fileName = null;
53 private NewNotificationsReader news = this.new SimNewNotificationsReader();
54 private NotificationWorker worker = new NotificationWorker(news);
57 public static void beforeClass() throws Exception {
61 public void testBasicResourceCreation() throws IOException, InterruptedException {
63 Consumer<NotificationsStatusDto> notesProcessor = this::notifyReceiver;
65 fileName = "notification_1.csv";
67 worker.register(OWNER, null, notesProcessor);
68 worker.register("owner_2", null, notesProcessor);
69 worker.register("owner_3", null, notesProcessor);
71 int pollInterval = 2000;
72 Thread.sleep(pollInterval);
74 worker.unregister("owner_2");
76 fileName = "notification_2.csv";
78 Thread.sleep(pollInterval);
84 private void notifyReceiver(NotificationsStatusDto notes) {
85 if (Objects.nonNull(notes)) {
86 System.out.println("Received notes:");
87 System.out.println(notes);
92 private class SimNewNotificationsReader implements NewNotificationsReader {
95 private String resourcesDir = "src/test/resources/";
97 public NotificationsStatusDto getNewNotifications(String ownerId, UUID eventId, int limit) {
98 if (fileName == null) {
101 String fn = fileName;
104 return getNotifications(fn);
107 private NotificationsStatusDto getNotifications(String fn) {
108 NotificationsStatusDto notificationsStatusDto = new NotificationsStatusDto();
109 List<NotificationEntityDto> inputList = new ArrayList<>();
110 try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(resourcesDir + fn))))) {
112 inputList = br.lines().skip(1).limit(limit).map(mapToEntity).collect(Collectors.toCollection(ArrayList::new));
113 } catch (IOException e) {
114 System.err.println("getNotifications(): file " + resourcesDir + fn + " open exception: " + e.getMessage());
116 notificationsStatusDto.setNotifications(inputList);
117 notificationsStatusDto.setLastScanned(inputList.get(0).getEventId());
118 return notificationsStatusDto;
121 private Function<String, NotificationEntityDto> mapToEntity = (line) -> {
122 String[] p = line.split("\\|");
123 NotificationEntityDto entity = new NotificationEntityDto();
124 DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
125 entity.setRead(Boolean.parseBoolean(p[1]));
126 entity.setEventId(UUID.fromString(p[2]));
127 entity.setEventType(p[4]);
128 entity.setDateTime(formatter.format(UUIDs.unixTimestamp(entity.getEventId())));
129 entity.setEventAttributes(JsonUtil.json2Object(p[5], Map.class));