@Test
public void testExtractViewArchive() throws Exception {

  File viewDir = createNiceMock(File.class);
  File extractedArchiveDir = createNiceMock(File.class);
  File viewArchive = createNiceMock(File.class);
  File archiveDir = createNiceMock(File.class);
  File entryFile  = createNiceMock(File.class);
  File classesDir = createNiceMock(File.class);
  File libDir = createNiceMock(File.class);
  File metaInfDir = createNiceMock(File.class);
  File fileEntry = createNiceMock(File.class);
  JarInputStream viewJarFile = createNiceMock(JarInputStream.class);
  JarEntry jarEntry = createNiceMock(JarEntry.class);
  InputStream is = createMock(InputStream.class);
  FileOutputStream fos = createMock(FileOutputStream.class);
  ViewExtractor viewExtractor = createMock(ViewExtractor.class);
  ResourceTypeEntity resourceTypeEntity = new ResourceTypeEntity();
  resourceTypeEntity.setId(10);
  resourceTypeEntity.setName("MY_VIEW{1.0.0}");
  ViewEntity viewDefinition = ViewEntityTest.getViewEntity();
  viewDefinition.setResourceType(resourceTypeEntity);
  Set<ViewInstanceEntity> viewInstanceEntities = ViewInstanceEntityTest.getViewInstanceEntities(viewDefinition);
  viewDefinition.setInstances(viewInstanceEntities);
  Map<File, ViewConfig> viewConfigs =
      Collections.singletonMap(viewArchive, viewDefinition.getConfiguration());
  long resourceId = 99L;
  for (ViewInstanceEntity viewInstanceEntity : viewInstanceEntities) {
    ResourceEntity resourceEntity = new ResourceEntity();
    resourceEntity.setId(resourceId);
    resourceEntity.setResourceType(resourceTypeEntity);
    viewInstanceEntity.setResource(resourceEntity);
  }
  Map<String, File> files = new HashMap<String, File>();
  if (System.getProperty("os.name").contains("Windows")) {
    files.put("\\var\\lib\\ambari-server\\resources\\views\\my_view-1.0.0.jar", viewArchive);
    files.put("\\var\\lib\\ambari-server\\resources\\views\\work", extractedArchiveDir);
    files.put("\\var\\lib\\ambari-server\\resources\\views\\work\\MY_VIEW{1.0.0}", archiveDir);
    files.put("\\var\\lib\\ambari-server\\resources\\views\\work\\MY_VIEW{1.0.0}\\view.xml", entryFile);
    files.put("\\var\\lib\\ambari-server\\resources\\views\\work\\MY_VIEW{1.0.0}\\WEB-INF\\classes", classesDir);
    files.put("\\var\\lib\\ambari-server\\resources\\views\\work\\MY_VIEW{1.0.0}\\WEB-INF\\lib", libDir);
    files.put("\\var\\lib\\ambari-server\\resources\\views\\work\\MY_VIEW{1.0.0}\\META-INF", metaInfDir);
  }
  else {
    files.put("/var/lib/ambari-server/resources/views/my_view-1.0.0.jar", viewArchive);
    files.put("/var/lib/ambari-server/resources/views/work", extractedArchiveDir);
    files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}", archiveDir);
    files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/view.xml", entryFile);
    files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/WEB-INF/classes", classesDir);
    files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/WEB-INF/lib", libDir);
    files.put("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}/META-INF", metaInfDir);
  }
  Map<File, FileOutputStream> outputStreams = new HashMap<File, FileOutputStream>();
  outputStreams.put(entryFile, fos);
  Map<File, JarInputStream> jarFiles = new HashMap<File, JarInputStream>();
  jarFiles.put(viewArchive, viewJarFile);
  // set expectations
  expect(configuration.getViewsDir()).andReturn(viewDir);
  if (System.getProperty("os.name").contains("Windows")) {
    expect(viewDir.getAbsolutePath()).andReturn("\\var\\lib\\ambari-server\\resources\\views");
  }
  else {
    expect(viewDir.getAbsolutePath()).andReturn("/var/lib/ambari-server/resources/views");
  }
  expect(configuration.getViewExtractionThreadPoolCoreSize()).andReturn(2).anyTimes();
  expect(configuration.getViewExtractionThreadPoolMaxSize()).andReturn(3).anyTimes();
  expect(configuration.getViewExtractionThreadPoolTimeout()).andReturn(10000L).anyTimes();
  if (System.getProperty("os.name").contains("Windows")) {
    expect(viewArchive.getAbsolutePath()).andReturn("\\var\\lib\\ambari-server\\resources\\views\\work\\MY_VIEW{1.0.0}").anyTimes();
  }
  else {
    expect(viewArchive.getAbsolutePath()).andReturn("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}").anyTimes();
  }
  expect(archiveDir.exists()).andReturn(false);
  if (System.getProperty("os.name").contains("Windows")) {
    expect(archiveDir.getAbsolutePath()).andReturn("\\var\\lib\\ambari-server\\resources\\views\\work\\MY_VIEW{1.0.0}").anyTimes();
  }
  else {
    expect(archiveDir.getAbsolutePath()).andReturn("/var/lib/ambari-server/resources/views/work/MY_VIEW{1.0.0}").anyTimes();
  }
  Capture<ViewEntity> viewEntityCapture = EasyMock.newCapture();
  if (System.getProperty("os.name").contains("Windows")) {
    expect(viewExtractor.ensureExtractedArchiveDirectory("\\var\\lib\\ambari-server\\resources\\views\\work")).andReturn(true);
  }
  else {
    expect(viewExtractor.ensureExtractedArchiveDirectory("/var/lib/ambari-server/resources/views/work")).andReturn(true);
  }
  expect(viewExtractor.extractViewArchive(capture(viewEntityCapture), eq(viewArchive), eq(archiveDir))).andReturn(null);
  // replay mocks
  replay(configuration, viewDir, extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir,
      libDir, metaInfDir, fileEntry, viewJarFile, jarEntry, is, fos, viewExtractor, resourceDAO, viewDAO, viewInstanceDAO);
  TestViewArchiveUtility archiveUtility = new TestViewArchiveUtility(viewConfigs, files, outputStreams, jarFiles, false);
  TestViewModule module = new TestViewModule(viewExtractor, archiveUtility, configuration);
  if (System.getProperty("os.name").contains("Windows")) {
    Assert.assertTrue(ViewRegistry.extractViewArchive("\\var\\lib\\ambari-server\\resources\\views\\my_view-1.0.0.jar",
        module, true));
  }
  else {
    Assert.assertTrue(ViewRegistry.extractViewArchive("/var/lib/ambari-server/resources/views/my_view-1.0.0.jar",
        module, true));
  }
  // verify mocks
  verify(configuration, viewDir, extractedArchiveDir, viewArchive, archiveDir, entryFile, classesDir,
      libDir, metaInfDir, fileEntry, viewJarFile, jarEntry, is, fos, viewExtractor, resourceDAO, viewDAO, viewInstanceDAO);
}