This is an automated email from the ASF dual-hosted git repository. mradhakrishnan pushed a commit to branch AMBARI-24711 in repository https://gitbox.apache.org/repos/asf/ambari.git
commit cf2b52a10f0988602cfc331ee5bc2a3f4fab1593 Author: Madhuvanthi Radhakrishnan <[email protected]> AuthorDate: Wed Jun 21 10:57:53 2017 -0700 AMBARI-21231: Download and extract mpacks. Add unit tests (mradhakrishnan) --- .../ambari/server/controller/MpackRequest.java | 84 +++++- .../ambari/server/controller/MpackResponse.java | 18 +- .../AbstractControllerResourceProvider.java | 2 + .../controller/internal/MpackResourceProvider.java | 22 +- .../apache/ambari/server/mpack/MpackManager.java | 201 ++++++++++----- .../org/apache/ambari/server/orm/dao/MpackDAO.java | 2 +- .../ambari/server/orm/entities/MpackEntity.java | 16 +- .../org/apache/ambari/server/state/Mpacks.java | 139 +++++++++- .../org/apache/ambari/server/state/Packlet.java | 75 ++++++ .../api/resources/MpackResourceDefinitionTest.java | 35 +++ .../server/api/services/AmbariMetaInfoTest.java | 71 ++++-- .../server/api/services/MpackServiceTest.java | 94 +++++++ .../server/configuration/ConfigurationTest.java | 10 + .../AmbariManagementControllerImplTest.java | 70 ++++- .../ambari/server/controller/MpackRequestTest.java | 44 ++++ .../server/controller/MpackResponseTest.java | 54 ++++ .../internal/MpackResourceProviderTest.java | 281 +++++++++++++++++++++ .../apache/ambari/server/orm/dao/MpackDAOTest.java | 76 ++++++ .../server/orm/entities/MpackEntityTest.java | 68 +++++ .../org/apache/ambari/server/state/MpacksTest.java | 95 +++++++ 20 files changed, 1335 insertions(+), 122 deletions(-) diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/MpackRequest.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/MpackRequest.java index fa6dd1d..a705d18 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/MpackRequest.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/MpackRequest.java @@ -26,24 +26,24 @@ import org.apache.ambari.server.orm.entities.MpackEntity; */ public class MpackRequest { - private String mpackId; + private Long mpackId; private String mpackName; private String mpackVersion; - private String mpackUrl; - private String registryId; + private String mpackUri; + private Long registryId; - public MpackRequest(String mpackId) { + public MpackRequest(Long mpackId) { this.setMpackId(mpackId); } public MpackRequest() { } - public String getMpackId() { + public Long getMpackId() { return mpackId; } - public void setMpackId(String mpackId) { + public void setMpackId(Long mpackId) { this.mpackId = mpackId; } @@ -63,20 +63,80 @@ public class MpackRequest { this.mpackVersion = mpackVersion; } - public String getMpackUrl() { - return mpackUrl; + public String getMpackUri() { + return mpackUri; } - public void setMpackUrl(String mpackUrl) { - this.mpackUrl = mpackUrl; + public void setMpackUri(String mpackUri) { + this.mpackUri = mpackUri; } - public String getRegistryId() { + public Long getRegistryId() { return registryId; } - public void setRegistryId(String registryId) { + public void setRegistryId(Long registryId) { this.registryId = registryId; } + @Override + public int hashCode() { + int result = 1; + result = 31 + getMpackId().hashCode(); + return result; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof MpackRequest)) { + return false; + } + if (this == obj) { + return true; + } + MpackRequest mpackRequest = (MpackRequest) obj; + + if (mpackId == null) { + if (mpackRequest.mpackId != null) { + return false; + } + } else if (!mpackId.equals(mpackRequest.mpackId)) { + return false; + } + + if (mpackName == null) { + if (mpackRequest.mpackName != null) { + return false; + } + } else if (!mpackName.equals(mpackRequest.mpackName)) { + return false; + } + + if (mpackUri == null) { + if (mpackRequest.mpackUri != null) { + return false; + } + } else if (!mpackUri.equals(mpackRequest.mpackUri)) { + return false; + } + + if (registryId == null) { + if (mpackRequest.registryId != null) { + return false; + } + } else if (!registryId.equals(mpackRequest.registryId)) { + return false; + } + + if (mpackVersion == null) { + if (mpackRequest.mpackVersion != null) { + return false; + } + } else if (!mpackVersion.equals(mpackRequest.mpackVersion)) { + return false; + } + + return true; + } + } diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/MpackResponse.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/MpackResponse.java index d7cca79..05687c2 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/MpackResponse.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/MpackResponse.java @@ -27,13 +27,13 @@ public class MpackResponse { private Long mpackId; private String mpackVersion; private String mpackName; - private String mpackUrl; - private String registryId; + private String mpackUri; + private Long registryId; public MpackResponse(Mpacks mpacks) { this.mpackId = mpacks.getMpackId(); this.mpackVersion = mpacks.getVersion(); - this.mpackUrl = mpacks.getMpacksUrl(); + this.mpackUri = mpacks.getMpacksUri(); this.mpackName = mpacks.getName(); this.registryId = mpacks.getRegistryId(); } @@ -42,8 +42,8 @@ public class MpackResponse { return mpackVersion; } - public String getMpackUrl() { - return mpackUrl; + public String getMpackUri() { + return mpackUri; } public Long getMpackId() { @@ -54,7 +54,7 @@ public class MpackResponse { return mpackName; } - public String getRegistryId() { + public Long getRegistryId() { return registryId; } @@ -66,11 +66,11 @@ public class MpackResponse { this.mpackName = mpackName; } - public void setMpackUrl(String mpackUrl) { - this.mpackUrl = mpackUrl; + public void setMpackUri(String mpackUri) { + this.mpackUri = mpackUri; } - public void setRegistryId(String registryId) { + public void setRegistryId(Long registryId) { this.registryId = registryId; } diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractControllerResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractControllerResourceProvider.java index 15006b0..06dbc7b 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractControllerResourceProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractControllerResourceProvider.java @@ -163,6 +163,8 @@ public abstract class AbstractControllerResourceProvider extends AbstractAuthori return resourceProviderFactory.getUpgradeResourceProvider(managementController); case Stack: return new StackResourceProvider(managementController); + case Mpack: + return new MpackResourceProvider(managementController); case StackVersion: return new StackVersionResourceProvider(managementController); case ClusterStackVersion: diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/MpackResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/MpackResourceProvider.java index 7b4682a..65eaeb6 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/MpackResourceProvider.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/MpackResourceProvider.java @@ -61,7 +61,7 @@ public class MpackResourceProvider extends AbstractControllerResourceProvider { public static final String REGISTRY_ID = "MpackInfo/registry_id"; public static final String MPACK_NAME = "MpackInfo/mpack_name"; public static final String MPACK_VERSION = "MpackInfo/mpack_version"; - public static final String MPACK_URL = "MpackInfo/mpack_url"; + public static final String MPACK_URI = "MpackInfo/mpack_uri"; public static final String PACKLETS = "MpackInfo/packlets"; @@ -87,7 +87,7 @@ public class MpackResourceProvider extends AbstractControllerResourceProvider { PROPERTY_IDS.add(REGISTRY_ID); PROPERTY_IDS.add(MPACK_NAME); PROPERTY_IDS.add(MPACK_VERSION); - PROPERTY_IDS.add(MPACK_URL); + PROPERTY_IDS.add(MPACK_URI); PROPERTY_IDS.add(PACKLETS); // keys @@ -112,7 +112,7 @@ public class MpackResourceProvider extends AbstractControllerResourceProvider { try { MpackRequest mpackRequest = getRequest(request); if (mpackRequest == null) - throw new BodyParseException("Please provide " + MPACK_NAME + " ," + MPACK_VERSION + " ," + MPACK_URL); + throw new BodyParseException("Please provide " + MPACK_NAME + " ," + MPACK_VERSION + " ," + MPACK_URI); MpackResponse response = getManagementController().registerMpack(mpackRequest); if (response != null) { notifyCreate(Resource.Type.Mpack, request); @@ -121,7 +121,7 @@ public class MpackResourceProvider extends AbstractControllerResourceProvider { resource.setProperty(REGISTRY_ID, response.getRegistryId()); resource.setProperty(MPACK_NAME, response.getMpackName()); resource.setProperty(MPACK_VERSION, response.getMpackVersion()); - resource.setProperty(MPACK_URL, response.getMpackUrl()); + resource.setProperty(MPACK_URI, response.getMpackUri()); associatedResources.add(resource); return getRequestStatus(null, associatedResources); @@ -139,17 +139,17 @@ public class MpackResourceProvider extends AbstractControllerResourceProvider { Set<Map<String, Object>> properties = request.getProperties(); for (Map propertyMap : properties) { //Mpack Download url is either given in the request body or is fetched using the registry id - if (!propertyMap.containsKey(MPACK_URL) && !propertyMap.containsKey(REGISTRY_ID)) + if (!propertyMap.containsKey(MPACK_URI) && !propertyMap.containsKey(REGISTRY_ID)) return null; //Fetch Mpack Download Url using the given registry id - else if (!propertyMap.containsKey(MPACK_URL)) { - mpackRequest.setRegistryId((String) propertyMap.get(REGISTRY_ID)); + else if (!propertyMap.containsKey(MPACK_URI)) { + mpackRequest.setRegistryId((Long) propertyMap.get(REGISTRY_ID)); mpackRequest.setMpackName((String) propertyMap.get(MPACK_NAME)); mpackRequest.setMpackVersion((String) propertyMap.get(MPACK_VERSION)); } - //Directle download the mpack using the given url + //Directly download the mpack using the given url else - mpackRequest.setMpackUrl((String) propertyMap.get(MPACK_URL)); + mpackRequest.setMpackUri((String) propertyMap.get(MPACK_URI)); } return mpackRequest; @@ -173,7 +173,7 @@ public class MpackResourceProvider extends AbstractControllerResourceProvider { resource.setProperty(MPACK_ID, entity.getMpackId()); resource.setProperty(MPACK_NAME, entity.getMpackName()); resource.setProperty(MPACK_VERSION, entity.getMpackVersion()); - resource.setProperty(MPACK_URL, entity.getMpackUrl()); + resource.setProperty(MPACK_URI, entity.getMpackUri()); resource.setProperty(REGISTRY_ID, entity.getRegistryId()); results.add(resource); } @@ -188,7 +188,7 @@ public class MpackResourceProvider extends AbstractControllerResourceProvider { resource.setProperty(MPACK_ID, entity.getMpackId()); resource.setProperty(MPACK_NAME, entity.getMpackName()); resource.setProperty(MPACK_VERSION, entity.getMpackVersion()); - resource.setProperty(MPACK_URL, entity.getMpackUrl()); + resource.setProperty(MPACK_URI, entity.getMpackUri()); resource.setProperty(REGISTRY_ID, entity.getRegistryId()); ArrayList<Packlet> packletArrayList = getManagementController().getPacklets(entity.getMpackId()); diff --git a/ambari-server/src/main/java/org/apache/ambari/server/mpack/MpackManager.java b/ambari-server/src/main/java/org/apache/ambari/server/mpack/MpackManager.java index dd4a4b3..a031af3 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/mpack/MpackManager.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/mpack/MpackManager.java @@ -18,8 +18,6 @@ package org.apache.ambari.server.mpack; import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; -import com.google.gson.stream.JsonReader; import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.AssistedInject; import org.apache.ambari.server.controller.MpackRequest; @@ -29,19 +27,28 @@ import org.apache.ambari.server.orm.dao.MpackDAO; import org.apache.ambari.server.orm.entities.MpackEntity; import org.apache.ambari.server.state.Mpacks; import org.apache.ambari.server.state.Packlet; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; +import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.lang.reflect.Type; -import java.util.ArrayList; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.nio.file.Paths; +import java.util.Map; import java.util.HashMap; +import java.util.ArrayList; import java.util.List; -import java.util.Map; -import java.util.regex.Pattern; -import java.util.regex.Matcher; +import java.io.File; +import java.io.OutputStream; +import java.io.FileInputStream; +import java.io.BufferedInputStream; +import java.io.FileOutputStream; +import java.io.IOException; /** @@ -52,6 +59,7 @@ public class MpackManager { protected Map<Long, Mpacks> mpackMap = new HashMap<>(); private File mpackStaging; private MpackDAO mpackDAO; + private Mpacks mpack; private final static Logger LOG = LoggerFactory.getLogger(MpackManager.class); @@ -73,56 +81,151 @@ public class MpackManager { * @throws ResourceAlreadyExistsException */ public MpackResponse registerMpack(MpackRequest mpackRequest) throws IOException, IllegalArgumentException, ResourceAlreadyExistsException { - //Todo : Madhu Expand the folders //Todo : Madhu Update StacksAPI + Long mpackId; - Mpacks mpacks = new Mpacks(); + mpack = new Mpacks(); String mpackName = ""; String mpackVersion = ""; boolean isValidMetadata = true; + + //Mpack registration using a software registry if (mpackRequest.getRegistryId() != null) { mpackName = mpackRequest.getMpackName(); mpackVersion = mpackRequest.getMpackVersion(); - mpacks = parseMpacksJson(mpackName, mpackVersion); - mpacks.setRegistryId(mpackRequest.getRegistryId()); + mpack.setRegistryId(mpackRequest.getRegistryId()); //Validate the Post request - isValidMetadata = validateMpackInfo(mpackName, mpackVersion, mpacks.getName(), mpacks.getVersion()); + isValidMetadata = validateMpackInfo(mpackName, mpackVersion, mpack.getName(), mpack.getVersion()); + //Todo : Madhu implement GET /registries/{registryId}/mpacks } else { - //Download the mpack and return the path. - String[] urlSplit = mpackRequest.getMpackUrl().split("/"); - String mpackNameVersion = urlSplit[urlSplit.length - 1]; - final String patternStrName = "([a-zA-Z]+)-([a-zA-Z]+)-([a-zA-Z]+)"; - final String patternStrVersion = "([0-9]).([0-9]).([0-9]).([0-9])-([0-9]+)"; - Pattern REGEX = Pattern.compile(patternStrName); - Matcher patternMatchObj = REGEX.matcher(mpackNameVersion); - if (patternMatchObj.find()) { - mpackName = patternMatchObj.group(); - } - REGEX = Pattern.compile(patternStrVersion); - patternMatchObj = REGEX.matcher(mpackNameVersion); - if (patternMatchObj.find()) { - mpackVersion = patternMatchObj.group(); - } - mpacks = parseMpacksJson(mpackName, mpackVersion); - mpacks.setMpacksUrl(mpackRequest.getMpackUrl()); + //Mpack registration using direct download + mpack.setMpacksUri(mpackRequest.getMpackUri()); + } + + //Download the mpack and return the path. + Path mpackTarPath = downloadMpack(mpackRequest.getMpackUri(), mpackStaging + File.separator); + //create a directory as mpack-staging-path/mpack-name/mpack-version + if (createMpackDirectory(mpackTarPath)) { + //expand the mpack.tar.gz file inside the directory created above + String mpackDirectory = mpackStaging + File.separator + mpack.getName() + File.separator + mpack.getVersion(); + extractMpackTar(mpackTarPath, mpackDirectory); } if (isValidMetadata) { - mpackId = populateDB(mpacks); + mpackId = populateDB(mpack); if (mpackId != null) { - mpackMap.put(mpackId, mpacks); - mpacks.setMpackId(mpackId); - return new MpackResponse(mpacks); + mpackMap.put(mpackId, mpack); + mpack.setMpackId(mpackId); + return new MpackResponse(mpack); } else { String message = "Mpack :" + mpackRequest.getMpackName() + " version: " + mpackRequest.getMpackVersion() + " already exists in server"; throw new ResourceAlreadyExistsException(message); } } else { - String message = "Incorrect information : Mismatch in - (" + mpackName + "," + mpacks.getName() + ") or (" + mpackVersion + "," + mpacks.getVersion() + ")"; + String message = "Incorrect information : Mismatch in - (" + mpackName + "," + mpack.getName() + ") or (" + mpackVersion + "," + mpack.getVersion() + ")"; throw new IllegalArgumentException(message); //Mismatch in information } } /** + * Mpack is downloaded as a tar.gz file. It is extracted into mpack-v2-staging/{mpack-name}/{mpack-version}/ directory + * + * @param mpackTarPath + * @param mpackDirectory + * @throws IOException + */ + private void extractMpackTar(Path mpackTarPath, String mpackDirectory) throws IOException { + TarArchiveInputStream mpackTarFile = new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(new File(String.valueOf(mpackTarPath)))))); + // To read individual TAR file + TarArchiveEntry entry = null; + File outputFile = null; + //Create a loop to read every single entry in TAR file + while ((entry = mpackTarFile.getNextTarEntry()) != null) { + outputFile = new File(mpackStaging, entry.getName()); + if (entry.isDirectory()) { + LOG.debug("Attempting to write output directory" + outputFile.getAbsolutePath()); + if (!outputFile.exists()) { + LOG.debug("Attempting to create output directory " + outputFile.getAbsolutePath()); + if (!outputFile.mkdirs()) { + throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath())); + } + } + } else { + LOG.debug("Creating output file %s." + outputFile.getAbsolutePath()); + final OutputStream outputFileStream = new FileOutputStream(outputFile); + IOUtils.copy(mpackTarFile, outputFileStream); + outputFileStream.close(); + } + } + mpackTarFile.close(); + String mpackTarDirectory = mpackTarPath.toString(); + Path extractedMpackDirectory = Files.move + (Paths.get(mpackStaging + File.separator + mpackTarDirectory.substring(mpackTarDirectory.lastIndexOf('/') + 1, mpackTarDirectory.indexOf(".tar")) + File.separator), + Paths.get(mpackDirectory), StandardCopyOption.REPLACE_EXISTING); + } + + /** + * Reads the mpack.json file within the {mpack-name}.tar.gz file and populates Mpack object. + * Extract the mpack-name and mpack-version from mpack.json to create the new mpack directory to hold the mpack files. + * + * @param mpackTarPath + * @return boolean + * @throws IOException + */ + private Boolean createMpackDirectory(Path mpackTarPath) throws IOException { + TarArchiveInputStream mpackTarFile = new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(new File(mpackTarPath.toString()))))); + // To read individual TAR file + TarArchiveEntry entry = null; + String individualFiles; + int offset; + // Create a loop to read every single entry in TAR file + while ((entry = mpackTarFile.getNextTarEntry()) != null) { + // Get the name of the file + individualFiles = entry.getName(); + String[] dirFile = individualFiles.split(File.separator); + //Search for mpack.json + String fileName = dirFile[dirFile.length - 1]; + if (fileName.contains("mpack") && fileName.contains(".json")) { + // Get Size of the file and create a byte array for the size + byte[] content = new byte[(int) entry.getSize()]; + offset = 0; + LOG.info("File Name in TAR File is: " + fileName); + LOG.debug("Size of the File is: " + entry.getSize()); + // Read file from the archive into byte array + mpackTarFile.read(content, offset, content.length - offset); + + //Read the mpack.json file into Mpack Object for further use. + String mpackJsonContents = new String(content, "UTF-8"); + Gson gson = new Gson(); + mpack = gson.fromJson(mpackJsonContents, Mpacks.class); + + mpackTarFile.close(); + + return new File(mpackStaging + File.separator + mpack.getName()).mkdir(); + + } + } + + return false; + } + + + /*** + * Download the mpack from the given uri + * @param sourceURI + * @param targetDirectory + * @return + */ + public static Path downloadMpack(String sourceURI, String targetDirectory) throws IOException { + URL url = new URL(sourceURI); + String fileName = sourceURI.substring(sourceURI.lastIndexOf('/') + 1, sourceURI.length()); + Path targetPath = new File(targetDirectory + File.separator + fileName).toPath(); + Files.copy(url.openStream(), targetPath, StandardCopyOption.REPLACE_EXISTING); + + return targetPath; + } + + + /** * Compares if the user's mpack information matches the downloaded mpack information. * * @param expectedMpackName @@ -140,24 +243,6 @@ public class MpackManager { } /** - * Parses the mpack.json file for mpack information and stores in memory for powering /mpacks/{mpack_id} - * - * @return Mpacks - * @throws IOException - * @param mpackName - * @param mpackVersion - */ - protected Mpacks parseMpacksJson(String mpackName, String mpackVersion) throws IOException { - Type type = new TypeToken<Mpacks>() { - }.getType(); - Gson gson = new Gson(); - String mpackJson = mpackName + "-" + mpackVersion + ".json"; - JsonReader jsonReader = new JsonReader(new FileReader(mpackStaging + "/" + mpackName +"/" + mpackVersion + "/" + mpackJson)); - Mpacks mpacks = gson.fromJson(jsonReader, type); - return mpacks; - } - - /** * Make an entry in the mpacks database for the newly registered mpack. * * @param mpacks @@ -173,7 +258,7 @@ public class MpackManager { MpackEntity mpackEntity = new MpackEntity(); mpackEntity.setMpackName(mpackName); mpackEntity.setMpackVersion(mpackVersion); - mpackEntity.setMpackUrl(mpacks.getMpacksUrl()); + mpackEntity.setMpackUri(mpacks.getMpacksUri()); Long mpackId = mpackDAO.create(mpackEntity); return mpackId; @@ -184,10 +269,12 @@ public class MpackManager { /** * Fetches the packlet info stored in the memory for mpacks/{mpack_id} call. + * @param mpackId + * @return ArrayList */ public ArrayList<Packlet> getPacklets(Long mpackId) { Mpacks mpack = mpackMap.get(mpackId); - if(mpack.getPacklets()!=null) + if (mpack.getPacklets() != null) return mpack.getPacklets(); return null; } diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/MpackDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/MpackDAO.java index e41edd2..a19ceaf 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/MpackDAO.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/MpackDAO.java @@ -39,7 +39,7 @@ public class MpackDAO { * JPA entity manager */ @Inject - private Provider<EntityManager> m_entityManagerProvider; + Provider<EntityManager> m_entityManagerProvider; /** * DAO utilities for dealing mostly with {@link TypedQuery} results. diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/MpackEntity.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/MpackEntity.java index addba17..ed5aa6e 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/MpackEntity.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/MpackEntity.java @@ -61,7 +61,7 @@ public class MpackEntity { private String mpackVersion; @Column(name = "mpack_uri", nullable = false) - private String mpackUrl; + private String mpackUri; public Long getMpackId() { return mpackId; @@ -79,8 +79,8 @@ public class MpackEntity { return mpackVersion; } - public String getMpackUrl() { - return mpackUrl; + public String getMpackUri() { + return mpackUri; } public void setMpackId(Long mpackId) { @@ -99,8 +99,8 @@ public class MpackEntity { this.mpackVersion = mpackVersion; } - public void setMpackUrl(String mpackUrl) { - this.mpackUrl = mpackUrl; + public void setMpackUri(String mpackUri) { + this.mpackUri = mpackUri; } public MpackEntity() { @@ -121,6 +121,8 @@ public class MpackEntity { EqualsBuilder equalsBuilder = new EqualsBuilder(); equalsBuilder.append(mpackId, that.mpackId); + equalsBuilder.append(mpackName, that.mpackName); + equalsBuilder.append(mpackVersion, that.mpackVersion); return equalsBuilder.isEquals(); } @@ -135,7 +137,7 @@ public class MpackEntity { */ @Override public int hashCode() { - return Objects.hash(mpackId); + return Objects.hash(mpackId, mpackName, mpackVersion); } /** @@ -150,7 +152,7 @@ public class MpackEntity { } buffer.append(", mpackName=").append(mpackName); buffer.append(", mpackVersion=").append(mpackVersion); - buffer.append(", mpackUrl=").append(mpackUrl); + buffer.append(", mpackUri=").append(mpackUri); buffer.append("}"); return buffer.toString(); } diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/Mpacks.java b/ambari-server/src/main/java/org/apache/ambari/server/state/Mpacks.java index 7abac7e..9a457b0 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/state/Mpacks.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/state/Mpacks.java @@ -29,7 +29,7 @@ public class Mpacks { private Long mpackId; - private String registryId; + private Long registryId; @SerializedName("name") private String name; @@ -46,7 +46,7 @@ public class Mpacks { @SerializedName("packlets") private ArrayList<Packlet> packlets; - private String mpacksUrl; + private String mpacksUri; public Long getMpackId() { return mpackId; @@ -56,20 +56,20 @@ public class Mpacks { this.mpackId = mpackId; } - public String getRegistryId() { + public Long getRegistryId() { return registryId; } - public void setRegistryId(String registryId) { + public void setRegistryId(Long registryId) { this.registryId = registryId; } - public String getMpacksUrl() { - return mpacksUrl; + public String getMpacksUri() { + return mpacksUri; } - public void setMpacksUrl(String mpacksUrl) { - this.mpacksUrl = mpacksUrl; + public void setMpacksUri(String mpacksUri) { + this.mpacksUri = mpacksUri; } public String getName() { @@ -111,4 +111,127 @@ public class Mpacks { public void setPacklets(ArrayList<Packlet> packlets) { this.packlets = packlets; } + + /** + * {@inheritDoc} + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((mpackId == null) ? 0 : mpackId.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((version == null) ? 0 : version.hashCode()); + result = prime * result + ((registryId == null) ? 0 : registryId.hashCode()); + result = prime * result + ((description == null) ? 0 : description.hashCode()); + result = prime * result + ((prerequisites == null) ? 0 : prerequisites.hashCode()); + result = prime * result + ((packlets == null) ? 0 : packlets.hashCode()); + result = prime * result + ((mpacksUri == null) ? 0 : mpacksUri.hashCode()); + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj == null) { + return false; + } + + if (getClass() != obj.getClass()) { + return false; + } + + Mpacks other = (Mpacks) obj; + + if (name != other.name) { + return false; + } + + if (version == null) { + if (other.version != null) { + return false; + } + } else if (!version.equals(other.version)) { + return false; + } + + if (description == null) { + if (other.description != null) { + return false; + } + } else if (!description.equals(other.description)) { + return false; + } + + if (mpackId == null) { + if (other.mpackId != null) { + return false; + } + } else if (!mpackId.equals(other.mpackId)) { + return false; + } + + if (registryId == null) { + if (other.registryId != null) { + return false; + } + } else if (!registryId.equals(other.registryId)) { + return false; + } + + if (registryId == null) { + if (other.registryId != null) { + return false; + } + } else if (!registryId.equals(other.registryId)) { + return false; + } + + if (registryId == null) { + if (other.registryId != null) { + return false; + } + } else if (!registryId.equals(other.registryId)) { + return false; + } + + if (prerequisites == null) { + if (other.prerequisites != null) { + return false; + } + } else if (!prerequisites.equals(other.prerequisites)) { + return false; + } + + if (packlets == null) { + if (other.packlets != null) { + return false; + } + } else if (!packlets.equals(other.packlets)) { + return false; + } + + return true; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append('{'); + sb.append("name=").append(name).append(", "); + sb.append("mpackId=").append(mpackId).append(", "); + sb.append("version=").append(version).append(", "); + sb.append("registryId=").append(registryId).append(", "); + sb.append("description=").append(description).append(", "); + sb.append("prereq=").append(prerequisites.toString()).append(", "); + sb.append("packlets=").append(packlets.toString()).append(", "); + sb.append('}'); + return sb.toString(); + } } diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/Packlet.java b/ambari-server/src/main/java/org/apache/ambari/server/state/Packlet.java index 5c91232..0bd2479 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/state/Packlet.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/state/Packlet.java @@ -62,4 +62,79 @@ public class Packlet { this.sourceDir = sourceDir; } + /** + * {@inheritDoc} + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((type == null) ? 0 : type.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + result = prime * result + ((version == null) ? 0 : version.hashCode()); + result = prime * result + ((sourceDir == null) ? 0 : sourceDir.hashCode()); + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj == null) { + return false; + } + + if (getClass() != obj.getClass()) { + return false; + } + + Packlet other = (Packlet) obj; + + if (type != other.type) { + return false; + } + + if (name == null) { + if (other.name != null) { + return false; + } + } else if (!name.equals(other.name)) { + return false; + } + + if (version == null) { + if (other.version != null) { + return false; + } + } else if (!version.equals(other.version)) { + return false; + } + + if (sourceDir == null) { + if (other.sourceDir != null) { + return false; + } + } else if (!sourceDir.equals(other.sourceDir)) { + return false; + } + return true; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append('{'); + sb.append("type=").append(type).append(", "); + sb.append("name=").append(name).append(", "); + sb.append("version=").append(version).append(", "); + sb.append("source directory=").append(sourceDir).append(", "); + sb.append('}'); + return sb.toString(); + } + } diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/resources/MpackResourceDefinitionTest.java b/ambari-server/src/test/java/org/apache/ambari/server/api/resources/MpackResourceDefinitionTest.java new file mode 100644 index 0000000..08166b8 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/api/resources/MpackResourceDefinitionTest.java @@ -0,0 +1,35 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.server.api.resources; + +import static junit.framework.Assert.assertEquals; +import org.junit.Test; + +/** + * MpackServiceResourceDefinition unit tests + */ +public class MpackResourceDefinitionTest { + @Test + public void testDefinitionNames() { + ResourceDefinition def = new MpackResourceDefinition(); + assertEquals("mpack", def.getSingularName()); + assertEquals("mpacks", def.getPluralName()); + } +} diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/services/AmbariMetaInfoTest.java b/ambari-server/src/test/java/org/apache/ambari/server/api/services/AmbariMetaInfoTest.java index 6e8852c..64287d7 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/api/services/AmbariMetaInfoTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/api/services/AmbariMetaInfoTest.java @@ -18,6 +18,27 @@ package org.apache.ambari.server.api.services; +import org.apache.ambari.server.orm.dao.AlertDefinitionDAO; +import org.apache.ambari.server.orm.dao.MetainfoDAO; +import org.apache.ambari.server.state.AutoDeployInfo; +import org.apache.ambari.server.state.Cluster; +import org.apache.ambari.server.state.Clusters; +import org.apache.ambari.server.state.ComponentInfo; +import org.apache.ambari.server.state.CustomCommandDefinition; +import org.apache.ambari.server.state.DependencyInfo; +import org.apache.ambari.server.state.OperatingSystemInfo; +import org.apache.ambari.server.state.PropertyDependencyInfo; +import org.apache.ambari.server.state.PropertyInfo; +import org.apache.ambari.server.state.RepositoryInfo; +import org.apache.ambari.server.state.ServiceInfo; +import org.apache.ambari.server.state.StackId; +import org.apache.ambari.server.state.StackInfo; +import org.apache.ambari.server.state.Packlet; +import org.apache.ambari.server.state.Mpacks; +import org.apache.ambari.server.controller.MpackRequest; +import org.apache.ambari.server.controller.MpackResponse; +import org.apache.ambari.server.mpack.MpackManager; +import org.apache.ambari.server.mpack.MpackManagerFactory; import static org.easymock.EasyMock.createNiceMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; @@ -39,6 +60,8 @@ import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.UUID; +import java.util.HashMap; +import java.util.ArrayList; import javax.persistence.EntityManager; @@ -54,25 +77,10 @@ import org.apache.ambari.server.metadata.AmbariServiceAlertDefinitions; import org.apache.ambari.server.orm.GuiceJpaInitializer; import org.apache.ambari.server.orm.InMemoryDefaultTestModule; import org.apache.ambari.server.orm.OrmTestHelper; -import org.apache.ambari.server.orm.dao.AlertDefinitionDAO; -import org.apache.ambari.server.orm.dao.MetainfoDAO; import org.apache.ambari.server.orm.entities.AlertDefinitionEntity; import org.apache.ambari.server.orm.entities.RepositoryVersionEntity; import org.apache.ambari.server.stack.StackManager; import org.apache.ambari.server.stack.StackManagerFactory; -import org.apache.ambari.server.state.AutoDeployInfo; -import org.apache.ambari.server.state.Cluster; -import org.apache.ambari.server.state.Clusters; -import org.apache.ambari.server.state.ComponentInfo; -import org.apache.ambari.server.state.CustomCommandDefinition; -import org.apache.ambari.server.state.DependencyInfo; -import org.apache.ambari.server.state.OperatingSystemInfo; -import org.apache.ambari.server.state.PropertyDependencyInfo; -import org.apache.ambari.server.state.PropertyInfo; -import org.apache.ambari.server.state.RepositoryInfo; -import org.apache.ambari.server.state.ServiceInfo; -import org.apache.ambari.server.state.StackId; -import org.apache.ambari.server.state.StackInfo; import org.apache.ambari.server.state.alert.AlertDefinition; import org.apache.ambari.server.state.alert.AlertDefinitionFactory; import org.apache.ambari.server.state.alert.MetricSource; @@ -343,6 +351,30 @@ public class AmbariMetaInfoTest { } @Test + public void testRegisterMpacks() throws Exception{ + MpackManager mm = metaInfo.getMpackManager(); + MpackRequest mpackRequest = createNiceMock(MpackRequest.class); + Mpacks mpacks = new Mpacks(); + mpacks.setMpackId((long)100); + mpacks.setPacklets(new ArrayList<Packlet>()); + mpacks.setPrerequisites(new HashMap<String, String>()); + mpacks.setRegistryId(new Long(100)); + mpacks.setVersion("3.0"); + mpacks.setMpacksUri("abc.tar.gz"); + mpacks.setDescription("Test mpacks"); + mpacks.setName("testMpack"); + MpackResponse mpackResponse = new MpackResponse(mpacks); + expect(mm.registerMpack(mpackRequest)).andReturn(mpackResponse); + replay(mm); + assertEquals(mpackResponse,metaInfo.registerMpack(mpackRequest)); + } + + @Test + public void testGetPacklets() throws Exception{ + + } + + @Test public void testConfigDependencies() throws Exception { ServiceInfo serviceInfo = metaInfo.getService(STACK_NAME_HDP, EXT_STACK_NAME, SERVICE_NAME_MAPRED2); @@ -1935,7 +1967,10 @@ public class AmbariMetaInfoTest { Properties properties = new Properties(); properties.setProperty(Configuration.METADATA_DIR_PATH.getKey(), stackRoot.getPath()); properties.setProperty(Configuration.SERVER_VERSION_FILE.getKey(), versionFile.getPath()); + properties.setProperty(Configuration.RESOURCES_DIR.getKey(), resourcesRoot.getPath()); + properties.setProperty(Configuration.MPACKS_V2_STAGING_DIR_PATH.getKey(),"/var/lib/ambari-server/resources/mpacks-v2"); + Configuration configuration = new Configuration(properties); TestAmbariMetaInfo metaInfo = new TestAmbariMetaInfo(configuration); @@ -2010,6 +2045,12 @@ public class AmbariMetaInfoTest { f.setAccessible(true); f.set(this, stackManagerFactory); + // MpackManagerFactory + MpackManagerFactory mpackManagerFactory = injector.getInstance(MpackManagerFactory.class); + f = c.getDeclaredField("mpackManagerFactory"); + f.setAccessible(true); + f.set(this, mpackManagerFactory); + //AlertDefinitionDAO alertDefinitionDAO = createNiceMock(AlertDefinitionDAO.class); f = c.getDeclaredField("alertDefinitionDao"); diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/services/MpackServiceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/api/services/MpackServiceTest.java new file mode 100644 index 0000000..06eb15d --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/api/services/MpackServiceTest.java @@ -0,0 +1,94 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.server.api.services; + +import org.apache.ambari.server.api.resources.ResourceInstance; +import org.apache.ambari.server.api.services.parsers.RequestBodyParser; +import org.apache.ambari.server.api.services.serializers.ResultSerializer; + +import org.apache.ambari.server.api.util.ApiVersion; +import org.apache.ambari.server.controller.spi.Resource; + + +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.UriInfo; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * Unit tests for MpackService + */ +public class MpackServiceTest extends BaseServiceTest{ + @Override + public List<BaseServiceTest.ServiceTestInvocation> getTestInvocations() throws Exception { + List<BaseServiceTest.ServiceTestInvocation> listInvocations = new ArrayList<>(); + + // getMpacks + MpacksService service = new TestMpackService("null"); + Method m = service.getClass().getMethod("getMpacks", String.class, HttpHeaders.class, UriInfo.class); + Object[] args = new Object[]{null, getHttpHeaders(), getUriInfo()}; + listInvocations.add(new ServiceTestInvocation(Request.Type.GET, service, m, args, null)); + + // getMpack + service = new TestMpackService("1"); + m = service.getClass().getMethod("getMpack", String.class, HttpHeaders.class, UriInfo.class, String.class); + args = new Object[]{null, getHttpHeaders(), getUriInfo(), ""}; + listInvocations.add(new ServiceTestInvocation(Request.Type.GET, service, m, args, null)); + + //createMpacks + service = new TestMpackService(null); + m = service.getClass().getMethod("createMpacks", String.class, HttpHeaders.class, UriInfo.class); + args = new Object[]{"body", getHttpHeaders(), getUriInfo()}; + listInvocations.add(new ServiceTestInvocation(Request.Type.POST, service, m, args, "body")); + + return listInvocations; + } + private class TestMpackService extends MpacksService { + + private String m_mpackId; + + private TestMpackService(String mpackId) { + super(ApiVersion.Default); + m_mpackId = mpackId; + } + + @Override + protected ResourceInstance createResource(Resource.Type type, Map<Resource.Type, String> mapIds) { + return getTestResource(); + } + + @Override + RequestFactory getRequestFactory() { + return getTestRequestFactory(); + } + + @Override + protected RequestBodyParser getBodyParser() { + return getTestBodyParser(); + } + + @Override + protected ResultSerializer getResultSerializer() { + return getTestResultSerializer(); + } + } + + +} diff --git a/ambari-server/src/test/java/org/apache/ambari/server/configuration/ConfigurationTest.java b/ambari-server/src/test/java/org/apache/ambari/server/configuration/ConfigurationTest.java index 6b79e75..b8d95c7 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/configuration/ConfigurationTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/configuration/ConfigurationTest.java @@ -157,6 +157,16 @@ public class ConfigurationTest { } @Test + public void testGetMpacksV2StagingPath() { + Properties ambariProperties = new Properties(); + ambariProperties.setProperty(Configuration.MPACKS_V2_STAGING_DIR_PATH.getKey(), "/var/lib/ambari-server/resources/mpacks-v2/"); + Configuration conf = new Configuration(ambariProperties); + Assert.assertEquals("/var/lib/ambari-server/resources/mpacks-v2/", conf.getMpacksV2StagingPath()); + conf = new Configuration(); + Assert.assertEquals(null, conf.getMpacksV2StagingPath()); + } + + @Test public void testGetClientHTTPSSettings() throws IOException { File passFile = File.createTempFile("https.pass.", "txt"); diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerImplTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerImplTest.java index af85d9b..ac4ceb9 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerImplTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/AmbariManagementControllerImplTest.java @@ -24,6 +24,30 @@ import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.JAVA_VERS import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.NOT_MANAGED_HDFS_PATH_LIST; import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.STACK_NAME; import static org.apache.ambari.server.agent.ExecutionCommand.KeyNames.STACK_VERSION; +import org.apache.ambari.server.controller.spi.Resource; +import org.apache.ambari.server.security.authorization.Users; +import org.apache.ambari.server.state.Cluster; +import org.apache.ambari.server.state.Clusters; +import org.apache.ambari.server.state.ComponentInfo; +import org.apache.ambari.server.state.Config; +import org.apache.ambari.server.state.ConfigHelper; +import org.apache.ambari.server.state.DesiredConfig; +import org.apache.ambari.server.state.Host; +import org.apache.ambari.server.state.MaintenanceState; +import org.apache.ambari.server.state.PropertyInfo; +import org.apache.ambari.server.state.RepositoryInfo; +import org.apache.ambari.server.state.RepositoryVersionState; +import org.apache.ambari.server.state.SecurityType; +import org.apache.ambari.server.state.Service; +import org.apache.ambari.server.state.ServiceComponent; +import org.apache.ambari.server.state.ServiceComponentHost; +import org.apache.ambari.server.state.ServiceInfo; +import org.apache.ambari.server.state.ServiceOsSpecific; +import org.apache.ambari.server.state.StackId; +import org.apache.ambari.server.state.StackInfo; +import org.apache.ambari.server.state.State; +import org.apache.ambari.server.state.Mpacks; +import org.apache.ambari.server.state.Packlet; import static org.easymock.EasyMock.anyBoolean; import static org.easymock.EasyMock.anyLong; import static org.easymock.EasyMock.anyObject; @@ -75,12 +99,10 @@ import org.apache.ambari.server.agent.stomp.MetadataHolder; import org.apache.ambari.server.api.services.AmbariMetaInfo; import org.apache.ambari.server.configuration.Configuration; import org.apache.ambari.server.controller.internal.RequestStageContainer; -import org.apache.ambari.server.controller.spi.Resource; import org.apache.ambari.server.orm.InMemoryDefaultTestModule; import org.apache.ambari.server.orm.dao.RepositoryVersionDAO; import org.apache.ambari.server.orm.entities.LdapSyncSpecEntity; import org.apache.ambari.server.orm.entities.RepositoryVersionEntity; -import org.apache.ambari.server.security.authorization.Users; import org.apache.ambari.server.security.authorization.internal.InternalAuthenticationToken; import org.apache.ambari.server.security.encryption.CredentialStoreService; import org.apache.ambari.server.security.encryption.CredentialStoreType; @@ -2425,6 +2447,49 @@ public class AmbariManagementControllerImplTest { verify(injector, clusters, ambariMetaInfo, stackInfo, cluster, repoVersionDAO, repoVersion); } + @Test + public void testRegisterMpacks() throws Exception{ + MpackRequest mpackRequest = createNiceMock(MpackRequest.class); + Mpacks mpacks = new Mpacks(); + mpacks.setMpackId((long)100); + mpacks.setPacklets(new ArrayList<Packlet>()); + mpacks.setPrerequisites(new HashMap<String, String>()); + mpacks.setRegistryId(new Long(100)); + mpacks.setVersion("3.0"); + mpacks.setMpacksUri("abc.tar.gz"); + mpacks.setDescription("Test mpacks"); + mpacks.setName("testMpack"); + MpackResponse mpackResponse = new MpackResponse(mpacks); + Injector injector = createNiceMock(Injector.class); + expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).atLeastOnce(); + expect(ambariMetaInfo.registerMpack(mpackRequest)).andReturn(mpackResponse); + replay(ambariMetaInfo,injector); + AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); + setAmbariMetaInfo(ambariMetaInfo, controller); + Assert.assertEquals(mpackResponse,controller.registerMpack(mpackRequest)); + } + + @Test + public void testGetPacklets() throws Exception { + Long mpackId = new Long(100); + ArrayList<Packlet> packletArrayList = new ArrayList<>(); + Packlet samplePacklet = new Packlet(); + Injector injector = createNiceMock(Injector.class); + samplePacklet.setType("service"); + samplePacklet.setVersion("3.0.0"); + samplePacklet.setName("NIFI"); + samplePacklet.setSourceDir("/abc/nifi.tar.gz"); + packletArrayList.add(samplePacklet); + expect(injector.getInstance(MaintenanceStateHelper.class)).andReturn(null).atLeastOnce(); + expect(ambariMetaInfo.getPacklets(mpackId)).andReturn(packletArrayList).atLeastOnce(); + replay(ambariMetaInfo,injector); + AmbariManagementController controller = new AmbariManagementControllerImpl(null, clusters, injector); + setAmbariMetaInfo(ambariMetaInfo, controller); + + Assert.assertEquals(packletArrayList,controller.getPacklets(mpackId)); + + } + public static void constructorInit(Injector injector, Capture<AmbariManagementController> controllerCapture, Gson gson, MaintenanceStateHelper maintenanceStateHelper, KerberosHelper kerberosHelper, Provider<MetadataHolder> m_metadataHolder, Provider<AgentConfigsHolder> m_agentConfigsHolder) { @@ -2434,6 +2499,7 @@ public class AmbariManagementControllerImplTest { expect(injector.getInstance(KerberosHelper.class)).andReturn(kerberosHelper); } + public static void constructorInit(Injector injector, Capture<AmbariManagementController> controllerCapture, KerberosHelper kerberosHelper) { injector.injectMembers(capture(controllerCapture)); diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/MpackRequestTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/MpackRequestTest.java new file mode 100644 index 0000000..e858e54 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/MpackRequestTest.java @@ -0,0 +1,44 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.server.controller; + + +import org.junit.Test; +import org.junit.Assert; + +/** + * Unit tests for MpackRequest + */ +public class MpackRequestTest { + @Test + public void testBasicGetAndSet() { + MpackRequest mpackRequest = + new MpackRequest(new Long(1)); + Assert.assertEquals("1", mpackRequest.getMpackId()); + mpackRequest.setMpackUri("abc.tar.gz"); + mpackRequest.setRegistryId(new Long(1)); + mpackRequest.setMpackVersion("3.0"); + mpackRequest.setMpackName("testmpack"); + + Assert.assertEquals("abc.tar.gz", mpackRequest.getMpackUri()); + Assert.assertEquals("1", mpackRequest.getRegistryId()); + Assert.assertEquals("3.0", mpackRequest.getMpackVersion()); + Assert.assertEquals("testmpack", mpackRequest.getMpackName()); + + } +} diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/MpackResponseTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/MpackResponseTest.java new file mode 100644 index 0000000..c61d515 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/MpackResponseTest.java @@ -0,0 +1,54 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.server.controller; + +import org.apache.ambari.server.state.Mpacks; +import org.apache.ambari.server.state.Packlet; +import org.junit.Test; +import org.junit.Assert; +import java.util.ArrayList; +import java.util.HashMap; + +/** + * Unit tests for MpackResponse + */ +public class MpackResponseTest { + @Test + public void testBasicGetAndSet() { + MpackResponse mpackResponse = new MpackResponse(setupMpacks()); + Assert.assertEquals(new Long(100), mpackResponse.getMpackId()); + Assert.assertEquals("100",mpackResponse.getRegistryId()); + Assert.assertEquals("3.0",mpackResponse.getMpackVersion()); + Assert.assertEquals("abc.tar.gz",mpackResponse.getMpackUri()); + Assert.assertEquals("testMpack", mpackResponse.getMpackName()); + + } + public Mpacks setupMpacks(){ + Mpacks mpacks = new Mpacks(); + mpacks.setMpackId((long)100); + mpacks.setPacklets(new ArrayList<Packlet>()); + mpacks.setPrerequisites(new HashMap<String, String>()); + mpacks.setRegistryId(new Long(100)); + mpacks.setVersion("3.0"); + mpacks.setMpacksUri("abc.tar.gz"); + mpacks.setDescription("Test mpacks"); + mpacks.setName("testMpack"); + + return mpacks; + } +} diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/MpackResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/MpackResourceProviderTest.java new file mode 100644 index 0000000..d6638c6 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/MpackResourceProviderTest.java @@ -0,0 +1,281 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.apache.ambari.server.controller.internal; + + + +import com.google.inject.Injector; +import com.google.inject.Binder; +import com.google.inject.Guice; +import com.google.inject.util.Modules; +import com.google.inject.Module; +import org.apache.ambari.server.controller.AmbariManagementController; +import org.apache.ambari.server.controller.MpackResponse; +import org.apache.ambari.server.controller.spi.Resource; +import org.apache.ambari.server.controller.spi.ResourceProvider; +import org.apache.ambari.server.controller.spi.Request; +import org.apache.ambari.server.controller.spi.Predicate; +import org.apache.ambari.server.controller.MpackRequest; +import org.apache.ambari.server.controller.utilities.*; +import org.apache.ambari.server.orm.InMemoryDefaultTestModule; +import org.apache.ambari.server.orm.dao.MpackDAO; +import org.apache.ambari.server.orm.entities.MpackEntity; +import org.apache.ambari.server.state.Mpacks; +import org.apache.ambari.server.state.Packlet; +import org.easymock.EasyMock; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.verify; +import org.junit.Test; +import org.junit.Assert; +import org.junit.Before; + + +import javax.persistence.EntityManager; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.List; + +public class MpackResourceProviderTest { + + private MpackDAO m_dao; + private Injector m_injector; + private AmbariManagementController m_amc; + + @Before + public void before() throws Exception { + m_dao = EasyMock.createNiceMock(MpackDAO.class); + m_amc = EasyMock.createNiceMock(AmbariManagementController.class); + + m_injector = Guice.createInjector(Modules.override(new InMemoryDefaultTestModule()).with( + new MockModule())); + } + + @Test + public void testGetResourcesMpacks() throws Exception { + Resource.Type type = Resource.Type.Mpack; + + Resource resourceExpected1 = new ResourceImpl(Resource.Type.Mpack); + resourceExpected1.setProperty(MpackResourceProvider.MPACK_ID, (long)1); + resourceExpected1.setProperty(MpackResourceProvider.MPACK_NAME, "TestMpack1"); + resourceExpected1.setProperty(MpackResourceProvider.MPACK_VERSION, "3.0"); + resourceExpected1.setProperty(MpackResourceProvider.MPACK_URI, "abcd.tar.gz"); + resourceExpected1.setProperty(MpackResourceProvider.REGISTRY_ID, null); + + Resource resourceExpected2 = new ResourceImpl(Resource.Type.Mpack); + resourceExpected2.setProperty(MpackResourceProvider.MPACK_ID, (long)2); + resourceExpected2.setProperty(MpackResourceProvider.MPACK_NAME, "TestMpack2"); + resourceExpected2.setProperty(MpackResourceProvider.MPACK_VERSION, "3.0"); + resourceExpected2.setProperty(MpackResourceProvider.MPACK_URI, "abc.tar.gz"); + resourceExpected2.setProperty(MpackResourceProvider.REGISTRY_ID, (long)1); + + List<MpackEntity> entities = new ArrayList<>(); + MpackEntity entity = new MpackEntity(); + entity.setMpackId((long) 1); + entity.setMpackUri("abcd.tar.gz"); + entity.setMpackName("TestMpack1"); + entity.setMpackVersion("3.0"); + entities.add(entity); + + entity = new MpackEntity(); + entity.setMpackId((long) 2); + entity.setMpackUri("abc.tar.gz"); + entity.setMpackName("TestMpack2"); + entity.setMpackVersion("3.0"); + entity.setRegistryId(new Long(1)); + entities.add(entity); + + // set expectations + EasyMock.expect(m_dao.findAll()).andReturn(entities).anyTimes(); + + // replay + replay(m_dao); + + ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( + type, + PropertyHelper.getPropertyIds(type), + PropertyHelper.getKeyPropertyIds(type), + m_amc); + + // create the request + Request request = PropertyHelper.getReadRequest(); + + // get all ... no predicate + Set<Resource> resources = provider.getResources(request, null); + + Assert.assertEquals(2, resources.size()); + + for (Resource resource : resources) { + Long mpackId = (Long) resource.getPropertyValue(MpackResourceProvider.MPACK_ID); + if (mpackId == (long) 1) { + Assert.assertEquals(resourceExpected1.getPropertyValue(MpackResourceProvider.MPACK_NAME), (String) resource.getPropertyValue(MpackResourceProvider.MPACK_NAME)); + Assert.assertEquals(resourceExpected1.getPropertyValue(MpackResourceProvider.MPACK_VERSION), (String) resource.getPropertyValue(MpackResourceProvider.MPACK_VERSION)); + Assert.assertEquals(resourceExpected1.getPropertyValue(MpackResourceProvider.MPACK_URI), (String) resource.getPropertyValue(MpackResourceProvider.MPACK_URI)); + Assert.assertEquals(resourceExpected1.getPropertyValue(MpackResourceProvider.REGISTRY_ID), (Long) resource.getPropertyValue(MpackResourceProvider.REGISTRY_ID)); + } else if (mpackId == (long) 2) { + Assert.assertEquals(resourceExpected2.getPropertyValue(MpackResourceProvider.MPACK_NAME), (String) resource.getPropertyValue(MpackResourceProvider.MPACK_NAME)); + Assert.assertEquals(resourceExpected2.getPropertyValue(MpackResourceProvider.MPACK_VERSION), (String) resource.getPropertyValue(MpackResourceProvider.MPACK_VERSION)); + Assert.assertEquals(resourceExpected2.getPropertyValue(MpackResourceProvider.MPACK_URI), (String) resource.getPropertyValue(MpackResourceProvider.MPACK_URI)); + Assert.assertEquals(resourceExpected2.getPropertyValue(MpackResourceProvider.REGISTRY_ID), (Long) resource.getPropertyValue(MpackResourceProvider.REGISTRY_ID)); + } else { + Assert.assertTrue(false); + } + } + + // verify + verify(m_dao); + } + + @Test + public void testGetResourcesMpackId() throws Exception { + Resource.Type type = Resource.Type.Mpack; + + Predicate predicate = new PredicateBuilder().property( + MpackResourceProvider.MPACK_ID).equals( + Long.valueOf(1).toString()).toPredicate(); + + MpackEntity entity = new MpackEntity(); + entity.setMpackId((long) 1); + entity.setMpackUri("abcd.tar.gz"); + entity.setMpackName("TestMpack1"); + entity.setMpackVersion("3.0"); + + + ArrayList<Packlet> packletArrayList = new ArrayList<>(); + Packlet packlet = new Packlet(); + packlet.setName("testService"); + packlet.setType("service"); + packlet.setSourceDir("testDir/testDir"); + packlet.setVersion("3.0"); + packletArrayList.add(packlet); + + Resource resourceExpected1 = new ResourceImpl(Resource.Type.Mpack); + resourceExpected1.setProperty(MpackResourceProvider.MPACK_ID, (long)1); + resourceExpected1.setProperty(MpackResourceProvider.MPACK_NAME, "TestMpack1"); + resourceExpected1.setProperty(MpackResourceProvider.MPACK_VERSION, "3.0"); + resourceExpected1.setProperty(MpackResourceProvider.MPACK_URI, "abcd.tar.gz"); + resourceExpected1.setProperty(MpackResourceProvider.REGISTRY_ID, null); + resourceExpected1.setProperty(MpackResourceProvider.PACKLETS,packletArrayList); + + // set expectations + EasyMock.expect(m_dao.findById((long)1)).andReturn(entity).anyTimes(); + EasyMock.expect(m_amc.getPacklets((long)1)).andReturn(packletArrayList).anyTimes(); + + // replay + replay(m_dao,m_amc); + + ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( + type, + PropertyHelper.getPropertyIds(type), + PropertyHelper.getKeyPropertyIds(type), + m_amc); + + // create the request + Request request = PropertyHelper.getReadRequest(); + + // get all ... no predicate + Set<Resource> resources = provider.getResources(request,predicate); + + Assert.assertEquals(1, resources.size()); + for(Resource resource: resources){ + Assert.assertEquals(resourceExpected1.getPropertyValue(MpackResourceProvider.MPACK_NAME), (String) resource.getPropertyValue(MpackResourceProvider.MPACK_NAME)); + Assert.assertEquals(resourceExpected1.getPropertyValue(MpackResourceProvider.MPACK_VERSION), (String) resource.getPropertyValue(MpackResourceProvider.MPACK_VERSION)); + Assert.assertEquals(resourceExpected1.getPropertyValue(MpackResourceProvider.MPACK_URI), (String) resource.getPropertyValue(MpackResourceProvider.MPACK_URI)); + Assert.assertEquals(resourceExpected1.getPropertyValue(MpackResourceProvider.REGISTRY_ID), (Long) resource.getPropertyValue(MpackResourceProvider.REGISTRY_ID)); + Assert.assertEquals(resourceExpected1.getPropertyValue(MpackResourceProvider.PACKLETS),(ArrayList)resource.getPropertyValue(MpackResourceProvider.PACKLETS)); + } + // verify + verify(m_dao,m_amc); + + } + + @Test + public void testCreateResources() throws Exception { + MpackRequest mpackRequest = new MpackRequest(); + mpackRequest.setMpackUri("abc.tar.gz"); + Request request = createMock(Request.class); + MpackResponse response = new MpackResponse(setupMpacks()); + Set<Map<String, Object>> properties = new HashSet<>(); + Map propertyMap = new HashMap(); + propertyMap.put(MpackResourceProvider.MPACK_URI,"abc.tar.gz"); + properties.add(propertyMap); + + // set expectations + EasyMock.expect(m_amc.registerMpack(mpackRequest)).andReturn(response).anyTimes(); + EasyMock.expect(request.getProperties()).andReturn(properties).anyTimes(); + replay(m_amc,request); + // end expectations + + ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( + Resource.Type.Mpack, + PropertyHelper.getPropertyIds(Resource.Type.Mpack), + PropertyHelper.getKeyPropertyIds(Resource.Type.Mpack), + m_amc); + + AbstractResourceProviderTest.TestObserver observer = new AbstractResourceProviderTest.TestObserver(); + ((ObservableResourceProvider)provider).addObserver(observer); + + RequestStatusImpl requestStatus = (RequestStatusImpl) provider.createResources(request); + Set<Resource> associatedResources = requestStatus.getAssociatedResources(); + + Assert.assertEquals(1,associatedResources.size()); + for(Resource r : associatedResources){ + Assert.assertEquals((long)100,r.getPropertyValue(MpackResourceProvider.MPACK_ID)); + Assert.assertEquals("testMpack",r.getPropertyValue(MpackResourceProvider.MPACK_NAME)); + Assert.assertEquals("3.0",r.getPropertyValue(MpackResourceProvider.MPACK_VERSION)); + Assert.assertEquals("abc.tar.gz",r.getPropertyValue(MpackResourceProvider.MPACK_URI)); + } + ResourceProviderEvent lastEvent = observer.getLastEvent(); + Assert.assertNotNull(lastEvent); + Assert.assertEquals(Resource.Type.Mpack, lastEvent.getResourceType()); + Assert.assertEquals(ResourceProviderEvent.Type.Create, lastEvent.getType()); + Assert.assertEquals(request, lastEvent.getRequest()); + Assert.assertNull(lastEvent.getPredicate()); + + verify(m_amc,request); + } + + public Mpacks setupMpacks(){ + Mpacks mpacks = new Mpacks(); + mpacks.setMpackId((long)100); + mpacks.setPacklets(new ArrayList<Packlet>()); + mpacks.setPrerequisites(new HashMap<String, String>()); + mpacks.setRegistryId(new Long(100)); + mpacks.setVersion("3.0"); + mpacks.setMpacksUri("abc.tar.gz"); + mpacks.setDescription("Test mpacks"); + mpacks.setName("testMpack"); + + return mpacks; + } + + /** + * + */ + private class MockModule implements Module { + @Override + public void configure(Binder binder) { + binder.bind(EntityManager.class).toInstance(EasyMock.createMock(EntityManager.class)); + binder.bind(MpackDAO.class).toInstance(m_dao); + binder.bind(AmbariManagementController.class).toInstance(m_amc); + } + } +} \ No newline at end of file diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/MpackDAOTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/MpackDAOTest.java new file mode 100644 index 0000000..7b45815 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/MpackDAOTest.java @@ -0,0 +1,76 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.apache.ambari.server.orm.dao; + +import com.google.inject.Injector; +import com.google.inject.Guice; +import org.apache.ambari.server.orm.entities.MpackEntity; +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import org.apache.ambari.server.orm.InMemoryDefaultTestModule; +import org.apache.ambari.server.orm.GuiceJpaInitializer; +import com.google.inject.persist.UnitOfWork; + +import java.util.List; +import java.util.ArrayList; + +/** + * Tests {@link MpackDAO}. + */ +public class MpackDAOTest { + private Injector m_injector; + private MpackDAO m_dao; + + @Before + public void init() { + m_injector = Guice.createInjector(new InMemoryDefaultTestModule()); + m_injector.getInstance(GuiceJpaInitializer.class); + m_injector.getInstance(UnitOfWork.class).begin(); + m_dao = m_injector.getInstance(MpackDAO.class); + } + + @Test + public void testCreateFind() { + List<MpackEntity> eDefinitions = new ArrayList<>(); + + // create 2 definitions + for (int i = 1; i < 3; i++) { + MpackEntity definition = new MpackEntity(); + definition.setMpackId(new Long(100)+i); + definition.setMpackName("testMpack" + i); + definition.setRegistryId(Long.valueOf(i)); + definition.setMpackVersion("3.0.0.0-12"+i); + definition.setMpackUri("http://c6401.ambari.apache.org:8080/resources/mpacks-repo/testMpack" + i + "-3.0.0.0-123.tar.gz"); + eDefinitions.add(definition); + m_dao.create(definition); + } + + List<MpackEntity> definitions = m_dao.findAll(); + assertNotNull(definitions); + assertEquals(2, definitions.size()); + definitions = m_dao.findByNameVersion("testMpack1","3.0.0.0-121"); + assertEquals(1, definitions.size()); + assertEquals(new Long(101),(Long)definitions.get(0).getMpackId()); + MpackEntity entity = m_dao.findById(new Long(102)); + assertEquals(entity.getMpackName(),"testMpack2"); + assertEquals(entity.getMpackVersion(),"3.0.0.0-122"); + + } +} diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/entities/MpackEntityTest.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/entities/MpackEntityTest.java new file mode 100644 index 0000000..7948111 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/entities/MpackEntityTest.java @@ -0,0 +1,68 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.server.orm.entities; + +import org.junit.Test; +import org.junit.Assert; + +import java.util.Objects; + +/** + * Tests methods on {@link MpackEntity}. + */ +public class MpackEntityTest { + /** + * Tests {@link MpackEntity#hashCode()} and {@link MpackEntity#equals(Object)} + */ + @Test + public void testHashCodeAndEquals(){ + MpackEntity entity1 = new MpackEntity(); + MpackEntity entity2 = new MpackEntity(); + + Assert.assertEquals(entity1.hashCode(), entity2.hashCode()); + Assert.assertTrue(Objects.equals(entity1, entity2)); + + entity1.setMpackId(new Long(1)); + entity2.setMpackId(new Long(2)); + Assert.assertNotSame(entity1.hashCode(), entity2.hashCode()); + Assert.assertFalse(Objects.equals(entity1, entity2)); + + entity2.setMpackId(new Long(1)); + Assert.assertEquals(entity1.hashCode(), entity2.hashCode()); + Assert.assertTrue(Objects.equals(entity1, entity2)); + + entity1.setMpackName("testMpack1"); + entity2.setMpackName("testMpack2"); + Assert.assertNotSame(entity1.hashCode(), entity2.hashCode()); + Assert.assertFalse(Objects.equals(entity1, entity2)); + + entity2.setMpackName("testMpack1"); + Assert.assertEquals(entity1.hashCode(), entity2.hashCode()); + Assert.assertTrue(Objects.equals(entity1, entity2)); + + entity1.setMpackVersion("3.0"); + entity2.setMpackVersion("3.1"); + Assert.assertNotSame(entity1.hashCode(), entity2.hashCode()); + Assert.assertFalse(Objects.equals(entity1, entity2)); + + entity2.setMpackVersion("3.0"); + Assert.assertEquals(entity1.hashCode(), entity2.hashCode()); + Assert.assertTrue(Objects.equals(entity1, entity2)); + } +} diff --git a/ambari-server/src/test/java/org/apache/ambari/server/state/MpacksTest.java b/ambari-server/src/test/java/org/apache/ambari/server/state/MpacksTest.java new file mode 100644 index 0000000..6d87a60 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/state/MpacksTest.java @@ -0,0 +1,95 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.ambari.server.state; + +import com.google.gson.Gson; +import org.junit.Test; +import org.junit.Assert; + +import java.util.ArrayList; +import java.util.HashMap; + +public class MpacksTest { + @Test + public void testMpacks() { + Mpacks mpacks = new Mpacks(); + mpacks.setName("name"); + mpacks.setMpackId((long)100); + mpacks.setDescription("desc"); + mpacks.setVersion("3.0"); + mpacks.setMpacksUri("abc.tar.gz"); + mpacks.setRegistryId(new Long(100)); + + Assert.assertEquals("name", mpacks.getName()); + Assert.assertEquals(new Long(100), mpacks.getMpackId()); + Assert.assertEquals("desc", mpacks.getDescription()); + Assert.assertEquals("abc.tar.gz", mpacks.getMpacksUri()); + Assert.assertEquals(new Long(100), mpacks.getRegistryId()); + + } + + @Test + public void testMpacksUsingGson() { + String mpackJsonContents = "{\n" + + " \"name\" : \"hdf-ambari-mpack\",\n" + + " \"version\": \"3.0.0.0-111\",\n" + + " \"description\" : \"HDF 3.0.0 Ambari Management Pack\",\n" + + " \"prerequisites\": {\n" + + " \"min-ambari-version\" : \"3.0.0.0\"\n" + + " },\n" + + " \"packlets\": [\n" + + " {\n" + + " \"type\" : \"service-packlet\",\n" + + " \"name\" : \"NIFI\",\n" + + " \"version\" : \"1.2.0.0-123\",\n" + + " \"source_dir\": \"packlets/NIFI-1.2.0.0-123.tar.gz\"\n" + + " },\n" + + " {\n" + + " \"type\" : \"service-packlet\",\n" + + " \"name\" : \"STREAMLINE\",\n" + + " \"version\" : \"1.0.0.0-100\",\n" + + " \"source_dir\": \"packlets/STREAMLINE-1.0.0.0-100.tar.gz\"\n" + + " }\n" + + " ]\n" + + "}\n"; + HashMap<String, String> expectedPrereq = new HashMap<>(); + expectedPrereq.put("min-ambari-version","3.0.0.0"); + ArrayList<Packlet> expectedPacklets = new ArrayList<>(); + Packlet nifi = new Packlet(); + nifi.setType("service-packlet"); + nifi.setVersion("1.2.0.0-123"); + nifi.setSourceDir("packlets/NIFI-1.2.0.0-123.tar.gz"); + nifi.setName("NIFI"); + Packlet streamline = new Packlet(); + streamline.setName("STREAMLINE"); + streamline.setType("service-packlet"); + streamline.setSourceDir("packlets/STREAMLINE-1.0.0.0-100.tar.gz"); + streamline.setVersion("1.0.0.0-100"); + expectedPacklets.add(nifi); + expectedPacklets.add(streamline); + + Gson gson = new Gson(); + Mpacks mpacks = gson.fromJson(mpackJsonContents, Mpacks.class); + Assert.assertEquals("hdf-ambari-mpack",mpacks.getName()); + Assert.assertEquals("3.0.0.0-111", mpacks.getVersion()); + Assert.assertEquals("HDF 3.0.0 Ambari Management Pack",mpacks.getDescription()); + Assert.assertEquals(expectedPrereq, mpacks.getPrerequisites()); + Assert.assertEquals(expectedPacklets.toString(), mpacks.getPacklets().toString()); + } + +}
