Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-notification-lib / openecomp-sdc-notification-worker / src / test / java / org / openecomp / sdc / notification / workers / NotificationWorkerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.notification.workers;
22
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;
29
30 import java.io.BufferedReader;
31 import java.io.File;
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;
38 import java.util.Map;
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;
44
45 //import org.junit.Before;
46
47
48 public class NotificationWorkerTest {
49
50     private static final String OWNER = "owner_1";
51
52     private String fileName = null;
53     private NewNotificationsReader news = this.new SimNewNotificationsReader();
54     private NotificationWorker worker = new NotificationWorker(news);
55
56     @BeforeClass
57     public static void beforeClass() throws Exception {
58     }
59
60     @Test
61     public void testBasicResourceCreation() throws IOException, InterruptedException {
62
63         Consumer<NotificationsStatusDto> notesProcessor = this::notifyReceiver;
64
65         fileName = "notification_1.csv";
66
67         worker.register(OWNER, null, notesProcessor);
68         worker.register("owner_2", null, notesProcessor);
69         worker.register("owner_3", null, notesProcessor);
70
71         int pollInterval = 2000;
72         Thread.sleep(pollInterval);
73
74         worker.unregister("owner_2");
75
76         fileName = "notification_2.csv";
77
78         Thread.sleep(pollInterval);
79
80         worker.stopPolling();
81
82     }
83
84     private void notifyReceiver(NotificationsStatusDto notes) {
85         if (Objects.nonNull(notes)) {
86             System.out.println("Received notes:");
87             System.out.println(notes);
88         }
89     }
90
91
92     private class SimNewNotificationsReader implements NewNotificationsReader {
93
94
95         private String resourcesDir = "src/test/resources/";
96
97         public NotificationsStatusDto getNewNotifications(String ownerId, UUID eventId, int limit) {
98             if (fileName == null) {
99                 return null;
100             }
101             String fn = fileName;
102             fileName = null;
103
104             return getNotifications(fn);
105         }
106
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))))) {
111                 int limit = 10;
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());
115             }
116             notificationsStatusDto.setNotifications(inputList);
117             notificationsStatusDto.setLastScanned(inputList.get(0).getEventId());
118             return notificationsStatusDto;
119         }
120
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));
130             return entity;
131         };
132     }
133
134 }