1   package com.atlassian.maven.plugins.amps.product.studio;
2   
3   import com.atlassian.maven.plugins.amps.MavenContext;
4   import com.atlassian.maven.plugins.amps.MavenGoals;
5   import com.atlassian.maven.plugins.amps.Product;
6   import com.google.inject.internal.util.ImmutableMap;
7   import com.google.inject.internal.util.Maps;
8   import org.apache.commons.io.FileUtils;
9   import org.apache.maven.model.Build;
10  import org.apache.maven.model.Model;
11  import org.apache.maven.plugin.MojoExecutionException;
12  import org.apache.maven.plugin.logging.Log;
13  import org.apache.maven.project.MavenProject;
14  import org.junit.After;
15  import org.junit.Before;
16  import org.junit.Test;
17  import org.junit.runner.RunWith;
18  import org.mockito.Mock;
19  import org.mockito.runners.MockitoJUnitRunner;
20  
21  import java.io.File;
22  import java.util.Map;
23  
24  import static com.atlassian.maven.plugins.amps.product.ProductHandlerFactory.STUDIO;
25  import static com.atlassian.maven.plugins.amps.product.ProductHandlerFactory.STUDIO_BAMBOO;
26  import static com.atlassian.maven.plugins.amps.product.ProductHandlerFactory.STUDIO_CONFLUENCE;
27  import static com.atlassian.maven.plugins.amps.product.ProductHandlerFactory.STUDIO_CROWD;
28  import static com.atlassian.maven.plugins.amps.product.ProductHandlerFactory.STUDIO_FECRU;
29  import static com.atlassian.maven.plugins.amps.product.ProductHandlerFactory.STUDIO_JIRA;
30  import static org.junit.Assert.assertEquals;
31  import static org.mockito.Mockito.verify;
32  import static org.mockito.Mockito.when;
33  
34  /**
35   * Test case for {@link com.atlassian.maven.plugins.amps.product.studio.StudioProductHandler}.
36   *
37   */
38  @RunWith(MockitoJUnitRunner.class)
39  public class TestStudioProductHandler
40  {
41      private static final String TEST_ONDEMAND_VERSION = "131";
42  
43      @Mock private MavenContext mockContext;
44  
45      @Mock private MavenProject mockProject;
46  
47      @Mock private Build mockBuild;
48  
49      @Mock private Log mockLog;
50  
51      @Mock private MavenGoals mockGoals;
52  
53      private File mockBuildDir;
54  
55      @Before
56      public void initMocks()
57      {
58          mockBuildDir = new File(System.getProperty("java.io.tmpdir"), "TestStudioProductHandler");
59          when(mockBuild.getDirectory()).thenReturn(mockBuildDir.getAbsolutePath());
60          when(mockProject.getBuild()).thenReturn(mockBuild);
61          when(mockContext.getProject()).thenReturn(mockProject);
62          when(mockContext.getLog()).thenReturn(mockLog);
63      }
64  
65      @After
66      public void deleteMockBuildDir()
67      {
68          FileUtils.deleteQuietly(mockBuildDir);
69      }
70  
71      @Test
72      public void configureProductsShouldSetCorrectJiraVersionFromOnDemandPom() throws MojoExecutionException
73      {
74          final Model model = new Model();
75          model.addProperty("jira.version", "5.0");
76          final TestedStudioProductHandler testedHandler = new TestedStudioProductHandler(mockContext, mockGoals, model);
77          final Product jira = createProduct(STUDIO_JIRA, null);
78          Map<String,Product> products = createProductsWith(STUDIO_JIRA, jira);
79          initDefaultValues(products);
80          testedHandler.configureStudioProducts(products);
81          assertEquals("5.0", jira.getVersion());
82      }
83  
84      @Test
85      public void configureProductsShouldSetCorrectConfluenceVersionFromOnDemandPom() throws MojoExecutionException
86      {
87          final Model model = new Model();
88          model.addProperty("confluence.version", "4.2");
89          final TestedStudioProductHandler testedHandler = new TestedStudioProductHandler(mockContext, mockGoals, model);
90          final Product confluence = createProduct(STUDIO_CONFLUENCE, null);
91          Map<String,Product> products = createProductsWith(STUDIO_CONFLUENCE, confluence);
92          initDefaultValues(products);
93          testedHandler.configureStudioProducts(products);
94          assertEquals("4.2", confluence.getVersion());
95      }
96  
97      @Test
98      public void configureProductsShouldSetOnDemandFecruVersion() throws MojoExecutionException
99      {
100         final Model model = new Model();
101         model.addProperty("crucible.version", "2.8");
102         final TestedStudioProductHandler testedHandler = new TestedStudioProductHandler(mockContext, mockGoals, model);
103         final Product fecru = createProduct(STUDIO_FECRU, null);
104         Map<String,Product> products = createProductsWith(STUDIO_FECRU, fecru);
105         initDefaultValues(products);
106         testedHandler.configureStudioProducts(products);
107         assertEquals(TEST_ONDEMAND_VERSION, fecru.getVersion());
108     }
109 
110     @Test
111     public void configureProductsShouldLogWarningIfJiraVersionNotPresentInOnDemandPom() throws MojoExecutionException
112     {
113         final Model model = new Model();
114         model.addProperty("confluence.version", "4.2");
115         final TestedStudioProductHandler testedHandler = new TestedStudioProductHandler(mockContext, mockGoals, model);
116         testedHandler.configureStudioProducts(createDefaultStudioProducts());
117         verify(mockLog).warn("Expected property 'jira.version' in the OnDemand fireball POM (version "
118                         + TEST_ONDEMAND_VERSION + ") not found. OnDemand version will be used instead");
119     }
120 
121     @Test
122     public void configureProductsShouldLogWarningIfConfluenceVersionNotPresentInOnDemandPom() throws MojoExecutionException
123     {
124         final Model model = new Model();
125         model.addProperty("jira.version", "5.1");
126         final TestedStudioProductHandler testedHandler = new TestedStudioProductHandler(mockContext, mockGoals, model);
127         testedHandler.configureStudioProducts(createDefaultStudioProducts());
128         verify(mockLog).warn("Expected property 'confluence.version' in the OnDemand fireball POM (version "
129                         + TEST_ONDEMAND_VERSION + ") not found. OnDemand version will be used instead");
130     }
131 
132     private void initDefaultValues(Map<String,Product> products)
133     {
134         for (Product product : products.values())
135         {
136             StudioProductHandler.setDefaultValues(mockContext, product);
137         }
138     }
139 
140     private Map<String,Product> createProductsWith(String productId, Product product)
141     {
142         final Map<String,Product> products = Maps.newHashMap();
143         products.putAll(createDefaultStudioProducts());
144         products.put(productId, product);
145         return ImmutableMap.copyOf(products);
146     }
147 
148     private Map<String,Product> createDefaultStudioProducts()
149     {
150         return ImmutableMap.<String,Product>builder()
151             .put(STUDIO, createProduct(STUDIO, TEST_ONDEMAND_VERSION))
152             .put(STUDIO_JIRA, createProduct(STUDIO_JIRA, "5.0"))
153             .put(STUDIO_CROWD, createProduct(STUDIO_CROWD, TEST_ONDEMAND_VERSION))
154             .put(STUDIO_CONFLUENCE, createProduct(STUDIO_CONFLUENCE, "4.1"))
155             .put(STUDIO_BAMBOO, createProduct(STUDIO_BAMBOO, TEST_ONDEMAND_VERSION))
156             .put(STUDIO_FECRU, createProduct(STUDIO_FECRU, TEST_ONDEMAND_VERSION))
157             .build();
158     }
159 
160     private Product createProduct(String id, String version)
161     {
162         final Product answer = new Product();
163         answer.setId(id);
164         answer.setInstanceId(id);
165         answer.setVersion(version);
166         return answer;
167     }
168 
169     private static class TestedStudioProductHandler extends StudioProductHandler
170     {
171         private final Model onDemandModelMock;
172 
173         public TestedStudioProductHandler(MavenContext context, MavenGoals goals, Model onDemandModelMock)
174         {
175             super(context, goals);
176             this.onDemandModelMock = onDemandModelMock;
177         }
178 
179         @Override
180         protected Model getOnDemandPomModel(Product ondemand, StudioProperties properties) throws MojoExecutionException
181         {
182             return onDemandModelMock;
183         }
184     }
185 }