Skip to content

Commit

Permalink
roboconf#645 Be able to add categories or tags to templates
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-zurczak committed Jun 14, 2017
1 parent 0fa1dbc commit eb40845
Show file tree
Hide file tree
Showing 18 changed files with 265 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class ApplicationTemplateDescriptor {

public static final String APPLICATION_NAME = "application-name";
public static final String APPLICATION_VERSION = "application-version";
public static final String APPLICATION_TAGS = "application-tags";
public static final String APPLICATION_DESCRIPTION = "application-description";
public static final String APPLICATION_DSL_ID = "application-dsl-id";
public static final String APPLICATION_GRAPH_EP = "graph-entry-point";
Expand All @@ -57,6 +58,7 @@ public class ApplicationTemplateDescriptor {
private String name, description, version, graphEntryPoint, instanceEntryPoint, dslId, externalExportsPrefix;
public final Map<String,String> externalExports = new HashMap<> ();
public final Set<String> invalidExternalExports = new HashSet<> ();
public final Set<String> tags = new HashSet<> ();


/**
Expand Down Expand Up @@ -174,6 +176,7 @@ public static ApplicationTemplateDescriptor load( Properties properties ) {
result.dslId = properties.getProperty( APPLICATION_DSL_ID, null );
result.externalExportsPrefix = properties.getProperty( APPLICATION_EXTERNAL_EXPORTS_PREFIX, null );

// Exports
final Pattern pattern = Pattern.compile(
"([^=\\s]+)\\s+" + APPLICATION_EXTERNAL_EXPORTS_AS + "\\s+([^=\\s]+)",
Pattern.CASE_INSENSITIVE );
Expand All @@ -187,6 +190,10 @@ public static ApplicationTemplateDescriptor load( Properties properties ) {
result.invalidExternalExports.add( rawExport );
}

// Tags
String rawTags = properties.getProperty( APPLICATION_TAGS, "" );
result.tags.addAll( Utils.splitNicely( rawTags, "," ));

return result;
}

Expand Down Expand Up @@ -252,6 +259,7 @@ public static void save( File f, ApplicationTemplateDescriptor descriptor ) thro
if( sb.length() > 0 )
properties.setProperty( APPLICATION_EXTERNAL_EXPORTS, sb.toString());

properties.setProperty( APPLICATION_TAGS, Utils.format( descriptor.tags, ", " ));
Utils.writePropertiesFile( properties, f );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public static ApplicationLoadResult loadApplication( File projectDirectory ) {
app.setVersion( appDescriptor.getVersion());
app.setDslId( appDescriptor.getDslId());
app.setExternalExportsPrefix( appDescriptor.getExternalExportsPrefix());
app.setTags( appDescriptor.tags );

for( Map.Entry<String,String> entry : appDescriptor.externalExports.entrySet())
app.externalExports.put( entry.getKey(), app.getExternalExportsPrefix() + "." + entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;

/**
* An application template groups an identifier, graph definitions and instances.
Expand All @@ -49,6 +52,12 @@ public class ApplicationTemplate extends AbstractApplication implements Serializ
// With a set, this could result in unpredictable behaviors.
private final List<Application> associatedApplications = new ArrayList<> ();

/**
* Tags associated with this template.
*/
// Private to guarantee atomic operations on it.
private final Set<String> tags = new TreeSet<> ();

/**
* External exports.
* <p>
Expand Down Expand Up @@ -227,4 +236,34 @@ void removeApplicationAssocation( Application app ) {
this.associatedApplications.remove( app );
}
}

/**
* Adds a tag.
* @param tag
*/
public void addTag( String tag ) {
synchronized( this.tags ) {
this.tags.add( tag );
}
}

/**
* Replaces all the tags.
* @param tags a (potentially null) collection of tags
*/
public void setTags( Collection<String> tags ) {

synchronized( this.tags ) {
this.tags.clear();
if( tags != null )
this.tags.addAll( tags );
}
}

/**
* @return the tags
*/
public Set<String> getTags() {
return Collections.unmodifiableSet( this.tags );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
/**
* @author Vincent Zurczak - Linagora
*/
public class IncopleteFilesParsingTest {
public class IncompleteFilesParsingTest {

@Test
public void testIncompleteFilesResultInEmptyGraphByDefault() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
import java.util.UUID;

import org.junit.Assert;
import net.roboconf.core.utils.Utils;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import net.roboconf.core.utils.Utils;

/**
* @author Vincent Zurczak - Linagora
*/
Expand Down Expand Up @@ -76,6 +76,10 @@ public void testSaveAndLoad() throws Exception {

desc1.setExternalExportsPrefix( "whatever" );
saveAndCompare( desc1 );

desc1.tags.add( "t1" );
desc1.tags.add( "t45" );
saveAndCompare( desc1 );
}


Expand Down Expand Up @@ -134,6 +138,7 @@ private void saveAndCompare( ApplicationTemplateDescriptor desc1 ) throws Except
Assert.assertEquals( desc1.getGraphEntryPoint(), desc2.getGraphEntryPoint());
Assert.assertEquals( desc1.getInstanceEntryPoint(), desc2.getInstanceEntryPoint());
Assert.assertEquals( desc1.externalExports, desc2.externalExports );
Assert.assertEquals( desc1.tags, desc2.tags );
Assert.assertEquals( desc1.invalidExternalExports, desc2.invalidExternalExports );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;

import org.junit.Assert;
import org.junit.Rule;
Expand Down Expand Up @@ -86,6 +87,9 @@ public void testLoadApplication_Lamp_Legacy_1() throws Exception {
Assert.assertEquals( "A sample LAMP application", result.applicationTemplate.getDescription());
Assert.assertEquals( "1.0.1-SNAPSHOT", result.applicationTemplate.getVersion());

List<String> tags = Arrays.asList( "test", "lamp", "example" );
Assert.assertEquals( new TreeSet<>( tags ), result.applicationTemplate.getTags());

Assert.assertNotNull( result.applicationTemplate.getGraphs());
Graphs g = result.applicationTemplate.getGraphs();
Assert.assertEquals( 1, g.getRootComponents().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package net.roboconf.core.model.beans;

import java.util.HashSet;
import java.util.Set;

import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -139,4 +140,22 @@ public void testSetNameWithAccents() {
Assert.assertEquals( "aeu eea", app.getName());
Assert.assertEquals( "âêû éèà", app.getDisplayName());
}


@Test
public void testTags() {

ApplicationTemplate app = new ApplicationTemplate();
Assert.assertEquals( 0, app.getTags().size());

app.addTag( "toto" );
Assert.assertEquals( 1, app.getTags().size());
Assert.assertEquals( "toto", app.getTags().iterator().next());

Set<String> newTags = new HashSet<> ();
newTags.add( "titi" );
newTags.add( "tutu" );
app.setTags( newTags );
Assert.assertEquals( newTags, app.getTags());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ application-name = Legacy LAMP
application-version = 1.0.1-SNAPSHOT
application-dsl-id = roboconf-1.0
application-description = A sample LAMP application
application-tags = test, lamp, example

graph-entry-point = lamp.graph
instance-entry-point = initial.instances
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public static Map<Class<?>,? super JsonSerializer<?>> getSerializers() {
private static final String APP_INST_TPL_EEP = "tplEep";

private static final String APP_TPL_APPS = "apps";
private static final String APP_TPL_TAGS = "tags";
private static final String COMP_INSTALLER = "installer";

private static final String INST_CHANNELS = "channels";
Expand Down Expand Up @@ -625,7 +626,7 @@ public void serialize(
for( Application associatedApp : app.getAssociatedApplications()) {

// #483 We do not know why, but after we delete an application
// from the web console, the resulting JSon array sometimes contain null.
// from the web console, the resulting JSon array sometimes contains null.
// This prevents the deletion of a template that does not have applications anymore.
// The "IF" is a WORKAROUND.
if( associatedApp != null
Expand All @@ -636,6 +637,17 @@ public void serialize(
}

generator.writeEndArray();

// Tags
Set<String> tags = app.getTags();
if( ! tags.isEmpty()) {
generator.writeArrayFieldStart( APP_TPL_TAGS );
for( String s : tags )
generator.writeString( s );

generator.writeEndArray();
}

generator.writeEndObject();
}
}
Expand Down Expand Up @@ -699,6 +711,11 @@ else if(( n = node.get( NAME )) != null )
if(( n = node.get( EEP )) != null )
application.setExternalExportsPrefix( n.textValue());

if(( n = node.get( APP_TPL_TAGS )) != null ) {
for( JsonNode arrayNodeItem : n )
application.addTag( arrayNodeItem.textValue());
}

return application;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void testApplicationTemplateBinding_1() throws Exception {
Assert.assertEquals( app.getDescription(), readApp.getDescription());
Assert.assertEquals( app.getVersion(), readApp.getVersion());
Assert.assertEquals( app.getExternalExportsPrefix(), readApp.getExternalExportsPrefix());
Assert.assertEquals( app.getTags(), readApp.getTags());
}


Expand All @@ -111,6 +112,7 @@ public void testApplicationTemplateBinding_2() throws Exception {
Assert.assertEquals( app.getDescription(), readApp.getDescription());
Assert.assertEquals( app.getVersion(), readApp.getVersion());
Assert.assertEquals( app.getExternalExportsPrefix(), readApp.getExternalExportsPrefix());
Assert.assertEquals( app.getTags(), readApp.getTags());
}


Expand Down Expand Up @@ -149,6 +151,7 @@ public void testApplicationTemplateBinding_4() throws Exception {
Assert.assertEquals( app.getDescription(), readApp.getDescription());
Assert.assertEquals( app.getVersion(), readApp.getVersion());
Assert.assertEquals( app.getExternalExportsPrefix(), readApp.getExternalExportsPrefix());
Assert.assertEquals( app.getTags(), readApp.getTags());
}


Expand All @@ -169,6 +172,7 @@ public void testApplicationTemplateBinding_5() throws Exception {
Assert.assertEquals( app.getDescription(), readApp.getDescription());
Assert.assertEquals( app.getVersion(), readApp.getVersion());
Assert.assertEquals( app.getExternalExportsPrefix(), readApp.getExternalExportsPrefix());
Assert.assertEquals( app.getTags(), readApp.getTags());
}


Expand Down Expand Up @@ -244,17 +248,19 @@ public void testApplicationTemplateBinding_9() throws Exception {
ObjectMapper mapper = JSonBindingUtils.createObjectMapper();

ApplicationTemplate app = new ApplicationTemplate( "àéoçù" ).description( "some text" ).version( "v1" );

StringWriter writer = new StringWriter();
mapper.writeValue( writer, app );
String s = writer.toString();

Assert.assertEquals( result, s );

ApplicationTemplate readApp = mapper.readValue( result, ApplicationTemplate.class );
Assert.assertEquals( app, readApp );
Assert.assertEquals( app.getName(), readApp.getName());
Assert.assertEquals( app.getDescription(), readApp.getDescription());
Assert.assertEquals( app.getVersion(), readApp.getVersion());
Assert.assertEquals( app.getExternalExportsPrefix(), readApp.getExternalExportsPrefix());
Assert.assertEquals( app.getTags(), readApp.getTags());
}


Expand All @@ -272,6 +278,32 @@ public void testApplicationTemplateBinding_10() throws Exception {
Assert.assertEquals( app.getDescription(), readApp.getDescription());
Assert.assertEquals( app.getVersion(), readApp.getVersion());
Assert.assertEquals( app.getExternalExportsPrefix(), readApp.getExternalExportsPrefix());
Assert.assertEquals( app.getTags(), readApp.getTags());
}


@Test
public void testApplicationTemplateBinding_11() throws Exception {

ObjectMapper mapper = JSonBindingUtils.createObjectMapper();
final String result =
"{\"name\":\"coucou\",\"displayName\":\"coucou\",\"desc\":\"some text\",\"version\":\"v1\",\"apps\":[],\"tags\":[\"t1\",\"t21\",\"t4\"]}";

ApplicationTemplate app = new ApplicationTemplate( "coucou" ).description( "some text" ).version( "v1" );
app.setTags( Arrays.asList( "t1", "t21", "t4" ));

StringWriter writer = new StringWriter();
mapper.writeValue( writer, app );
String s = writer.toString();
Assert.assertEquals( result, s );

ApplicationTemplate readApp = mapper.readValue( result, ApplicationTemplate.class );
Assert.assertEquals( app, readApp );
Assert.assertEquals( app.getName(), readApp.getName());
Assert.assertEquals( app.getDescription(), readApp.getDescription());
Assert.assertEquals( app.getVersion(), readApp.getVersion());
Assert.assertEquals( app.getExternalExportsPrefix(), readApp.getExternalExportsPrefix());
Assert.assertEquals( app.getTags(), readApp.getTags());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ List<Instance> listChildrenInstances(
*
* @HTTP 200 Everything went fine.
* @HTTP 204 No instruction was found.
* @HTTP 404 The application was not found.
*/
@GET
@Path( "/commands/{command-name}" )
Expand All @@ -364,4 +365,22 @@ List<Instance> listChildrenInstances(
@POST
@Path( "/commands/execute" )
Response executeCommand( @PathParam("name") String app, @QueryParam("command-name") String commandName );


/**
* Replaces the tags for an application template.
* @param name the application name
* @param version the application version
* @param tags the tags
* @return a response
*
* @HTTP 200 Everything went fine.
* @HTTP 404 The application template was not found.
*/
@POST
@Path( "/tags" )
Response replaceTags(
@PathParam("name") String name,
@QueryParam("version") String version,
@QueryParam("tags") List<String> tags );
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Response loadUploadedZippedApplicationTemplate(
* </p>
*
* @param exactQualifier the exact qualifier to search (null to match all)
* @param tag a tag to filter results (null to match all)
* @return a non-null list of application templates (with at most one element if "exactName" was set)
* @HTTP 200 Everything went fine.
*/
Expand All @@ -154,7 +155,8 @@ Response loadUploadedZippedApplicationTemplate(
@Produces( MediaType.APPLICATION_JSON )
List<ApplicationTemplate> listApplicationTemplates(
@QueryParam( "name" ) String exactName,
@QueryParam( "qualifier" ) String exactQualifier );
@QueryParam( "qualifier" ) String exactQualifier,
@QueryParam( "tag" ) String tag );


/**
Expand Down
Loading

0 comments on commit eb40845

Please sign in to comment.