/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.camunda.bpm.container.impl.jmx.deployment.util;

import org.camunda.bpm.container.impl.jmx.deployment.scanning.ProcessApplicationScanningUtil;
import org.junit.Test;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Map;

import static org.junit.Assert.*;


/**
 * @author Clint Manning
 */
public class VfsProcessApplicationScannerTest {

  @Test
  public void testScanProcessArchivePathForResources() throws MalformedURLException {

    // given: scanning the relative test resource root
    URLClassLoader classLoader = new URLClassLoader(new URL[]{new URL("file:")});
    String processRootPath = "classpath:org/camunda/bpm/container/impl/jmx/deployment/process/";
    Map<String, byte[]> scanResult = ProcessApplicationScanningUtil.findResources(classLoader, processRootPath, null);

    // expect: finds only the BPMN process file and not treats the 'bpmn' folder
    assertEquals(1, scanResult.size());
    String processFileName = "VfsProcessScannerTest.bpmn20.xml";
    assertTrue("'" + processFileName + "'not found", contains(scanResult, processFileName));
    assertFalse("'bpmn' folder in resource path found", contains(scanResult, "bpmn"));
  }

  @Test
  public void testScanProcessArchivePathForCmmnResources() throws MalformedURLException {

    // given: scanning the relative test resource root
    URLClassLoader classLoader = new URLClassLoader(new URL[]{new URL("file:")});
    String processRootPath = "classpath:org/camunda/bpm/container/impl/jmx/deployment/case/";
    Map<String, byte[]> scanResult = ProcessApplicationScanningUtil.findResources(classLoader, processRootPath, null);

    // expect: finds only the CMMN process file and not treats the 'cmmn' folder
    assertEquals(1, scanResult.size());
    String processFileName = "VfsProcessScannerTest.cmmn";
    assertTrue("'" + processFileName + "' not found", contains(scanResult, processFileName));
    assertFalse("'cmmn' in resource path found", contains(scanResult, "cmmn"));
  }

  private boolean contains(Map<String, byte[]> scanResult, String prefix) {
    for (String string : scanResult.keySet()) {
      if (string.startsWith(prefix)) {
        return true;
      }
    }
    return false;
  }
}

@@ -1771,20 +1771,35 @@
 esult, %22
-bpmn
+processResource.txt
 %22));%0A  %7D
@@ -2596,19 +2596,31 @@
 sult, %22c
-mmn
+aseResource.txt
 %22));%0A  %7D
@@ -2687,19 +2687,19 @@
  String 
-pre
+suf
 fix) %7B%0A 
@@ -2766,22 +2766,20 @@
 ing.
-start
+end
 sWith(
-pre
+suf
 fix)

revert changes to fix db upgrade test