@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } PackageMaterial that = (PackageMaterial) o; if (this.getFingerprint() != null ? !this.getFingerprint().equals(that.getFingerprint()) : that.getFingerprint() != null) { return false; } return true; }
@Override protected void appendCriteria(Map<String, Object> parameters) { parameters.put("fingerprint", getFingerprint()); }
@Test public void shouldNotCalculateFingerprintWhenAvailable() { String fingerprint = "fingerprint"; PackageDefinition packageDefinition = mock(PackageDefinition.class); PackageMaterial packageMaterial = new PackageMaterial(); packageMaterial.setPackageDefinition(packageDefinition); packageMaterial.setFingerprint(fingerprint); assertThat(packageMaterial.getFingerprint(),is(fingerprint)); verify(packageDefinition,never()).getFingerprint(anyString()); }
@Test public void shouldGetSqlCriteria() { PackageMaterial material = new PackageMaterial(); PackageRepository repository = PackageRepositoryMother.create("repo-id", "repo", "pluginid", "version", new Configuration(ConfigurationPropertyMother.create("k1", false, "v1"))); material.setPackageDefinition(PackageDefinitionMother.create("p-id", "name", new Configuration(ConfigurationPropertyMother.create("k2", false, "v2")), repository)); Map<String, Object> criteria = material.getSqlCriteria(); assertThat(criteria.get("type"), is(PackageMaterial.class.getSimpleName())); assertThat(criteria.get("fingerprint"), is(material.getFingerprint())); }
@Test public void shouldSetFingerprintWhenConvertingMaterialInstanceToMaterial() { String fingerprint = "fingerprint"; PackageMaterial material = MaterialsMother.packageMaterial(); PackageMaterialInstance materialInstance = new PackageMaterialInstance(JsonHelper.toJsonString(material), "flyweight"); ReflectionUtil.setField(materialInstance, "fingerprint", fingerprint); materialInstance.setId(1L); PackageMaterial constructedMaterial = (PackageMaterial) materialInstance.toOldMaterial(null, null, null); assertThat(constructedMaterial.getFingerprint(), is(fingerprint)); }
@Test public void shouldGetDifferentFingerprintWhenPluginIdChanges() { PackageMaterial material = new PackageMaterial(); PackageRepository repository = PackageRepositoryMother.create("repo-id", "repo", "yum-1", "version", new Configuration(ConfigurationPropertyMother.create("k1", false, "v1"))); material.setPackageDefinition(PackageDefinitionMother.create("p-id-1", "name", new Configuration(ConfigurationPropertyMother.create("k2", false, "v2")), repository)); PackageMaterial anotherMaterial = new PackageMaterial(); PackageRepository anotherRepository = PackageRepositoryMother.create("repo-id", "repo", "yum-2", "version", new Configuration(ConfigurationPropertyMother.create("k1", false, "v1"))); anotherMaterial.setPackageDefinition(PackageDefinitionMother.create("p-id-2", "name", new Configuration(ConfigurationPropertyMother.create("k2", false, "v2")), anotherRepository)); assertThat(material.getFingerprint().equals(anotherMaterial.getFingerprint()), is(false)); }
@Test public void shouldGetFingerprintForMaterial() { PackageMaterial material = new PackageMaterial(); PackageRepository repository = PackageRepositoryMother.create("repo-id", "repo", "pluginid", "version", new Configuration(ConfigurationPropertyMother.create("k1", false, "v1"), ConfigurationPropertyMother.create("secure-key", true, "secure-value"))); material.setPackageDefinition(PackageDefinitionMother.create("p-id", "name", new Configuration(ConfigurationPropertyMother.create("k2", false, "v2")), repository)); assertThat(material.getFingerprint(), is(CachedDigestUtils.sha256Hex("plugin-id=pluginid<|>k2=v2<|>k1=v1<|>secure-key=secure-value"))); }