Skip to content

Commit

Permalink
Update generic repo TagType implementation
Browse files Browse the repository at this point in the history
1. Added URI column (not requiring uniqueness, for the time being)
2. Added basic add/modify/search/delete tests
3. Fixed scripts for H2
  • Loading branch information
mederly committed Dec 19, 2022
1 parent e788b07 commit a25c633
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -609,13 +609,6 @@ CREATE TABLE m_generic_object (
oid VARCHAR(36) NOT NULL,
PRIMARY KEY (oid)
);
CREATE TABLE m_tag (
name_norm VARCHAR(255),
name_orig VARCHAR(255),
objectType VARCHAR(255),
oid VARCHAR(36) NOT NULL,
PRIMARY KEY (oid)
);
CREATE TABLE m_global_metadata (
name VARCHAR(255) NOT NULL,
value VARCHAR(255),
Expand Down Expand Up @@ -729,6 +722,13 @@ CREATE TABLE m_system_configuration (
oid VARCHAR(36) NOT NULL,
PRIMARY KEY (oid)
);
CREATE TABLE m_tag (
name_norm VARCHAR(255),
name_orig VARCHAR(255),
uri VARCHAR(255),
oid VARCHAR(36) NOT NULL,
PRIMARY KEY (oid)
);
CREATE TABLE m_trigger (
id INTEGER NOT NULL,
owner_oid VARCHAR(36) NOT NULL,
Expand Down Expand Up @@ -959,10 +959,6 @@ CREATE INDEX iGenericObjectNameOrig
ON m_generic_object (name_orig);
ALTER TABLE m_generic_object
ADD CONSTRAINT uc_generic_object_name UNIQUE (name_norm);
CREATE INDEX iTagNameOrig
ON m_tag (name_orig);
ALTER TABLE m_tag
ADD CONSTRAINT uc_tag_name UNIQUE (name_norm);
CREATE INDEX iLookupTableNameOrig
ON m_lookup_table (name_orig);
ALTER TABLE m_lookup_table
Expand Down Expand Up @@ -1023,6 +1019,10 @@ CREATE INDEX iSystemConfigurationNameOrig
ON m_system_configuration (name_orig);
ALTER TABLE m_system_configuration
ADD CONSTRAINT uc_system_configuration_name UNIQUE (name_norm);
CREATE INDEX iTagNameOrig
ON m_tag (name_orig);
ALTER TABLE m_tag
ADD CONSTRAINT uc_tag_name UNIQUE (name_norm);
CREATE INDEX iTriggerTimestamp
ON m_trigger (timestampValue);
CREATE INDEX iFullName
Expand Down Expand Up @@ -1183,8 +1183,6 @@ ALTER TABLE m_function_library
ADD CONSTRAINT fk_function_library FOREIGN KEY (oid) REFERENCES m_object;
ALTER TABLE m_generic_object
ADD CONSTRAINT fk_generic_object FOREIGN KEY (oid) REFERENCES m_focus;
ALTER TABLE m_tag
ADD CONSTRAINT fk_tag FOREIGN KEY (oid) REFERENCES m_object;
ALTER TABLE m_lookup_table
ADD CONSTRAINT fk_lookup_table FOREIGN KEY (oid) REFERENCES m_object;
ALTER TABLE m_lookup_table_row
Expand Down Expand Up @@ -1213,6 +1211,8 @@ ALTER TABLE m_service
ADD CONSTRAINT fk_service FOREIGN KEY (oid) REFERENCES m_abstract_role;
ALTER TABLE m_system_configuration
ADD CONSTRAINT fk_system_configuration FOREIGN KEY (oid) REFERENCES m_object;
ALTER TABLE m_tag
ADD CONSTRAINT fk_tag FOREIGN KEY (oid) REFERENCES m_object;
ALTER TABLE m_trigger
ADD CONSTRAINT fk_trigger_owner FOREIGN KEY (owner_oid) REFERENCES m_object;
ALTER TABLE m_user
Expand Down
8 changes: 0 additions & 8 deletions config/sql/generic-old/h2-upgrade-4.5-4.6.sql

This file was deleted.

18 changes: 18 additions & 0 deletions config/sql/generic-old/h2-upgrade-4.6-4.7.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE TABLE m_tag (
name_norm VARCHAR(255),
name_orig VARCHAR(255),
uri VARCHAR(255),
oid VARCHAR(36) NOT NULL,
PRIMARY KEY (oid)
);

CREATE INDEX iTagNameOrig
ON m_tag (name_orig);
ALTER TABLE m_tag
ADD CONSTRAINT uc_tag_name UNIQUE (name_norm);

ALTER TABLE m_tag
ADD CONSTRAINT fk_tag FOREIGN KEY (oid) REFERENCES m_object;

-- WRITE CHANGES ABOVE ^^
UPDATE m_global_metadata SET value = '4.7' WHERE name = 'databaseSchemaVersion';
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package com.evolveum.midpoint.repo.sql;

import static com.evolveum.midpoint.prism.util.PrismTestUtil.displayCollection;

import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.AssertJUnit.*;

Expand All @@ -22,6 +24,8 @@
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;

import com.evolveum.midpoint.test.util.TestUtil;

import org.hibernate.Session;
import org.jetbrains.annotations.NotNull;
import org.springframework.test.annotation.DirtiesContext;
Expand Down Expand Up @@ -1641,6 +1645,86 @@ public void test544AddNameMetadataUsingReplace() throws Exception {
assertValueMetadataChannel(userAfter, UserType.F_NAME, channel);
}

@Test
public void test560AddModifySearchDeleteTag() throws Exception {
OperationResult result = createOperationResult();

String name = "tag1";
String uri = "http://test.com/tags#tag1";
String uri2 = "http://test.com/tags#tag2";
TagType tag = new TagType()
.name(name)
.uri(uri);

when("tag is added");
String oid = repositoryService.addObject(tag.asPrismObject(), null, result);

when("tag is retrieved by OID");
TagType byOid = repositoryService
.getObject(TagType.class, oid, null, result)
.asObjectable();

then("tag is the same");
displayDumpable("tag retrieved by OID", tag);
PrismAsserts.assertEquivalent(
"tag retrieved by OID differs from the original one", tag.asPrismObject(), byOid.asPrismObject());

when("searching by old URI");
List<PrismObject<TagType>> byUri = repositoryService.searchObjects(
TagType.class,
prismContext.queryFor(TagType.class)
.item(TagType.F_URI).eq(uri)
.build(),
null,
result);

then("there is one matching tag");
displayCollection("tags retrieved by URI", byUri);
assertThat(byUri).as("tags by original uri").hasSize(1);
PrismAsserts.assertEquivalent(
"tag retrieved by URI differs from the original one", tag.asPrismObject(), byUri.get(0));

when("modifying the URI");
repositoryService.modifyObject(
TagType.class,
oid,
prismContext.deltaFor(TagType.class)
.item(TagType.F_URI).replace(uri2)
.asItemDeltas(),
result);

and("searching by new URI");
List<PrismObject<TagType>> byUri2 = repositoryService.searchObjects(
TagType.class,
prismContext.queryFor(TagType.class)
.item(TagType.F_URI).eq(uri2)
.build(),
null,
result);

then("there is one matching tag");
displayCollection("tags retrieved by updated URI", byUri);
assertThat(byUri2).as("tags by original uri").hasSize(1);
tag.setUri(uri2);
PrismAsserts.assertEquivalent(
"tag retrieved by updated URI differs from the expected one", tag.asPrismObject(), byUri2.get(0));

when("tag is deleted");
repositoryService.deleteObject(TagType.class, oid, result);

then("all is OK");
result.computeStatus();
TestUtil.assertSuccess(result);

then("there is no tag by OID");
try {
repositoryService.getObject(TagType.class, oid, null, result);
fail("unexpected success");
} catch (ObjectNotFoundException e) {
displayExpectedException(e);
}
}

private void assertValueMetadataChannel(PrismObject<?> object, ItemName itemName, String expectedValue) {
Item<PrismValue, ItemDefinition> item = object.findItem(itemName);
assertThat(item).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,29 @@ public void test020QueryTagLong() throws Exception {
}
}

@Test
public void test021QueryTagUri() throws Exception {
Session session = open();
try {

ObjectQuery query = prismContext.queryFor(TagType.class)
.item(TagType.F_URI).eq(SchemaConstants.MODEL_POLICY_SITUATION_EXCLUSION_VIOLATION)
.build();
String real = getInterpretedQuery(session, TagType.class, query);

assertThat(real).isEqualToIgnoringWhitespace(""
+ "select\n"
+ " _t.oid,\n"
+ " _t.fullObject\n"
+ "from\n"
+ " RTag _t\n"
+ "where\n"
+ " _t.uri = :uri");
} finally {
close(session);
}
}

@Test
public void test029QueryAccountByNonExistingAttribute() throws Exception {
Session session = open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,30 @@ public void test700FullTextSearch() throws Exception {
true, 0);
}

@Test
public void test670TagByName() throws SchemaException {
ObjectQuery query = prismContext.queryFor(TagType.class)
.item(F_NAME).eqPoly("custom2").matchingOrig()
.build();
OperationResult result = new OperationResult("search");
List<PrismObject<TagType>> tags = repositoryService.searchObjects(TagType.class, query, null, result);
assertThatOperationResult(result).isSuccess();
assertThat(tags).as("tags found").hasSize(1);
assertThat(tags.get(0).getName().getOrig()).as("tag name").isEqualTo("custom2");
}

@Test
public void test675TagByUri() throws SchemaException {
ObjectQuery query = prismContext.queryFor(TagType.class)
.item(TagType.F_URI).eq(SchemaConstants.MODEL_POLICY_SITUATION_EXCLUSION_VIOLATION)
.build();
OperationResult result = new OperationResult("search");
List<PrismObject<TagType>> tags = repositoryService.searchObjects(TagType.class, query, null, result);
assertThatOperationResult(result).isSuccess();
assertThat(tags).as("tags found").hasSize(1);
assertThat(tags.get(0).getName().getOrig()).as("tag name").isEqualTo("exclusion-violation");
}

@Test
public void test710FulltextSearchNestedInOwnedBy() throws Exception {
given("query for assignments owned by role matching given fulltext search");
Expand Down
11 changes: 11 additions & 0 deletions repo/repo-sql-impl-test/src/test/resources/basic/objects-2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,15 @@
<ownerRef oid="00000000-0000-0000-0000-000000000002" type="c:UserType"><!-- administrator --></ownerRef>
</accessCertificationDefinition>

<tag>
<name>exclusion-violation</name>
<uri>http://midpoint.evolveum.com/xml/ns/public/model/policy/situation#exclusionViolation</uri>
</tag>
<tag>
<name>custom1</name>
<uri>http://test.com/tags#custom1</uri>
</tag>
<tag>
<name>custom2</name>
</tag>
</objects>
26 changes: 0 additions & 26 deletions repo/repo-sql-impl-test/src/test/resources/schema/extension.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -106,34 +106,8 @@
</xsd:appinfo>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="stringType" type="xsd:string"/>
<xsd:element name="floatType" type="xsd:float"/>
<xsd:element name="doubleType" type="xsd:double"/>
<xsd:element name="intType" type="xsd:int"/>
<xsd:element name="shortType" type="xsd:short"/>
<xsd:element name="longType" type="xsd:long"/>
<xsd:element name="dateType" type="xsd:dateTime"/>
<xsd:element name="decimalType" type="xsd:decimal"/>
<xsd:element name="locations" type="tns:LocationsType"/>
<xsd:element name="referenceType" type="c:ObjectReferenceType"/>
<xsd:element name="polyType" type="t:PolyStringType"/>
<xsd:element name="enumType" type="c:BeforeAfterType"/>
<xsd:element name="skipAutogeneration" type="xsd:boolean" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:appinfo>
<a:indexed>true</a:indexed>
<a:displayName>Do not generate</a:displayName>
<a:displayOrder>10</a:displayOrder>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="multivalued" type="xsd:string" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:appinfo>
<a:indexed>true</a:indexed>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
public class RTag extends RObject {

private RPolyString nameCopy;
private String uri;

@JaxbName(localPart = "name")
@AttributeOverrides({
Expand All @@ -50,12 +51,21 @@ public void setNameCopy(RPolyString nameCopy) {
this.nameCopy = nameCopy;
}

public String getUri() {
return uri;
}

public void setUri(String uri) {
this.uri = uri;
}

// dynamically called
public static void copyFromJAXB(
TagType jaxb, RTag repo, RepositoryContext repositoryContext,
IdGeneratorResult generatorResult) throws DtoTranslationException {
copyAssignmentHolderInformationFromJAXB(jaxb, repo, repositoryContext, generatorResult);

repo.setNameCopy(RPolyString.copyFromJAXB(jaxb.getName()));
repo.setUri(jaxb.getUri());
}
}

0 comments on commit a25c633

Please sign in to comment.