com.cedarsoftware.ncube.formatters.NCubeTestReader.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of n-cube Show documentation
Show all versions of n-cube Show documentation
Multi-dimensional Rule Engine
package com.cedarsoftware.ncube.formatters
import com.cedarsoftware.ncube.CellInfo
import com.cedarsoftware.ncube.NCubeTest
import com.cedarsoftware.util.ArrayUtilities
import com.cedarsoftware.util.CompactCILinkedMap
import com.cedarsoftware.util.io.JsonObject
import com.cedarsoftware.util.io.JsonReader
import groovy.transform.CompileStatic
import static com.cedarsoftware.util.StringUtilities.isEmpty
/**
* @author Ken Partlow ([email protected])
*
* Copyright (c) Cedar Software LLC
*
* 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.
*/
@CompileStatic
class NCubeTestReader
{
static List convert(String s) throws IOException
{
List list = []
if (isEmpty(s))
{
return list
}
Map args = [(JsonReader.USE_MAPS):true] as Map
Object[] items = (Object[]) JsonReader.jsonToJava(s, args)
for (Object o : items)
{
JsonObject item = (JsonObject) o
String name = (String) item['name']
Map coord = createCoord((Object[]) item['coord'])
List assertions = createAssertions((Object[]) item['assertions'])
CellInfo[] cellInfos = assertions.toArray(new CellInfo[0]) as CellInfo[]
NCubeTest test = new NCubeTest(name, coord, cellInfos)
list.add(test)
}
return list
}
static Map createCoord(Object[] inputs)
{
Map coord = new CompactCILinkedMap<>()
if (ArrayUtilities.isEmpty(inputs))
{
return coord
}
for (Object item : inputs)
{
JsonObject input = (JsonObject) item
for (entry in input.entrySet())
{
coord.put(entry.key, createCellInfo((JsonObject) entry.value))
}
}
return coord
}
static CellInfo createCellInfo(JsonObject o)
{
String type = (String) o['type']
String value = (String) o['value']
return new CellInfo(type, value, o['isUrl'], false)
}
static List createAssertions(Object[] cellInfoMaps)
{
List list = []
if (ArrayUtilities.isEmpty(cellInfoMaps))
{
return list
}
for (cellInfoMap in cellInfoMaps)
{
list.add(createCellInfo((JsonObject) cellInfoMap))
}
return list
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy