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