use of org.eclipse.winery.model.tosca.TExtensibleElements in project winery by eclipse.
the class TOSCAGraphIsomorphismTest method testServiceTemplateComplianceRuleChecker.
@Test
public void testServiceTemplateComplianceRuleChecker() throws IOException {
HashMap<DefinitionsChildId, TExtensibleElements> allEntities = new HashMap<>();
TServiceTemplate tServiceTemplate = createTServiceTemplate("ServiceTemplateTestId", TEST_TARGET_NAMESPACE);
ComplianceRuleId crId1 = new ComplianceRuleId(new QName(TEST_TARGET_NAMESPACE, "test1"));
ComplianceRuleId crId2 = new ComplianceRuleId(new QName(TEST_TARGET_NAMESPACE, "test2"));
ComplianceRuleId crId3 = new ComplianceRuleId(new QName(TEST_TARGET_NAMESPACE, "test3"));
// create NodeType
NodeTypeId nodeTypeIdA = createNodeTypeId("IDA");
NodeTypeId nodeTypeIdB = createNodeTypeId("IDB");
NodeTypeId nodeTypeIdC = createNodeTypeId("IDC");
NodeTypeId nodeTypeIdD = createNodeTypeId("IDD");
TNodeType nodeTypeA = createTNodeType(nodeTypeIdA);
allEntities.put(nodeTypeIdA, nodeTypeA);
TNodeType nodeTypeB = createTNodeType(nodeTypeIdB);
setDerivedFrom(nodeTypeIdA, nodeTypeB);
allEntities.put(nodeTypeIdB, nodeTypeB);
TNodeType nodeTypeC = createTNodeType(nodeTypeIdC);
setDerivedFrom(nodeTypeIdA, nodeTypeC);
allEntities.put(nodeTypeIdC, nodeTypeC);
TNodeType nodeTypeD = createTNodeType(nodeTypeIdD);
allEntities.put(nodeTypeIdD, nodeTypeD);
TNodeTemplate ruleOneIdentifier = createTNodeTemplate("R1_I", nodeTypeIdA);
TNodeTemplate ruleOneRequiredStructure = createTNodeTemplate("R1_RS", nodeTypeIdB);
TNodeTemplate ruleTwoIdentifier = createTNodeTemplate("R2_I", nodeTypeIdA);
TNodeTemplate ruleTwoRequiredStructure = createTNodeTemplate("R2_RS", nodeTypeIdC);
TNodeTemplate ruleThreeIdentifier = createTNodeTemplate("R3_I", nodeTypeIdA);
TNodeTemplate ruleThreeRequiredStructure = createTNodeTemplate("R3_RS", nodeTypeIdD);
TNodeTemplate serviceTemplateTopology = createTNodeTemplate("ST", nodeTypeIdB);
// satisfied
TComplianceRule ruleOne = createTComplianceRule(crId1);
ruleOne.setIdentifier(createTTopologyTemplate(Arrays.asList(ruleOneIdentifier), newArrayList()));
ruleOne.setRequiredStructure(createTTopologyTemplate(Arrays.asList(ruleOneRequiredStructure), newArrayList()));
allEntities.put(crId1, ruleOne);
// unsatisfied
TComplianceRule ruleTwo = createTComplianceRule(crId2);
ruleTwo.setIdentifier(createTTopologyTemplate(Arrays.asList(ruleTwoIdentifier), newArrayList()));
ruleTwo.setRequiredStructure(createTTopologyTemplate(Arrays.asList(ruleTwoRequiredStructure), newArrayList()));
allEntities.put(crId2, ruleTwo);
// invalid rule
TComplianceRule ruleThree = createTComplianceRule(crId3);
ruleThree.setIdentifier(createTTopologyTemplate(Arrays.asList(ruleThreeIdentifier), newArrayList()));
ruleThree.setRequiredStructure(createTTopologyTemplate(Arrays.asList(ruleThreeRequiredStructure), newArrayList()));
allEntities.put(crId3, ruleThree);
tServiceTemplate.setTopologyTemplate(createTTopologyTemplate(Arrays.asList(serviceTemplateTopology), newArrayList()));
persist(allEntities);
ServiceTemplateComplianceRuleRuleChecker checker = new ServiceTemplateComplianceRuleRuleChecker(tServiceTemplate);
assertEquals(3, checker.getRuleIds(tServiceTemplate).stream().count());
assertEquals(3, checker.getRuleIds(tServiceTemplate).stream().filter(id -> id.getQName().getLocalPart().matches("test1|test2|test3")).count());
ServiceTemplateCheckingResult serviceTemplateCheckingResult = checker.checkComplianceRules();
assertEquals(1, serviceTemplateCheckingResult.getSatisfied().size());
assertEquals(1, serviceTemplateCheckingResult.getUnsatisfied().size());
assertEquals(1, serviceTemplateCheckingResult.getException().size());
JAXBContext jaxbContext = null;
try {
jaxbContext = JAXBContext.newInstance(ServiceTemplateCheckingResult.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(serviceTemplateCheckingResult, sw);
System.out.println(sw.toString());
} catch (JAXBException e) {
e.printStackTrace();
}
}
use of org.eclipse.winery.model.tosca.TExtensibleElements in project winery by eclipse.
the class WineryRepositoryClient method getDefinitions.
@Override
public Definitions getDefinitions(DefinitionsChildId id) {
for (WebResource wr : this.repositoryResources) {
String path = Util.getUrlPath(id);
Definitions definitions = WineryRepositoryClient.getDefinitions(wr.path(path));
if (definitions == null) {
// in case of an error, just try the next one
continue;
}
TExtensibleElements element = definitions.getElement();
if (element instanceof TEntityType) {
this.cache((TEntityType) element, id.getQName());
return definitions;
}
}
return new Definitions();
}
use of org.eclipse.winery.model.tosca.TExtensibleElements in project winery by eclipse.
the class AbstractComponentsWithTypeReferenceResource method onJsonPost.
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response onJsonPost(QNameWithTypeApiData jsonData) {
// only check for type parameter as namespace and name are checked in super.onPost
if (StringUtils.isEmpty(jsonData.type)) {
return Response.status(Status.BAD_REQUEST).build();
}
ResourceCreationResult creationResult = super.onPost(jsonData.namespace, jsonData.localname);
if (!creationResult.isSuccess()) {
return creationResult.getResponse();
}
if (creationResult.getStatus().equals(Status.CREATED)) {
final DefinitionsChildId id = (DefinitionsChildId) creationResult.getId();
final IRepository repository = RepositoryFactory.getRepository();
final Definitions definitions = repository.getDefinitions(id);
final TExtensibleElements element = definitions.getElement();
((HasType) element).setType(jsonData.type);
if (id instanceof EntityTemplateId) {
BackendUtils.initializeProperties(repository, (TEntityTemplate) element);
}
try {
BackendUtils.persist(id, definitions);
} catch (IOException e) {
throw new WebApplicationException(e);
}
}
return creationResult.getResponse();
}
Aggregations