protected void createAndCheckoutBranch()
      throws MojoExecutionException, IOException, GitAPIException {
    if (Strings.isNullOrBlank(oldBranchName)) {
      // lets find the previous branch
      List<String> branches =
          new ArrayList<String>(RepositoryUtils.getBranches(git.getRepository()));
      int size = branches.size();
      if (size > 0) {
        String last = branches.get(size - 1);
        int idx = last.lastIndexOf('/');
        if (idx > 0) {
          oldBranchName = last.substring(idx + 1);
          getLog().info("Using previous branch: " + oldBranchName);
        }
      }
    }
    if (Strings.isNullOrBlank(oldBranchName)) {
      oldBranchName = "master";
      getLog().warn("Could not deduce the old branch so setting it to: " + oldBranchName);
    }
    checkoutBranch(git, oldBranchName);
    getLog().info("Creating branch " + branchName + " in " + getGitBuildPathDescription());

    createOrCheckoutBranch(git, branchName, remoteName);
    checkoutBranch(git, branchName);
  }
  protected void assertCreateVersion(String parrentVersion, String version) {
    dataStore.createVersion(parrentVersion, version);

    assertHasVersion(version);

    // we should now have a remote branch of this name too
    Collection<String> remoteBranches = RepositoryUtils.getBranches(remote.getRepository());
    System.out.println("Remote branches: " + remoteBranches);
    String remoteBranch = "refs/heads/" + version;
    assertTrue(
        "Should contain " + remoteBranch + " but has remote branches " + remoteBranches,
        remoteBranches.contains(remoteBranch));
  }
  @Test
  public void testDataStore() throws Exception {
    String defaultVersion = dataStore.getDefaultVersion();
    assertEquals("defaultVersion", "1.0", defaultVersion);

    // now lets import some data - using the old non-git file layout...
    String importPath = basedir + "/../fabric8-karaf/src/main/resources/distro/fabric/import";
    if (useOldImportFormat) {
      assertFolderExists(importPath);
      dataStore.importFromFileSystem(importPath);
      assertHasVersion(defaultVersion);
    } else {
      String prefix = importPath + "/fabric";
      String profileImport = prefix + "/configs/versions/1.0/profiles";
      assertFolderExists(profileImport);

      dataStore.importFromFileSystem(new File(profileImport), "fabric", "1.0", true);
      assertHasVersion(defaultVersion);
    }

    remote.checkout().setName("1.0").call();
    String importedProfile = "example-dozer";
    String profile = importedProfile;
    assertProfileExists(defaultVersion, profile);

    // assertFolderExists("Should have imported an mq/ReadMe.md file!",
    // getLocalGitFile("fabric/profiles/mq/ReadMe.md"));

    String version = "1.1";
    assertCreateVersion("1.0", version);

    assertProfileConfiguration(
        version, importedProfile, Constants.AGENT_PID, "attribute.parents", "feature-camel");
    assertProfileTextFileConfigurationContains(
        version, "example-camel-mq", "camel.xml", "http://camel.apache.org/schema/blueprint");

    List<String> fileNames = dataStore.getConfigurationFileNames(version, "example-camel-mq");
    assertNotNull("Should have some file names", fileNames);
    assertTrue("Should have some file names", fileNames.size() > 0);
    assertTrue("Should contain 'came", fileNames.size() > 0);
    assertCollectionContains("configurationFileNames", fileNames, "camel.xml");

    // lets test the profile attributes
    Map<String, String> profileAttributes =
        dataStore.getProfileAttributes(version, importedProfile);
    String parent = profileAttributes.get("parents");
    assertEquals(importedProfile + ".profileAttributes[parent]", "feature-camel", parent);

    System.out.println("Profile attributes: " + profileAttributes);
    String profileAttributeKey = "myKey";
    String expectedProfileAttributeValue = "myValue";
    dataStore.setProfileAttribute(
        version, importedProfile, profileAttributeKey, expectedProfileAttributeValue);
    profileAttributes = dataStore.getProfileAttributes(version, importedProfile);
    System.out.println("Profile attributes: " + profileAttributes);
    assertMapContains(
        "Profile attribute[" + profileAttributeKey + "]",
        profileAttributes,
        profileAttributeKey,
        expectedProfileAttributeValue);

    String hawtioRepoKey = "repository.hawtio";
    Map<String, String> hawtioAttrbutes =
        dataStore.getConfiguration(version, "hawtio", Constants.AGENT_PID);
    String currentHawtRepo = hawtioAttrbutes.get(hawtioRepoKey);
    System.out.println("Current repository.hawtio: " + currentHawtRepo);

    // now lets write via the hawtio API
    FabricGitFacade hawtio = new FabricGitFacade();
    hawtio.bindGitDataStoreForTesting(dataStore);
    hawtio.activateForTesting();
    String hawtioPropertyFile =
        "/fabric/profiles/"
            + dataStore.convertProfileIdToDirectory("hawtio")
            + "/"
            + Constants.AGENT_PID
            + ".properties";
    hawtio.write(
        version,
        hawtioPropertyFile,
        "My commit message",
        "me",
        "*****@*****.**",
        "# new file\n"
            + hawtioRepoKey
            + " = "
            + "mvn\\:io.hawt/hawtio-karaf/myNewVersion/xml/features"
            + "\n");
    hawtioAttrbutes = dataStore.getConfiguration(version, "hawtio", Constants.AGENT_PID);
    String actual = hawtioAttrbutes.get(hawtioRepoKey);
    assertEquals(
        "should have found the updated hawtio repo key",
        "mvn:io.hawt/hawtio-karaf/myNewVersion/xml/features",
        actual);

    // lets check that the file configurations recurses into folders
    Map<String, byte[]> tomcatFileConfigurations =
        dataStore.getFileConfigurations("1.0", "controller-tomcat");
    assertHasFileConfiguration(tomcatFileConfigurations, "tomcat/conf/server.xml.mvel");

    Collection<String> schemas =
        dataStore.listFiles("1.0", Arrays.asList("example-dozer"), "schemas");
    assertNotNull(schemas);
    assertContainerEquals(
        "schemas for example-dozer", Arrays.asList("invoice.xsd"), new ArrayList<String>(schemas));

    // check we don't accidentally create a profile
    String profileNotCreated = "shouldNotBeCreated";
    assertEquals(
        "Should not create profile: " + profileNotCreated,
        null,
        dataStore.getProfile(version, profileNotCreated, false));
    assertProfileNotExists(defaultVersion, profileNotCreated);
    assertFolderNotExists(
        getLocalGitFile(
            "fabric/profiles/" + dataStore.convertProfileIdToDirectory(profileNotCreated)));

    // now lets create some profiles in this new version
    String newProfile = "myNewProfile";
    dataStore.createProfile(version, newProfile);
    assertProfileExists(version, newProfile);

    // lazy create a profile
    String anotherNewProfile = "anotherNewProfile";
    dataStore.getProfile(version, anotherNewProfile, true);
    assertProfileExists(version, anotherNewProfile);

    version = "1.2";
    assertCreateVersion("1.1", version);

    // check this version has the profile too
    assertProfileExists(version, newProfile);
    assertProfileExists(version, profile);

    // now lets delete a profile
    dataStore.deleteProfile(version, newProfile);
    assertProfileNotExists(version, newProfile);

    // lets check the remote repo
    remote.checkout().setName("1.1").call();

    assertProfileExists("1.1", profile);
    assertProfileExists("1.1", newProfile);
    assertFolderExists(
        getRemoteGitFile("fabric/profiles/" + dataStore.convertProfileIdToDirectory(profile)));
    assertFolderExists(
        getRemoteGitFile("fabric/profiles/" + dataStore.convertProfileIdToDirectory(newProfile)));

    remote.checkout().setName("1.2").call();

    assertProfileExists("1.2", profile);
    assertProfileNotExists("1.2", newProfile);
    assertFolderExists(
        getRemoteGitFile("fabric/profiles/" + dataStore.convertProfileIdToDirectory(profile)));
    assertFolderNotExists(
        getRemoteGitFile("fabric/profiles/" + dataStore.convertProfileIdToDirectory(newProfile)));

    remote.checkout().setName("1.0").call();
    assertFolderExists(
        getRemoteGitFile("fabric/profiles/" + dataStore.convertProfileIdToDirectory(profile)));
    assertFolderNotExists(
        getRemoteGitFile("fabric/profiles/" + dataStore.convertProfileIdToDirectory(newProfile)));

    // delete version 1.2
    assertHasVersion("1.1");
    assertHasVersion("1.2");
    dataStore.removeVersion("1.2");
    assertHasVersion("1.1");
    assertHasNotVersion("1.2");

    Collection<String> remoteBranches = RepositoryUtils.getBranches(remote.getRepository());
    System.out.println("Remote branches after delete: " + remoteBranches);
  }