Use NormalizedNode streaming serialization in sal-remoterpc-connector
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / protobuff / messages / AbstractMessagesTest.java
1 /*
2  *
3  *  Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  *  This program and the accompanying materials are made available under the
6  *  terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  *  and is available at http://www.eclipse.org/legal/epl-v10.html
8  *
9  */
10
11 package org.opendaylight.controller.protobuff.messages;
12
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.io.FileOutputStream;
16
17 /**
18  * @author: syedbahm Date: 7/31/14
19  */
20 public abstract class AbstractMessagesTest {
21     public final String VERSION_COMPATIBILTY_TEST_DATA_PATH = "." + File.separator + "src" + File.separator + "test"
22             + File.separator + "resources" + File.separator + "version-compatibility-serialized-data";
23     private File file;
24     private File testDataFile;
25
26     protected AbstractMessagesTest() {
27         init();
28     }
29
30     protected void init() {
31         file = new File(getTestFileName());
32         testDataFile = new File(VERSION_COMPATIBILTY_TEST_DATA_PATH + File.separator + getTestFileName() + "Data");
33     }
34
35     abstract public void verifySerialization() throws Exception;
36
37     protected void writeToFile(com.google.protobuf.GeneratedMessage.Builder<?> builder) throws Exception {
38
39         FileOutputStream output = new FileOutputStream(file);
40         builder.build().writeTo(output);
41         output.close();
42
43     }
44
45     protected com.google.protobuf.GeneratedMessage readFromFile(com.google.protobuf.Parser<?> parser) throws Exception {
46         com.google.protobuf.GeneratedMessage message = (com.google.protobuf.GeneratedMessage) parser
47                 .parseFrom(new FileInputStream(file));
48
49         /*
50          * Note: we will delete only the test file -- comment below if you want
51          * to capture the version-compatibility-serialized-data test data
52          * file.The file will be generated at root of the
53          * sal-protocolbuffer-encoding and you need to move it to
54          * test/resources/version-compatbility-serialized-data folder renaming
55          * the file to include suffix <TestFileName>"Data"
56          */
57         file.delete();
58         return message;
59     }
60
61     protected com.google.protobuf.GeneratedMessage readFromTestDataFile(com.google.protobuf.Parser<?> parser)
62             throws Exception {
63         return (com.google.protobuf.GeneratedMessage) parser.parseFrom(new FileInputStream(testDataFile));
64     }
65
66     public abstract String getTestFileName();
67 }