Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test case to verify JSON marshalling after PR#3046 fix #3049

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2024 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.kie.server.api.marshalling.json;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.Test;
import org.kie.server.api.marshalling.test.types.FreeFormItemType;
import org.kie.server.api.marshalling.test.types.ItemsType;
import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.junit.Assert.assertEquals;

public class JSONMarshallerTypesTest {

private Logger logger = LoggerFactory.getLogger(JSONMarshallerTypesTest.class);

private static ItemsType createTestObject() {
FreeFormItemType freeFormItemType = new FreeFormItemType();
freeFormItemType.setItemValue("test");
List<Serializable> standardItemsAndFreeformItems = new ArrayList<Serializable>();
standardItemsAndFreeformItems.add(freeFormItemType);
ItemsType itemsType = new ItemsType();
itemsType.setStandardItemsAndFreeformItems(standardItemsAndFreeformItems);
return itemsType;
}

@Test
public void testMarshallingTypes() throws IOException {
Reflections reflections = new Reflections("org.kie.server.api.marshalling.test.types", new SubTypesScanner(false));
Set<Class<?>> clazzes = reflections.getSubTypesOf(Object.class).stream().collect(Collectors.toSet());

JSONMarshaller marshaller = new JSONMarshaller(clazzes, JSONMarshallerTypesTest.class.getClassLoader());
String rawContent = "{\n"
+ " \"org.kie.server.api.marshalling.test.types.ItemsType\" : {\n"
+ " \"standardItemsAndFreeformItems\" : [ {\n"
+ " \"FreeFormItemType\" : {\n"
+ " \"itemValue\" : \"test\"\n"
+ " }\n"
+ " } ]\n"
+ " }\n"
+ "}";
String marshalledContent = marshaller.marshall(createTestObject());
assertEquals(rawContent, marshalledContent);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2024 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.server.api.marshalling.test.types;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "FreeFormItemType", propOrder = {
"itemValue"
})
public class FreeFormItemType implements Serializable, Cloneable
{

private final static long serialVersionUID = 1L;

@XmlElement(required = true)
protected String itemValue;

public String getItemValue() {
return itemValue;
}

public void setItemValue(String value) {
this.itemValue = value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2024 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.server.api.marshalling.test.types;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ItemsType", propOrder = {
"standardItemsAndFreeformItems"
})
public class ItemsType
implements Serializable, Cloneable
{

private final static long serialVersionUID = 1L;
@XmlElements({
@XmlElement(name = "standardItem", type = StandardItemType.class),
@XmlElement(name = "freeformItem", type = FreeFormItemType.class)
})
protected List<Serializable> standardItemsAndFreeformItems;

public List<Serializable> getStandardItemsAndFreeformItems() {
if (standardItemsAndFreeformItems == null) {
standardItemsAndFreeformItems = new ArrayList<Serializable>();
}
return this.standardItemsAndFreeformItems;
}

public void setStandardItemsAndFreeformItems(List<Serializable> value) {
this.standardItemsAndFreeformItems = null;
List<Serializable> draftl = this.getStandardItemsAndFreeformItems();
draftl.addAll(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2024 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.server.api.marshalling.test.types;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "StandardItemType", propOrder = {
"value"
})
public class StandardItemType implements Serializable, Cloneable
{

private final static long serialVersionUID = 1L;
@XmlValue
protected String value;

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

}