summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNode.java7
-rw-r--r--common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNodeKey.java11
-rw-r--r--common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQueryBuilder.java32
-rw-r--r--common/src/test/java/org/onap/so/client/aai/DSLQueryBuilderTest.java20
4 files changed, 58 insertions, 12 deletions
diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNode.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNode.java
index 7da1408f2d..1e4750d2cf 100644
--- a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNode.java
+++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNode.java
@@ -64,13 +64,14 @@ public class DSLNode implements QueryStep {
@Override
public String build() {
+ StringBuilder result = new StringBuilder(query);
if (output) {
- query.append("*");
+ result.append("*");
}
for (DSLNodeKey key : nodeKeys) {
- query.append(key.build());
+ result.append(key.build());
}
- return query.toString();
+ return result.toString();
}
}
diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNodeKey.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNodeKey.java
index 159bfb1c29..c40a3e6f96 100644
--- a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNodeKey.java
+++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNodeKey.java
@@ -49,11 +49,12 @@ public class DSLNodeKey implements QueryStep {
@Override
public String build() {
-
+ StringBuilder result = new StringBuilder(query);
+
if (not) {
- query.append(" !");
+ result.append(" !");
}
- query.append("('").append(keyName).append("', ");
+ result.append("('").append(keyName).append("', ");
List<String> temp = new ArrayList<>();
for (String item : values) {
if (item.equals("null")) {
@@ -64,8 +65,8 @@ public class DSLNodeKey implements QueryStep {
temp.add(String.format("'%s'", item));
}
}
- query.append(Joiner.on(", ").join(temp)).append(")");
+ result.append(Joiner.on(", ").join(temp)).append(")");
- return query.toString();
+ return result.toString();
}
}
diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQueryBuilder.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQueryBuilder.java
index 6554368b6d..3a47c38444 100644
--- a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQueryBuilder.java
+++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQueryBuilder.java
@@ -34,7 +34,7 @@ import com.google.common.base.Joiner;
public class DSLQueryBuilder<S, E> implements QueryStep {
private List<QueryStep> steps = new ArrayList<>();
-
+ private String suffix = "";
public DSLQueryBuilder() {
@@ -103,8 +103,9 @@ public class DSLQueryBuilder<S, E> implements QueryStep {
return to(__.node(name, key));
}
- public String limit(int limit) {
- return compile() + " LIMIT " + limit;
+ public DSLQueryBuilder<S, E> limit(int limit) {
+ suffix = " LIMIT " + limit;
+ return this;
}
@Override
@@ -112,8 +113,31 @@ public class DSLQueryBuilder<S, E> implements QueryStep {
return compile();
}
+ @Override
+ public String toString() {
+ return build();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (o != null) {
+ if (o instanceof QueryStep) {
+ return ((QueryStep)o).build().equals(this.build());
+ } else if (o instanceof String) {
+ return o.equals(this.build());
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public int hashCode() {
+
+ return build().hashCode();
+ }
+
private String compile() {
- return Joiner.on(" ").join(steps.stream().map(item -> item.build()).collect(Collectors.toList()));
+ return Joiner.on(" ").join(steps.stream().map(item -> item.build()).collect(Collectors.toList())) + suffix;
}
protected QueryStep getFirst() {
diff --git a/common/src/test/java/org/onap/so/client/aai/DSLQueryBuilderTest.java b/common/src/test/java/org/onap/so/client/aai/DSLQueryBuilderTest.java
index a156e3c158..a5bbc64eb9 100644
--- a/common/src/test/java/org/onap/so/client/aai/DSLQueryBuilderTest.java
+++ b/common/src/test/java/org/onap/so/client/aai/DSLQueryBuilderTest.java
@@ -21,6 +21,7 @@
package org.onap.so.client.aai;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.onap.so.client.graphinventory.entities.DSLNode;
@@ -89,4 +90,23 @@ public class DSLQueryBuilderTest {
builder.to(AAIObjectType.P_INTERFACE).to(AAIObjectType.SRIOV_PF, __.key("pf-pci-id", "my-id"));
assertEquals("pserver*('hostname', 'my-hostname') > p-interface > sriov-pf('pf-pci-id', 'my-id')", builder.build());
}
+
+ @Test
+ public void limitTest() {
+ DSLQueryBuilder<DSLNode, DSLNode> builder = new DSLQueryBuilder<>(new DSLNode(AAIObjectType.PSERVER,
+ __.key("hostname", "my-hostname")).output());
+
+ builder.to(AAIObjectType.P_INTERFACE).limit(2).to(AAIObjectType.SRIOV_PF, __.key("pf-pci-id", "my-id"));
+ assertEquals("pserver*('hostname', 'my-hostname') > p-interface > sriov-pf('pf-pci-id', 'my-id') LIMIT 2", builder.build());
+ }
+
+ @Test
+ public void equalsTest() {
+ DSLQueryBuilder<DSLNode, DSLNode> builder = new DSLQueryBuilder<>(new DSLNode(AAIObjectType.PSERVER,
+ __.key("hostname", "my-hostname")).output());
+
+ builder.to(AAIObjectType.P_INTERFACE).to(AAIObjectType.SRIOV_PF, __.key("pf-pci-id", "my-id"));
+ assertTrue(builder.equals("pserver*('hostname', 'my-hostname') > p-interface > sriov-pf('pf-pci-id', 'my-id')"));
+ assertTrue(builder.equals(builder));
+ }
}