re base code
[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.*;
31 import java.text.DateFormat;
32 import java.util.*;
33 import java.util.function.Consumer;
34 import java.util.function.Function;
35 import java.util.stream.Collectors;
36
37 //import org.junit.Before;
38
39
40 public class NotificationWorkerTest {
41
42     private static final String OWNER = "owner_1";
43
44     private String fileName = null;
45     private NewNotificationsReader news = this.new SimNewNotificationsReader();
46     private NotificationWorker worker = new NotificationWorker(news);
47
48     @BeforeClass
49     public static void beforeClass() throws Exception {
50     }
51
52     @Test
53     public void testBasicResourceCreation() throws IOException, InterruptedException {
54
55         Consumer<NotificationsStatusDto> notesProcessor = this::notifyReceiver;
56
57         fileName = "notification_1.csv";
58
59         worker.register(OWNER, null, notesProcessor);
60         worker.register("owner_2", null, notesProcessor);
61         worker.register("owner_3", null, notesProcessor);
62
63         int pollInterval = 2000;
64         Thread.sleep(pollInterval);
65
66         worker.unregister("owner_2");
67
68         fileName = "notification_2.csv";
69
70         Thread.sleep(pollInterval);
71
72         worker.stopPolling();
73
74     }
75
76     private void notifyReceiver(NotificationsStatusDto notes) {
77         if (Objects.nonNull(notes)) {
78             System.out.println("Received notes:");
79             System.out.println(notes);
80         }
81     }
82
83
84     private class SimNewNotificationsReader implements NewNotificationsReader {
85
86
87         private String resourcesDir = "src/test/resources/";
88
89         public NotificationsStatusDto getNewNotifications(String ownerId, UUID eventId, int limit) {
90             if (fileName == null) {
91                 return null;
92             }
93             String fn = fileName;
94             fileName = null;
95
96             return getNotifications(fn);
97         }
98
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))))) {
103                 int limit = 10;
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());
107             }
108             notificationsStatusDto.setNotifications(inputList);
109             notificationsStatusDto.setLastScanned(inputList.get(0).getEventId());
110             return notificationsStatusDto;
111         }
112
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));
122             return entity;
123         };
124     }
125
126 }