PageRenderTime 39ms CodeModel.GetById 15ms app.highlight 19ms RepoModel.GetById 2ms app.codeStats 0ms

/src/test/java/nl/bitbrains/nebu/rest/server/TestDeploymentsProvider.java

https://github.com/deltaforge/nebu-core
Java | 343 lines | 258 code | 61 blank | 24 comment | 2 complexity | e3c85e7909a666973f1e3ed71808ed54 MD5 | raw file
  1package nl.bitbrains.nebu.rest.server;
  2
  3import java.text.ParseException;
  4import java.util.HashMap;
  5import java.util.List;
  6import java.util.Map;
  7
  8import javax.ws.rs.client.Entity;
  9import javax.ws.rs.core.MediaType;
 10import javax.ws.rs.core.Response;
 11
 12import nl.bitbrains.nebu.common.cache.CacheException;
 13import nl.bitbrains.nebu.common.cache.CacheManager;
 14import nl.bitbrains.nebu.common.factories.IdentifiableFactory;
 15import nl.bitbrains.nebu.common.util.xml.XMLConverter;
 16import nl.bitbrains.nebu.containers.Application;
 17import nl.bitbrains.nebu.containers.ApplicationBuilder;
 18import nl.bitbrains.nebu.containers.Deployment;
 19import nl.bitbrains.nebu.containers.DeploymentBuilder;
 20import nl.bitbrains.nebu.containers.DeploymentFactory;
 21import nl.bitbrains.nebu.containers.DeploymentRequest;
 22import nl.bitbrains.nebu.containers.DeploymentRequestFactory;
 23import nl.bitbrains.nebu.containers.VMTemplate;
 24import nl.bitbrains.nebu.containers.VMTemplateBuilder;
 25import nl.bitbrains.nebu.deployer.Deployer;
 26import nl.bitbrains.nebu.deployer.DeployerException;
 27import nl.bitbrains.nebu.deployer.MissingPolicyException;
 28import nl.bitbrains.nebu.rest.RESTRequestException;
 29import nl.bitbrains.nebu.rest.client.RequestSender;
 30import nl.bitbrains.nebu.rest.server.AppsProvider;
 31import nl.bitbrains.nebu.rest.server.DeploymentsProvider;
 32
 33import org.glassfish.jersey.server.ResourceConfig;
 34import org.glassfish.jersey.test.JerseyTest;
 35import org.jdom2.Element;
 36import org.jdom2.JDOMException;
 37import org.junit.Assert;
 38import org.junit.Test;
 39import org.junit.runner.RunWith;
 40import org.mockito.Matchers;
 41import org.mockito.Mock;
 42import org.mockito.Mockito;
 43import org.mockito.MockitoAnnotations;
 44import org.powermock.api.mockito.PowerMockito;
 45import org.powermock.core.classloader.annotations.PowerMockIgnore;
 46import org.powermock.core.classloader.annotations.PrepareForTest;
 47import org.powermock.modules.junit4.PowerMockRunner;
 48import org.w3c.dom.Document;
 49
 50/**
 51 * @author Jesse Donkervliet, Tim Hegeman, and Stefan Hugtenburg
 52 * 
 53 */
 54@RunWith(PowerMockRunner.class)
 55@PrepareForTest({ RequestSender.class, CacheManager.class, XMLConverter.class, Application.class,
 56        VMTemplate.class, Deployment.class, Deployer.class })
 57@PowerMockIgnore("javax.management.*")
 58public class TestDeploymentsProvider extends JerseyTest {
 59    @Mock
 60    private RequestSender reqSender;
 61
 62    private Deployer deployer;
 63
 64    // IS currently not mocked! FIXME TODO: Check if this is possible without
 65    // stackoverflow from powermock.
 66    private Deployment mockedDeployment;
 67
 68    private nl.bitbrains.nebu.containers.Application app;
 69
 70    private VMTemplate template1;
 71    private VMTemplate template2;
 72
 73    private final String id = "id1";
 74    private final String templateID1 = "templateID";
 75    private final int num1 = 10;
 76    private final String templateID2 = "templateID2";
 77    private final int num2 = 5;
 78
 79    private final String depID = "depid";
 80
 81    @Override
 82    protected javax.ws.rs.core.Application configure() {
 83        return new ResourceConfig(AppsProvider.class);
 84    }
 85
 86    @Override
 87    public void setUp() throws Exception {
 88        super.setUp();
 89        MockitoAnnotations.initMocks(this);
 90        PowerMockito.mockStatic(CacheManager.class);
 91        this.deployer = PowerMockito.mock(Deployer.class);
 92
 93        this.app = new ApplicationBuilder().withUuid(this.id).build();
 94        this.template1 = new VMTemplateBuilder().withUuid(this.templateID1).build();
 95        this.template2 = new VMTemplateBuilder().withUuid(this.templateID2).build();
 96        this.mockedDeployment = new DeploymentBuilder().withUuid(this.depID).build();
 97
 98        // this.app = PowerMockito.mock(Application.class);
 99        // this.template1 = PowerMockito.mock(VMTemplate.class);
100        // this.template2 = PowerMockito.mock(VMTemplate.class);
101        // this.mockedDeployment = PowerMockito.mock(Deployment.class);
102    }
103
104    private void setUpCacheManager(final String key, final Object value) throws CacheException {
105        Mockito.when(CacheManager.get(key)).thenReturn(value);
106    }
107
108    private void setUpCacheManagerException(final String key, final Throwable error)
109            throws CacheException {
110        Mockito.when(CacheManager.get(key)).thenThrow(error);
111    }
112
113    private Document getPostRootBody(final Map<String, Integer> templates) throws JDOMException {
114        final Element root = new Element(DeploymentRequestFactory.TAG_ELEMENT_ROOT);
115        for (final String key : templates.keySet()) {
116            final Element template = this.makeTemplateElem(key, templates.get(key));
117            root.addContent(template);
118        }
119        return XMLConverter.convertJDOMElementW3CDocument(root);
120    }
121
122    private Element makeTemplateElem(final String id, final int number) {
123        final Element result = new Element(DeploymentRequestFactory.TAG_ELEMENT_TEMPLATE);
124        result.setAttribute(IdentifiableFactory.TAG_ID, id);
125        result.addContent(new Element(DeploymentRequestFactory.TAG_ELEMENT_NUMBER).setText(Integer
126                .toString(number)));
127        return result;
128    }
129
130    private void mockRequestSender() {
131        PowerMockito.mockStatic(RequestSender.class);
132        Mockito.when(RequestSender.get()).thenReturn(this.reqSender);
133    }
134
135    private void mockRequestSender(final Throwable e) throws RESTRequestException {
136        PowerMockito.mockStatic(RequestSender.class);
137        Mockito.when(RequestSender.get()).thenReturn(this.reqSender);
138        Mockito.doThrow(e).when(this.reqSender)
139                .putTemplate(Matchers.anyString(), Matchers.any(Element.class));
140    }
141
142    // private void createAppWithTemplate() throws CacheException {
143    // final Map<String, Application> map = new HashMap<String, Application>();
144    // map.put(this.id, this.app);
145    // Mockito.when(this.app.getUniqueIdentifier()).thenReturn(this.id);
146    // Mockito.when(this.app.getVMTemplate(this.templateID1)).thenReturn(this.template1);
147    // Mockito.when(this.app.getVMTemplate(this.templateID2)).thenReturn(this.template2);
148    // Mockito.when(this.template1.getUniqueIdentifier()).thenReturn(this.templateID1);
149    // Mockito.when(this.template2.getUniqueIdentifier()).thenReturn(this.templateID2);
150    // this.setUpCacheManager(AppsProvider.CACHE_KEY_APPS, map);
151    // this.setUpCacheManager(Deployer.CACHE_KEY, this.deployer);
152    // Mockito.when(this.deployer.generateDeployment(Matchers.any(DeploymentRequest.class)))
153    // .thenReturn(this.mockedDeployment);
154    // Mockito.when(this.mockedDeployment.getUniqueIdentifier()).thenReturn(this.depID);
155    // }
156
157    private void createAppWithTemplates() throws CacheException, MissingPolicyException,
158            DeployerException {
159        final Map<String, Application> map = new HashMap<String, Application>();
160        map.put(this.id, this.app);
161        this.app.putVMTemplate(this.template1);
162        this.app.putVMTemplate(this.template2);
163        this.setUpCacheManager(AppsProvider.CACHE_KEY_APPS, map);
164        this.setUpCacheManager(Deployer.CACHE_KEY, this.deployer);
165        Mockito.when(this.deployer.generateDeployment(Matchers.any(DeploymentRequest.class)))
166                .thenReturn(this.mockedDeployment);
167
168    }
169
170    private void createAppWithTemplatesCacheException() throws CacheException,
171            MissingPolicyException, DeployerException {
172        final Map<String, Application> map = new HashMap<String, Application>();
173        map.put(this.id, this.app);
174        this.app.putVMTemplate(this.template1);
175        this.app.putVMTemplate(this.template2);
176        this.setUpCacheManager(AppsProvider.CACHE_KEY_APPS, map);
177        this.setUpCacheManagerException(Deployer.CACHE_KEY, new CacheException(""));
178    }
179
180    private void createAppWithTemplatesDeployerException() throws CacheException,
181            MissingPolicyException, DeployerException {
182        final Map<String, Application> map = new HashMap<String, Application>();
183        map.put(this.id, this.app);
184        this.app.putVMTemplate(this.template1);
185        this.app.putVMTemplate(this.template2);
186        this.setUpCacheManager(AppsProvider.CACHE_KEY_APPS, map);
187        this.setUpCacheManager(Deployer.CACHE_KEY, this.deployer);
188        Mockito.when(this.deployer.generateDeployment(Matchers.any(DeploymentRequest.class)))
189                .thenThrow(new DeployerException(""));
190    }
191
192    @Test
193    public void testGetAllDeploymentsNone() throws CacheException, ParseException,
194            MissingPolicyException, DeployerException {
195        this.createAppWithTemplates();
196
197        final Response rep = this.target(AppsProvider.PATH).path(this.id)
198                .path(DeploymentsProvider.PATH).request().get();
199
200        Assert.assertEquals(Response.Status.OK.getStatusCode(), rep.getStatus());
201
202        final Element elem = XMLConverter.convertW3CDocumentJDOMElement(rep
203                .readEntity(Document.class));
204        final List<Deployment> list = XMLConverter
205                .convertJDOMElementToList(elem, new DeploymentFactory(false));
206        Assert.assertEquals(0, list.size());
207    }
208
209    @Test
210    public void testGetAllTemplatesOne() throws CacheException, ParseException,
211            MissingPolicyException, DeployerException {
212        this.createAppWithTemplates();
213        this.app.putDeployment(new DeploymentBuilder().withUuid(this.depID).build());
214
215        final Response rep = this.target(AppsProvider.PATH).path(this.id)
216                .path(DeploymentsProvider.PATH).request().get();
217
218        Assert.assertEquals(Response.Status.OK.getStatusCode(), rep.getStatus());
219
220        final Element elem = XMLConverter.convertW3CDocumentJDOMElement(rep
221                .readEntity(Document.class));
222        final List<Deployment> list = XMLConverter
223                .convertJDOMElementToList(elem, new DeploymentFactory(false));
224        Assert.assertEquals(1, list.size());
225        Assert.assertEquals(this.depID, list.get(0).getUniqueIdentifier());
226    }
227
228    @Test
229    public void testGetAllTemplatesMultiple() throws CacheException, ParseException,
230            MissingPolicyException, DeployerException {
231        this.createAppWithTemplates();
232        final int numDeps = 8;
233        for (int i = 0; i < numDeps; i++) {
234            this.app.putDeployment(new DeploymentBuilder().withUuid(this.depID + i).build());
235        }
236
237        final Response rep = this.target(AppsProvider.PATH).path(this.id)
238                .path(DeploymentsProvider.PATH).request().get();
239
240        Assert.assertEquals(Response.Status.OK.getStatusCode(), rep.getStatus());
241
242        final Element elem = XMLConverter.convertW3CDocumentJDOMElement(rep
243                .readEntity(Document.class));
244        final List<Deployment> list = XMLConverter
245                .convertJDOMElementToList(elem, new DeploymentFactory(false));
246        Assert.assertEquals(numDeps, list.size());
247    }
248
249    @Test
250    public void testPostDeploymentResponse() throws CacheException, ParseException, JDOMException,
251            MissingPolicyException, DeployerException {
252        this.createAppWithTemplates();
253        final Map<String, Integer> map = new HashMap<String, Integer>();
254        map.put(this.templateID1, this.num1);
255        map.put(this.templateID2, this.num2);
256
257        final Entity<Document> entity = Entity.entity(this.getPostRootBody(map),
258                                                      MediaType.APPLICATION_XML_TYPE);
259
260        final Response rep = this.target(AppsProvider.PATH).path(this.id)
261                .path(DeploymentsProvider.PATH).request().post(entity);
262
263        Assert.assertEquals(Response.Status.CREATED.getStatusCode(), rep.getStatus());
264        Assert.assertNotNull(rep.getHeaderString(AppsProvider.CONTENT_LOCATION_HEADER));
265    }
266
267    @Test
268    public void testPostDeploymentInternalInfo() throws CacheException, ParseException,
269            JDOMException, MissingPolicyException, DeployerException {
270        this.createAppWithTemplates();
271        final Map<String, Integer> map = new HashMap<String, Integer>();
272        map.put(this.templateID1, this.num1);
273        map.put(this.templateID2, this.num2);
274
275        final Entity<Document> entity = Entity.entity(this.getPostRootBody(map),
276                                                      MediaType.APPLICATION_XML_TYPE);
277
278        final Response rep = this.target(AppsProvider.PATH).path(this.id)
279                .path(DeploymentsProvider.PATH).request().post(entity);
280        Assert.assertEquals(this.mockedDeployment, this.app.getDeployment(this.depID));
281    }
282
283    @Test
284    public void testPostDeploymentCacheException() throws CacheException, ParseException,
285            MissingPolicyException, DeployerException, JDOMException {
286        this.createAppWithTemplatesCacheException();
287        final Map<String, Integer> map = new HashMap<String, Integer>();
288        map.put(this.templateID1, this.num1);
289        map.put(this.templateID2, this.num2);
290
291        final Entity<Document> entity = Entity.entity(this.getPostRootBody(map),
292                                                      MediaType.APPLICATION_XML_TYPE);
293
294        final Response rep = this.target(AppsProvider.PATH).path(this.id)
295                .path(DeploymentsProvider.PATH).request().post(entity);
296
297        Assert.assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rep.getStatus());
298    }
299
300    @Test
301    public void testPostDeploymentParseException() throws CacheException, ParseException,
302            MissingPolicyException, DeployerException, JDOMException {
303        this.createAppWithTemplates();
304
305        final Entity<Document> entity = Entity
306                .entity(XMLConverter.convertJDOMElementW3CDocument(new Element("hallo")),
307                        MediaType.APPLICATION_XML_TYPE);
308
309        final Response rep = this.target(AppsProvider.PATH).path(this.id)
310                .path(DeploymentsProvider.PATH).request().post(entity);
311
312        Assert.assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rep.getStatus());
313    }
314
315    @Test
316    public void testPostDeploymentDeployerException() throws CacheException, ParseException,
317            MissingPolicyException, DeployerException, JDOMException {
318        this.createAppWithTemplatesDeployerException();
319        final Map<String, Integer> map = new HashMap<String, Integer>();
320        map.put(this.templateID1, this.num1);
321        map.put(this.templateID2, this.num2);
322
323        final Entity<Document> entity = Entity.entity(this.getPostRootBody(map),
324                                                      MediaType.APPLICATION_XML_TYPE);
325
326        final Response rep = this.target(AppsProvider.PATH).path(this.id)
327                .path(DeploymentsProvider.PATH).request().post(entity);
328
329        Assert.assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rep.getStatus());
330    }
331
332    @Test
333    public void testGetNotExistingDeployment() throws CacheException, ParseException,
334            MissingPolicyException, DeployerException {
335        this.createAppWithTemplates();
336
337        final Response rep = this.target(AppsProvider.PATH).path(this.id)
338                .path(DeploymentsProvider.PATH).path("THISIDDOESNOTEXIST").request().get();
339
340        Assert.assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rep.getStatus());
341    }
342
343}