Change the header to SO
[so.git] / common / src / test / java / org / openecomp / mso / adapter_utils / tests / MsoAlarmLoggerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.mso.adapter_utils.tests;
22
23
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.BufferedReader;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.io.InputStreamReader;
32 import java.io.PrintWriter;
33
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36
37 import org.openecomp.mso.logger.MsoAlarmLogger;
38 import org.openecomp.mso.properties.MsoJavaProperties;
39 import org.openecomp.mso.properties.MsoPropertiesException;
40 import org.openecomp.mso.properties.MsoPropertiesFactory;
41
42 /**
43  * This junit test very roughly the alarm logger
44  *
45  */
46 public class MsoAlarmLoggerTest {
47
48         public static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
49         public static MsoAlarmLogger msoAlarmLogger;
50
51         @BeforeClass
52         public static final void createObjects() throws MsoPropertiesException
53         {
54         
55                 File outputFile = new File ("target/alarm-test.log");
56                 if (outputFile.exists()) {
57                         outputFile.delete();
58                 }
59                 msoAlarmLogger = new MsoAlarmLogger("target/alarm-test.log");
60         }
61
62         @Test
63         public void testAlarmConfig() throws MsoPropertiesException, IOException {
64
65                 msoAlarmLogger.sendAlarm("test", 0, "detail message");
66
67                 FileInputStream inputStream = new FileInputStream("target/alarm-test.log");
68                 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
69
70                 String line = reader.readLine();
71                 String[] splitLine = line.split("\\|");
72                 assertTrue(splitLine.length==4);
73                 assertTrue("test".equals(splitLine[1]));
74                 assertTrue("0".equals(splitLine[2]));
75                 assertTrue("detail message".equals(splitLine[3]));
76
77                 line = reader.readLine();
78                 assertNull(line);
79                 reader.close();
80                 inputStream.close();
81
82                 // Reset the file for others tests
83                 PrintWriter writer = new PrintWriter(new File("target/alarm-test.log"));
84                 writer.print("");
85                 writer.close();
86
87         }
88
89         @Test
90         public void testAlarm() throws IOException {
91
92                 msoAlarmLogger.sendAlarm("test", 0, "detail message");
93                 msoAlarmLogger.sendAlarm("test2", 1, "detail message2");
94                 msoAlarmLogger.sendAlarm("test3", 2, "detail message3");
95
96                 FileInputStream inputStream = new FileInputStream("target/alarm-test.log");
97                 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
98
99                 String line = reader.readLine();
100                 String[] splitLine = line.split("\\|");
101                 assertTrue(splitLine.length==4);
102                 assertTrue("test".equals(splitLine[1]));
103                 assertTrue("0".equals(splitLine[2]));
104                 assertTrue("detail message".equals(splitLine[3]));
105
106                 line = reader.readLine();
107                 splitLine = line.split("\\|");
108                 assertTrue(splitLine.length==4);
109                 assertTrue("test2".equals(splitLine[1]));
110                 assertTrue("1".equals(splitLine[2]));
111                 assertTrue("detail message2".equals(splitLine[3]));
112
113                 line = reader.readLine();
114                 splitLine = line.split("\\|");
115                 assertTrue(splitLine.length==4);
116                 assertTrue("test3".equals(splitLine[1]));
117                 assertTrue("2".equals(splitLine[2]));
118                 assertTrue("detail message3".equals(splitLine[3]));
119
120                 line = reader.readLine();
121                 assertNull(line);
122                 reader.close();
123                 inputStream.close();
124
125                 // Reset the file for others tests
126                 PrintWriter writer = new PrintWriter(new File("target/alarm-test.log"));
127                 writer.print("");
128                 writer.close();
129
130         }
131 }