Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.linkedin.restli.tools.compatibility.CompatibilityInfoMap Maven / Gradle / Ivy
/*
Copyright (c) 2012 LinkedIn Corp.
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 com.linkedin.restli.tools.compatibility;
import com.linkedin.data.message.Message;
import com.linkedin.data.schema.compatibility.CompatibilityMessage;
import com.linkedin.restli.tools.idlcheck.CompatibilityInfo;
import com.linkedin.restli.tools.idlcheck.CompatibilityLevel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
/**
* @author Moira Tagle
* @version $Revision: $
*/
public class CompatibilityInfoMap
{
private Map> _restSpecMap = new HashMap>();
private Map> _modelMap = new HashMap>();
public CompatibilityInfoMap()
{
_restSpecMap.put(CompatibilityInfo.Level.INCOMPATIBLE, new ArrayList());
_restSpecMap.put(CompatibilityInfo.Level.COMPATIBLE, new ArrayList());
_modelMap.put(CompatibilityInfo.Level.INCOMPATIBLE, new ArrayList());
_modelMap.put(CompatibilityInfo.Level.COMPATIBLE, new ArrayList());
}
public void addRestSpecInfo(CompatibilityInfo.Type infoType, Stack path,
Object... parameters)
{
_restSpecMap.get(infoType.getLevel()).add(new CompatibilityInfo(path, infoType, parameters));
}
public void addRestSpecInfo(Object pathTail, CompatibilityInfo.Type infoType, Stack path,
Object... parameters)
{
path.push(pathTail);
_restSpecMap.get(infoType.getLevel()).add(new CompatibilityInfo(path, infoType, parameters));
path.pop();
}
public void addRestSpecInfo(Object pathTail, CompatibilityMessage message, Stack path)
{
path.push(pathTail);
final CompatibilityInfo.Type infoType;
if (message.isError())
{
infoType = CompatibilityInfo.Type.TYPE_ERROR;
final String info = String.format(message.getFormat(), message.getArgs());
_restSpecMap.get(infoType.getLevel()).add(new CompatibilityInfo(path, infoType, info));
}
else
{
infoType = CompatibilityInfo.Type.TYPE_INFO;
final String info = String.format(message.getFormat(), message.getArgs());
_restSpecMap.get(infoType.getLevel()).add(new CompatibilityInfo(Arrays.asList(message.getPath()), infoType, info));
}
path.pop();
}
public void addRestSpecInfo(Message message)
{
final CompatibilityInfo.Type infoType = CompatibilityInfo.Type.OTHER_ERROR;
_restSpecMap.get(infoType.getLevel()).add(new CompatibilityInfo(Arrays.asList(message.getPath()),
infoType,
message.toString()));
}
/**
* Add info used for adding errors related to {@link com.linkedin.data.schema.NamedDataSchema} compatibility.
* The path will be the path to the relevant field within the NamedDataSchema.
* @param message {@link CompatibilityMessage}
*/
public void addModelInfo(CompatibilityMessage message)
{
final CompatibilityInfo.Type infoType;
CompatibilityInfo info;
String infoMessage = String.format(message.getFormat(), message.getArgs());
if (message.isError())
{
switch (message.getImpact())
{
case BREAKS_NEW_READER:
infoType = CompatibilityInfo.Type.TYPE_BREAKS_NEW_READER;
break;
case BREAKS_OLD_READER:
infoType = CompatibilityInfo.Type.TYPE_BREAKS_OLD_READER;
break;
case BREAKS_NEW_AND_OLD_READERS:
infoType = CompatibilityInfo.Type.TYPE_BREAKS_NEW_AND_OLD_READERS;
break;
default:
infoType = CompatibilityInfo.Type.OTHER_ERROR;
break;
}
}
else
{
infoType = CompatibilityInfo.Type.TYPE_INFO;
}
info = new CompatibilityInfo(Arrays.asList(message.getPath()), infoType, infoMessage);
_modelMap.get(infoType.getLevel()).add(info);
}
/**
* @return summary message about the check result, including all categories
* empty string if called before checking any files
*/
public String createSummary(String prevRestModelPath, String currRestModelPath)
{
final StringBuilder summaryMessage = new StringBuilder();
createSummaryForInfo(getIncompatibles(), "Incompatible changes", summaryMessage);
createSummaryForInfo(getCompatibles(), "Compatible changes", summaryMessage);
if (summaryMessage.length() != 0)
{
summaryMessage.insert(0, new StringBuilder("\nRest.li compatibility report between published \"")
.append(prevRestModelPath)
.append("\" and current \"")
.append(currRestModelPath)
.append("\":\n"));
}
return summaryMessage.toString();
}
public String createSummary()
{
final StringBuilder summaryMessage = new StringBuilder();
createSummaryForInfo(getIncompatibles(), "Incompatible changes", summaryMessage);
createSummaryForInfo(getCompatibles(), "Compatible changes", summaryMessage);
if (summaryMessage.length() != 0)
{
summaryMessage.insert(0, "\nidl compatibility report:\n");
}
return summaryMessage.toString();
}
private static void createSummaryForInfo(Collection info,
String description,
StringBuilder summaryMessage)
{
if (info.isEmpty())
{
return;
}
summaryMessage.append(description).append(":\n");
int issueIndex = 1;
for (CompatibilityInfo i: info)
{
summaryMessage.append(" ").append(issueIndex).append(") ").append(i.toString()).append("\n");
++issueIndex;
}
}
public boolean isCompatible(CompatibilityLevel level)
{
final Collection incompatibles = getIncompatibles();
final Collection compatibles = getCompatibles();
return isCompatible(incompatibles, compatibles, level);
}
public boolean isRestSpecCompatible(CompatibilityLevel level)
{
final Collection incompatibles = getRestSpecIncompatibles();
final Collection compatibles = getRestSpecCompatibles();
return isCompatible(incompatibles, compatibles, level);
}
public boolean isModelCompatible(CompatibilityLevel level)
{
final Collection incompatibles = getModelIncompatibles();
final Collection compatibles = getModelCompatibles();
return isCompatible(incompatibles, compatibles, level);
}
private boolean isCompatible(Collection incompatibles, Collection compatibles, CompatibilityLevel level)
{
return ((incompatibles.isEmpty() || level.ordinal() < CompatibilityLevel.BACKWARDS.ordinal()) &&
(compatibles.isEmpty() || level.ordinal() < CompatibilityLevel.EQUIVALENT.ordinal()));
}
public boolean isEquivalent()
{
return isCompatible(CompatibilityLevel.EQUIVALENT);
}
public boolean isRestSpecEquivalent()
{
return isRestSpecCompatible(CompatibilityLevel.EQUIVALENT);
}
public boolean isModelEquivalent()
{
return isModelCompatible(CompatibilityLevel.EQUIVALENT);
}
/**
* @return check results in the backwards incompatibility category.
* empty collection if called before checking any files
*/
public Collection getIncompatibles()
{
return get(CompatibilityInfo.Level.INCOMPATIBLE);
}
/**
* @return check results in the backwards compatibility category.
* empty collection if called before checking any files
*/
public Collection getCompatibles()
{
return get(CompatibilityInfo.Level.COMPATIBLE);
}
public Collection getRestSpecIncompatibles()
{
return getRestSpecInfo(CompatibilityInfo.Level.INCOMPATIBLE);
}
public Collection getRestSpecCompatibles()
{
return getRestSpecInfo(CompatibilityInfo.Level.COMPATIBLE);
}
public Collection getModelIncompatibles()
{
return getModelInfo(CompatibilityInfo.Level.INCOMPATIBLE);
}
public Collection getModelCompatibles()
{
return getModelInfo(CompatibilityInfo.Level.COMPATIBLE);
}
public Collection get(CompatibilityInfo.Level level)
{
Collection infos = new ArrayList(getRestSpecInfo(level));
infos.addAll(getModelInfo(level));
return infos;
}
public Collection getRestSpecInfo(CompatibilityInfo.Level level)
{
return _restSpecMap.get(level);
}
public Collection getModelInfo(CompatibilityInfo.Level level)
{
return _modelMap.get(level);
}
public boolean addAll(CompatibilityInfoMap other)
{
for(Map.Entry> entry : _restSpecMap.entrySet())
{
entry.getValue().addAll(other.getRestSpecInfo(entry.getKey()));
}
for(Map.Entry> entry : _modelMap.entrySet())
{
entry.getValue().addAll(other.getModelInfo(entry.getKey()));
}
return true;
}
}