/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

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