Skip to content

Commit e163da9

Browse files
committed
Improves the tests for encoders
1 parent 22143db commit e163da9

8 files changed

Lines changed: 1607 additions & 1672 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/* ----------------------------------------------------------------------------
2+
* Copyright (C) 2024 European Space Agency
3+
* European Space Operations Centre
4+
* Darmstadt
5+
* Germany
6+
* ----------------------------------------------------------------------------
7+
* System : CCSDS MO Transport Framework
8+
* ----------------------------------------------------------------------------
9+
* Licensed under the European Space Agency Public License, Version 2.0
10+
* You may not use this file except in compliance with the License.
11+
*
12+
* Except as expressly set forth in this License, the Software is provided to
13+
* You on an "as is" basis and without warranties of any kind, including without
14+
* limitation merchantability, fitness for a particular purpose, absence of
15+
* defects or errors, accuracy or non-infringement of intellectual property rights.
16+
*
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
* ----------------------------------------------------------------------------
20+
*/
21+
22+
import org.ccsds.moims.mo.mal.MALException;
23+
import org.ccsds.moims.mo.mal.OperationField;
24+
import org.ccsds.moims.mo.mal.structures.Element;
25+
import org.ccsds.moims.mo.mal.structures.Identifier;
26+
import org.ccsds.moims.mo.mal.structures.NullableAttribute;
27+
import org.ccsds.moims.mo.mal.structures.NullableAttributeList;
28+
import org.ccsds.moims.mo.mal.structures.StringList;
29+
import static org.junit.Assert.assertEquals;
30+
import static org.junit.Assert.assertNotNull;
31+
import org.junit.Test;
32+
33+
/**
34+
* Tests that Encode and then Decode data.
35+
*/
36+
public abstract class EncoderDecoderTest {
37+
38+
public abstract Element encodeThenDecode(Element element, OperationField field) throws MALException;
39+
40+
/**
41+
* Simple test for encoding decoding a list of NullableAttributeList.
42+
*
43+
* @throws java.lang.Exception
44+
*/
45+
@Test
46+
public void testEncodeDecodeNullableAttributeList() throws Exception {
47+
NullableAttributeList list = new NullableAttributeList();
48+
list.add(new NullableAttribute(new Identifier("String A")));
49+
list.add(new NullableAttribute(new Identifier("String B")));
50+
list.add(new NullableAttribute(new Identifier("String C")));
51+
52+
NullableAttributeList readList = (NullableAttributeList) encodeThenDecode(list, null);
53+
54+
// Assertions:
55+
assertNotNull(readList);
56+
assertEquals(3, readList.size());
57+
assertEquals("NullableAttributeList.get(0)", list.get(0), readList.get(0));
58+
assertEquals("NullableAttributeList.get(1)", list.get(1), readList.get(1));
59+
assertEquals("NullableAttributeList.get(2)", list.get(2), readList.get(2));
60+
}
61+
62+
/**
63+
* Simple test for encoding decoding a list of StringList. Note that the
64+
* String MAL type is mapped to the String Java type, therefore the type
65+
* might need to be wrapped in a Union type for correct encoding/decoding.
66+
*
67+
* @throws java.lang.Exception
68+
*/
69+
@Test
70+
public void testEncodeDecodeStringList() throws Exception {
71+
StringList list = new StringList();
72+
list.add("String A");
73+
list.add("String B");
74+
list.add("String C");
75+
76+
StringList readList = (StringList) encodeThenDecode(list, null);
77+
78+
// Assertions:
79+
assertNotNull(readList);
80+
assertEquals(3, readList.size());
81+
assertEquals("StringList.get(0)", list.get(0), readList.get(0));
82+
assertEquals("StringList.get(1)", list.get(1), readList.get(1));
83+
assertEquals("StringList.get(2)", list.get(2), readList.get(2));
84+
}
85+
}

encodings/encoding-binary/src/test/java/FixedEncoderDecoderTest.java

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,20 @@
2222
import esa.mo.mal.encoder.binary.fixed.FixedBinaryStreamFactory;
2323
import java.io.ByteArrayInputStream;
2424
import java.io.ByteArrayOutputStream;
25+
import org.ccsds.moims.mo.mal.MALException;
26+
import org.ccsds.moims.mo.mal.OperationField;
2527
import org.ccsds.moims.mo.mal.encoding.MALElementInputStream;
2628
import org.ccsds.moims.mo.mal.encoding.MALElementOutputStream;
27-
import org.ccsds.moims.mo.mal.structures.Identifier;
28-
import org.ccsds.moims.mo.mal.structures.NullableAttribute;
29-
import org.ccsds.moims.mo.mal.structures.NullableAttributeList;
30-
import org.ccsds.moims.mo.mal.structures.StringList;
29+
import org.ccsds.moims.mo.mal.structures.Element;
3130
import org.junit.After;
3231
import org.junit.AfterClass;
33-
import static org.junit.Assert.assertEquals;
34-
import static org.junit.Assert.assertNotNull;
3532
import org.junit.Before;
3633
import org.junit.BeforeClass;
37-
import org.junit.Test;
3834

3935
/**
4036
* Tests for Encoding and Decoding with FixedBinary
4137
*/
42-
public class FixedEncoderDecoderTest {
38+
public class FixedEncoderDecoderTest extends EncoderDecoderTest {
4339

4440
public FixedEncoderDecoderTest() {
4541
}
@@ -60,69 +56,18 @@ public void setUp() {
6056
public void tearDown() {
6157
}
6258

63-
/**
64-
* Simple test for encoding decoding a list of NullableAttributeList.
65-
*
66-
* @throws java.lang.Exception
67-
*/
68-
@Test
69-
public void testEncodeDecodeNullableAttributeList() throws Exception {
70-
NullableAttributeList list = new NullableAttributeList();
71-
list.add(new NullableAttribute(new Identifier("String A")));
72-
list.add(new NullableAttribute(new Identifier("String B")));
73-
list.add(new NullableAttribute(new Identifier("String C")));
74-
59+
@Override
60+
public Element encodeThenDecode(Element element, OperationField field) throws MALException {
61+
// Encode
7562
FixedBinaryStreamFactory factory = new FixedBinaryStreamFactory();
7663
ByteArrayOutputStream baos = new ByteArrayOutputStream();
7764
MALElementOutputStream malWriter = factory.createOutputStream(baos);
65+
malWriter.writeElement(element, field);
7866

79-
malWriter.writeElement(list, null);
80-
67+
// Decode
8168
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
8269
MALElementInputStream malReader = factory.createInputStream(bais);
83-
84-
NullableAttributeList readList = new NullableAttributeList();
85-
readList = (NullableAttributeList) malReader.readElement(readList, null);
86-
87-
// Assertions:
88-
assertNotNull(readList);
89-
assertEquals(3, readList.size());
90-
assertEquals("NullableAttributeList.get(0)", list.get(0), readList.get(0));
91-
assertEquals("NullableAttributeList.get(1)", list.get(1), readList.get(1));
92-
assertEquals("NullableAttributeList.get(2)", list.get(2), readList.get(2));
70+
return malReader.readElement(element.createElement(), field);
9371
}
9472

95-
/**
96-
* Simple test for encoding decoding a list of StringList. Note that the
97-
* String MAL type is mapped to the String Java type, therefore the type
98-
* might need to be wrapped in a Union type for correct encoding/decoding.
99-
*
100-
* @throws java.lang.Exception
101-
*/
102-
@Test
103-
public void testEncodeDecodeStringList() throws Exception {
104-
StringList list = new StringList();
105-
list.add("String A");
106-
list.add("String B");
107-
list.add("String C");
108-
109-
FixedBinaryStreamFactory factory = new FixedBinaryStreamFactory();
110-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
111-
MALElementOutputStream malWriter = factory.createOutputStream(baos);
112-
113-
malWriter.writeElement(list, null);
114-
115-
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
116-
MALElementInputStream malReader = factory.createInputStream(bais);
117-
118-
StringList readList = new StringList();
119-
readList = (StringList) malReader.readElement(readList, null);
120-
121-
// Assertions:
122-
assertNotNull(readList);
123-
assertEquals(3, readList.size());
124-
assertEquals("StringList.get(0)", list.get(0), readList.get(0));
125-
assertEquals("StringList.get(1)", list.get(1), readList.get(1));
126-
assertEquals("StringList.get(2)", list.get(2), readList.get(2));
127-
}
12873
}

encodings/encoding-xml/src/test/java/esa/mo/mal/encoder/http/test/HTTPXMLElementInputTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public static void setUpBeforeClass() throws Exception {
4949

5050
@Test
5151
public void testDecodeRegisterMessage() throws MALException {
52-
5352
String testXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
5453
+ "<malxml:Body xmlns:malxml=\"http://www.ccsds.org/schema/malxml/MAL\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
5554
+ "<Subscription malxml:type=\"" + Subscription.TYPE_ID.getTypeId() + "\">"

0 commit comments

Comments
 (0)