blob: 154dcdd6254b2794c7ae5eec72536c8c7effaf37 [file] [log] [blame]
b.janani68c55e12016-02-24 12:23:03 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.utils.io.impl;
18
19import org.junit.Test;
20import org.junit.Rule;
21import org.junit.rules.ExpectedException;
22import org.onosproject.yangutils.utils.io.impl.SerializedDataStore.SerializedDataStoreType;
23
24import java.io.FileNotFoundException;
25import java.io.IOException;
26import java.lang.reflect.Constructor;
27import java.lang.reflect.InvocationTargetException;
28import java.util.LinkedList;
29import java.util.List;
30
31import org.slf4j.Logger;
32import static org.slf4j.LoggerFactory.getLogger;
33
34import static org.hamcrest.core.Is.is;
35import static org.junit.Assert.assertThat;
36import static org.junit.Assert.assertNotNull;
37
38/**
39 * Unit tests for the serialized data store for its contents.
40 */
41public final class SerializedDataStoreTest {
42
43 private final Logger log = getLogger(getClass());
44
45 @Rule
46 public ExpectedException thrown = ExpectedException.none();
47
48 /**
49 * A private constructor is tested.
50 *
51 * @throws SecurityException if any security violation is observed.
52 * @throws NoSuchMethodException if when the method is not found.
53 * @throws IllegalArgumentException if there is illegal argument found.
54 * @throws InstantiationException if instantiation is provoked for the private constructor.
55 * @throws IllegalAccessException if instance is provoked or a method is provoked.
56 * @throws InvocationTargetException when an exception occurs by the method or constructor.
57 */
58 @Test
59 public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
60 InstantiationException, IllegalAccessException, InvocationTargetException {
61
62 Class<?>[] classesToConstruct = {SerializedDataStore.class };
63 for (Class<?> clazz : classesToConstruct) {
64 Constructor<?> constructor = clazz.getDeclaredConstructor();
65 constructor.setAccessible(true);
66 assertNotNull(constructor.newInstance());
67 }
68 }
69
70 /**
71 * This test case checks the attribute info that is read and put into the list.
72 */
73 @Test
74 public void insertAttributeDataTest() throws IOException, ClassNotFoundException, FileNotFoundException {
75
76 String attributeData = "attribute content lists this";
77 SerializedDataStore.setSerializeData(attributeData, SerializedDataStoreType.ATTRIBUTE);
78 List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.ATTRIBUTE);
79 List<String> expectedinfo = new LinkedList<>();
80 expectedinfo.add(attributeData);
81 assertThat(true, is(attributeInfo.equals(expectedinfo)));
82 SerializedDataStoreType.valueOf(SerializedDataStoreType.ATTRIBUTE.toString());
83 }
84
85 /**
86 * This test case checks the builder interface that is read and put into the list.
87 */
88 @Test
89 public void insertBuilderInterfaceMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
90
91 String builderInterfaceMethodsData = "builder interface methods content lists this";
92 SerializedDataStore.setSerializeData(builderInterfaceMethodsData,
93 SerializedDataStoreType.BUILDER_INTERFACE_METHODS);
94 List<String> attributeInfo = SerializedDataStore
95 .getSerializeData(SerializedDataStoreType.BUILDER_INTERFACE_METHODS);
96 List<String> expectedinfo = new LinkedList<>();
97 expectedinfo.add(builderInterfaceMethodsData);
98 assertThat(true, is(attributeInfo.equals(expectedinfo)));
99 }
100
101 /**
102 * This test case checks the builder methods that is read and put into the list.
103 */
104 @Test
105 public void insertBuilderMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
106
107 String builderMethodsData = "builder methods content lists this";
108 SerializedDataStore.setSerializeData(builderMethodsData, SerializedDataStoreType.BUILDER_METHODS);
109 List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.BUILDER_METHODS);
110 List<String> expectedinfo = new LinkedList<>();
111 expectedinfo.add(builderMethodsData);
112 assertThat(true, is(attributeInfo.equals(expectedinfo)));
113 }
114
115 /**
116 * This test case checks the impl methods that is read and put into the list.
117 */
118 @Test
119 public void insertImplMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
120
121 String implMethodsData = "impl methods content lists this";
122 SerializedDataStore.setSerializeData(implMethodsData, SerializedDataStoreType.IMPL_METHODS);
123 List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.IMPL_METHODS);
124 List<String> expectedinfo = new LinkedList<>();
125 expectedinfo.add(implMethodsData);
126 assertThat(true, is(attributeInfo.equals(expectedinfo)));
127 }
128
129 /**
130 * This test case checks the import methods that is read and put into the list.
131 */
132 @Test
133 public void insertImportTest() throws IOException, ClassNotFoundException, FileNotFoundException {
134
135 String importData = "interface methods content lists this";
136 SerializedDataStore.setSerializeData(importData, SerializedDataStoreType.IMPORT);
137 List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.IMPORT);
138 List<String> expectedinfo = new LinkedList<>();
139 expectedinfo.add(importData);
140 assertThat(true, is(attributeInfo.equals(expectedinfo)));
141 }
142
143 /**
144 * This test case checks the interface methods that is read and put into the list.
145 */
146 @Test
147 public void insertInterfaceMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
148
149 String interfaceMethodsData = "interface methods content lists this";
150 SerializedDataStore.setSerializeData(interfaceMethodsData, SerializedDataStoreType.INTERFACE_METHODS);
151 List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.INTERFACE_METHODS);
152 List<String> expectedinfo = new LinkedList<>();
153 expectedinfo.add(interfaceMethodsData);
154 assertThat(true, is(attributeInfo.equals(expectedinfo)));
155 }
156}