public void testSimpleLifecycle() throws Exception { final String RESOURCE = "/r1"; final String LIFECYCLE = "simpleLifecycle"; RegistryContext context = registry.getRegistryContext(); context.selectDBConfig("h2-db"); context.addAspect(LIFECYCLE, new SimpleLifecycle(), MultitenantConstants.SUPER_TENANT_ID); String[] aspects = registry.getAvailableAspects(); assertTrue(aspects.length > 0); boolean found = false; for (String aspect : aspects) { if (aspect.equals(LIFECYCLE)) { found = true; } } assertTrue("Lifecycle not found in available aspects", found); Resource resource = registry.newResource(); resource.setDescription("My thing"); registry.put(RESOURCE, resource); registry.associateAspect(RESOURCE, LIFECYCLE); resource = registry.get(RESOURCE); assertNotNull(resource); String propValue = resource.getProperty(SimpleLifecycle.STATE_PROP); assertNotNull(propValue); assertEquals("Wrong initial state!", SimpleLifecycle.INIT, propValue); String[] actions = registry.getAspectActions(RESOURCE, LIFECYCLE); assertNotNull("No available actions", actions); assertEquals("Wrong # of available actions", 1, actions.length); assertEquals("Wrong available action", SimpleLifecycle.ACTION, actions[0]); registry.invokeAspect(RESOURCE, LIFECYCLE, SimpleLifecycle.ACTION); resource = registry.get(RESOURCE); // OK, now we should be in the next state propValue = resource.getProperty(SimpleLifecycle.STATE_PROP); assertNotNull(propValue); assertEquals("Wrong state!", SimpleLifecycle.FINAL, propValue); }
public void testLifecycle() throws RegistryException { Resource r1 = registry.newResource(); byte[] r1content = RegistryUtils.encodeString("R1 content"); r1.setContent(r1content); registry.put("/d12/r1", r1); String text1 = "this can be used as a test resource."; String text2 = "I like this"; final Comment comment1 = new Comment(text1); comment1.setUser("someone"); registry.addComment("/d12/r1", comment1); final Comment comment2 = new Comment(text2); comment2.setUser("someone"); registry.addComment("/d12/r1", comment2); Comment[] comments = registry.getComments("/d12/r1"); Assert.assertNotNull(registry.get("/d12/r1").getContent()); boolean commentFound = false; for (Comment comment : comments) { if (comment.getText().equals(text1)) { commentFound = true; break; } } Assert.assertTrue( "comment '" + text1 + "' is not associated with the artifact /d12/r1", commentFound); Resource commentsResource = registry.get("/d12/r1;comments"); Assert.assertTrue( "Comment collection resource should be a directory.", commentsResource instanceof Collection); comments = (Comment[]) commentsResource.getContent(); List commentTexts = new ArrayList(); for (Comment comment : comments) { Resource commentResource = registry.get(comment.getPath()); commentTexts.add(commentResource.getContent()); } Assert.assertTrue( text1 + " is not associated for resource /d12/r1.", commentTexts.contains(text1)); Assert.assertTrue( text2 + " is not associated for resource /d12/r1.", commentTexts.contains(text2)); registry.associateAspect("/d12/r1", LIFECYCLE_NAME); registry.invokeAspect("/d12/r1", LIFECYCLE_NAME, "promote"); Resource resource = registry.get("/developed/d12/r1"); Assert.assertNotNull(resource); Assert.assertNotNull(resource.getContent()); comments = registry.getComments("/developed/d12/r1"); commentFound = false; for (Comment comment : comments) { if (comment.getText().equals(text1)) { commentFound = true; break; } } Assert.assertTrue( "comment '" + text1 + "' is not associated with the artifact /developed/d12/r1", commentFound); commentsResource = registry.get("/developed/d12/r1;comments"); Assert.assertTrue( "Comment collection resource should be a directory.", commentsResource instanceof Collection); comments = (Comment[]) commentsResource.getContent(); commentTexts = new ArrayList(); for (Comment comment : comments) { Resource commentResource = registry.get(comment.getPath()); commentTexts.add(commentResource.getContent()); } Assert.assertTrue( text1 + " is not associated for resource /developed/d12/r1.", commentTexts.contains(text1)); Assert.assertTrue( text2 + " is not associated for resource /developed/d12/r1.", commentTexts.contains(text2)); }
/** * Saves the REST Service registry artifact created from the imported swagger definition. * * @param requestContext information about current request. * @param data service artifact metadata. * @throws RegistryException If a failure occurs when adding the api to registry. */ public static String addServiceToRegistry(RequestContext requestContext, OMElement data) throws RegistryException { if (requestContext == null || data == null) { throw new IllegalArgumentException( "Some or all of the arguments may be null. Cannot add the rest service to registry. "); } Registry registry = requestContext.getRegistry(); // Creating new resource. Resource serviceResource = new ResourceImpl(); // setting API media type. serviceResource.setMediaType(CommonConstants.REST_SERVICE_MEDIA_TYPE); serviceResource.setProperty(CommonConstants.SOURCE_PROPERTY, CommonConstants.SOURCE_AUTO); OMElement overview = data.getFirstChildWithName(new QName(CommonConstants.SERVICE_ELEMENT_NAMESPACE, OVERVIEW)); String serviceVersion = overview .getFirstChildWithName(new QName(CommonConstants.SERVICE_ELEMENT_NAMESPACE, VERSION)) .getText(); String apiName = overview .getFirstChildWithName(new QName(CommonConstants.SERVICE_ELEMENT_NAMESPACE, NAME)) .getText(); serviceVersion = (serviceVersion == null) ? CommonConstants.SERVICE_VERSION_DEFAULT_VALUE : serviceVersion; String serviceProvider = CarbonContext.getThreadLocalCarbonContext().getUsername(); String pathExpression = getRestServicePath(requestContext, data, apiName, serviceProvider); // set version property. serviceResource.setProperty(RegistryConstants.VERSION_PARAMETER_NAME, serviceVersion); // copy other property serviceResource.setProperties(copyProperties(requestContext)); // set content. serviceResource.setContent(RegistryUtils.encodeString(data.toString())); String resourceId = serviceResource.getUUID(); // set resource UUID resourceId = (resourceId == null) ? UUID.randomUUID().toString() : resourceId; serviceResource.setUUID(resourceId); String servicePath = getChrootedServiceLocation(requestContext.getRegistryContext()) + CarbonContext.getThreadLocalCarbonContext().getUsername() + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + serviceVersion + RegistryConstants.PATH_SEPARATOR + apiName + "-rest_service"; // saving the api resource to repository. registry.put(pathExpression, serviceResource); String defaultLifeCycle = CommonUtil.getDefaultLifecycle(registry, "restservice"); if (defaultLifeCycle != null && !defaultLifeCycle.isEmpty()) { registry.associateAspect(serviceResource.getId(), defaultLifeCycle); } if (log.isDebugEnabled()) { log.debug("REST Service created at " + pathExpression); } return pathExpression; }
/** * Adds the given artifact to the registry. Please do not use this method to update an existing * artifact use the update method instead. If this method is used to update an existing artifact, * all existing properties (such as lifecycle details) will be removed from the existing artifact. * * @param artifact the artifact. * @throws GovernanceException if the operation failed. */ public void addGovernanceArtifact(GovernanceArtifact artifact) throws GovernanceException { // adding the attributes for name, namespace + artifact if (artifact.getQName() == null || artifact.getQName().getLocalPart() == null) { String msg = "A valid qualified name was not set for this artifact"; log.error(msg); throw new GovernanceException(msg); } validateArtifact(artifact); String artifactName = artifact.getQName().getLocalPart(); artifact.setAttributes(artifactNameAttribute, new String[] {artifactName}); // namespace can be null String namespace = artifact.getQName().getNamespaceURI(); if (artifactNamespaceAttribute != null) { artifact.setAttributes(artifactNamespaceAttribute, new String[] {namespace}); } ((GovernanceArtifactImpl) artifact).associateRegistry(registry); boolean succeeded = false; try { registry.beginTransaction(); Resource resource = registry.newResource(); resource.setMediaType(mediaType); setContent(artifact, resource); // the artifact will not actually stored in the tmp path. String path = GovernanceUtils.getPathFromPathExpression(pathExpression, artifact); if (registry.resourceExists(path)) { throw new GovernanceException( "Governance artifact " + artifactName + " already exists at " + path); } String artifactId = artifact.getId(); resource.setUUID(artifactId); registry.put(path, resource); if (lifecycle != null) { registry.associateAspect(path, lifecycle); } ((GovernanceArtifactImpl) artifact).updatePath(); // artifact.setId(resource.getUUID()); //This is done to get the UUID of a existing // resource. addRelationships(path, artifact); succeeded = true; } catch (RegistryException e) { String msg; if (artifact.getPath() != null) { msg = "Failed to add artifact: artifact id: " + artifact.getId() + ", path: " + artifact.getPath() + ". " + e.getMessage(); } else { msg = "Failed to add artifact: artifact id: " + artifact.getId() + ". " + e.getMessage(); } log.error(msg, e); throw new GovernanceException(msg, e); } finally { if (succeeded) { try { registry.commitTransaction(); } catch (RegistryException e) { String msg; if (artifact.getPath() != null) { msg = "Error in committing transactions. Failed to add artifact: artifact " + "id: " + artifact.getId() + ", path: " + artifact.getPath() + "."; } else { msg = "Error in committing transactions. Failed to add artifact: artifact " + "id: " + artifact.getId() + "."; } log.error(msg, e); } } else { try { registry.rollbackTransaction(); } catch (RegistryException e) { String msg = "Error in rolling back transactions. Failed to add artifact: " + "artifact id: " + artifact.getId() + ", path: " + artifact.getPath() + "."; log.error(msg, e); } } } }