Merge "Fix test coverage not being reported in Sonar"
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / protobuff / messages / persistent / PersistentMessagesTest.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.persistent;
12
13 import org.junit.Assert;
14 import org.junit.Test;
15 import org.opendaylight.controller.protobuff.messages.AbstractMessagesTest;
16 import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
17 import org.opendaylight.yangtools.yang.common.QName;
18
19
20 /**
21  * This test case is present to ensure that if others have used proper version of protocol buffer
22  * for the peristent.proto messages
23  *
24  * If a different version of protocol buffer and there is change in serializaiton format
25  * this test would break as we are comparing with protocol buffer 2.5 generated
26  * serialized data.
27  *
28  * @author: syedbahm
29  *
30  */
31
32 public class PersistentMessagesTest extends AbstractMessagesTest {
33
34   private final String namespace = "urn:protobuff", revision = "2014-07-31",
35       localName = "test";
36
37   @Override
38   @Test
39   public void verifySerialization() throws Exception {
40     NormalizedNodeMessages.InstanceIdentifier.Builder instanceIdentifierBuilder =
41         NormalizedNodeMessages.InstanceIdentifier.newBuilder();
42     NormalizedNodeMessages.PathArgument.Builder pathArgument =
43         NormalizedNodeMessages.PathArgument.newBuilder();
44     pathArgument.setNodeType(NormalizedNodeMessages.QName.newBuilder()
45         .setValue(QName.create(namespace, revision, localName).toString())
46         .build());
47     pathArgument.setValue("test");
48     instanceIdentifierBuilder.addArguments(pathArgument.build());
49     NormalizedNodeMessages.InstanceIdentifier expectedOne =
50         instanceIdentifierBuilder.build();
51
52     PersistentMessages.Modification.Builder builder =
53         PersistentMessages.Modification.newBuilder();
54     builder.setType("test");
55     builder.setPath(expectedOne);
56
57     writeToFile((com.google.protobuf.GeneratedMessage.Builder<?>) builder);
58
59     PersistentMessages.Modification modificationNew =
60         (PersistentMessages.Modification) readFromFile(PersistentMessages.Modification.PARSER);
61     Assert.assertEquals("test", modificationNew.getType());
62     Assert.assertEquals(expectedOne.getArguments(0).getValue(), modificationNew
63         .getPath().getArguments(0).getValue());
64     Assert.assertEquals(expectedOne.getArguments(0).getType(), modificationNew
65         .getPath().getArguments(0).getType());
66
67     // we will compare with the serialized data that we had shipped
68     PersistentMessages.Modification modificationOriginal =
69         (PersistentMessages.Modification) readFromTestDataFile(PersistentMessages.Modification.PARSER);
70     Assert.assertEquals(modificationOriginal.getPath().getArguments(0)
71         .getValue(), modificationNew.getPath().getArguments(0).getValue());
72     Assert.assertEquals(modificationOriginal.getPath().getArguments(0)
73         .getType(), modificationNew.getPath().getArguments(0).getType());
74
75
76   }
77
78   @Override
79   public String getTestFileName() {
80     return PersistentMessagesTest.class.getSimpleName();
81   }
82 }