diff options
author | 2019-03-02 00:17:46 +0100 | |
---|---|---|
committer | 2019-03-02 02:00:43 +0100 | |
commit | 75c0f1dd0b0b9700a3f235611131fdc500e10eb0 (patch) | |
tree | 8a09aac5249bd3ea6218a2c8f640c41975218390 | |
parent | c2bcf2a333a66d401c6afe0589c117f0986a5bd2 (diff) |
Improve tests
Improve tests and equals/hashcode of each model objects + fix Instant
bug of LoopLog
Issue-ID: CLAMP-300
Change-Id: If081f07864ba73449f8c6bcf8e23ede138204082
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
13 files changed, 479 insertions, 109 deletions
diff --git a/src/main/java/org/onap/clamp/clds/util/JsonUtils.java b/src/main/java/org/onap/clamp/clds/util/JsonUtils.java index f59864ac..6182598b 100644 --- a/src/main/java/org/onap/clamp/clds/util/JsonUtils.java +++ b/src/main/java/org/onap/clamp/clds/util/JsonUtils.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP CLAMP * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights + * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights * reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -32,6 +32,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -43,6 +44,8 @@ import java.util.stream.StreamSupport; import org.onap.clamp.clds.model.properties.AbstractModelElement; import org.onap.clamp.clds.service.SecureServicePermission; import org.onap.clamp.clds.service.SecureServicePermissionDeserializer; +import org.onap.clamp.dao.model.gson.converter.InstantDeserializer; +import org.onap.clamp.dao.model.gson.converter.InstantSerializer; /** * This class is used to access the GSON with restricted type access. @@ -56,12 +59,17 @@ public class JsonUtils { public static final Gson GSON = new GsonBuilder() .registerTypeAdapter(SecureServicePermission.class, new SecureServicePermissionDeserializer()).create(); + public static final Gson GSON_JPA_MODEL = new GsonBuilder() + .registerTypeAdapter(Instant.class, new InstantSerializer()) + .registerTypeAdapter(Instant.class, new InstantDeserializer()).setPrettyPrinting() + .excludeFieldsWithoutExposeAnnotation().create(); + private JsonUtils() { } /** - * Return the value field of the json node element that has a name field equals - * to the given name. + * typeAdapter Return the value field of the json node element that has a name + * field equals to the given name. */ public static String getStringValueByName(JsonElement jsonElement, String name) { String value = extractJsonValueFromElement(jsonElement, name).map(JsonUtils::extractStringValueFromElement) diff --git a/src/main/java/org/onap/clamp/dao/LoopLogRepository.java b/src/main/java/org/onap/clamp/dao/LoopLogRepository.java index 5f983b2a..199a25e8 100644 --- a/src/main/java/org/onap/clamp/dao/LoopLogRepository.java +++ b/src/main/java/org/onap/clamp/dao/LoopLogRepository.java @@ -28,6 +28,6 @@ import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @Repository -public interface LoopLogRepository extends CrudRepository<LoopLog, String> { +public interface LoopLogRepository extends CrudRepository<LoopLog, Long> { } diff --git a/src/main/java/org/onap/clamp/dao/model/Loop.java b/src/main/java/org/onap/clamp/dao/model/Loop.java index 3473b54f..c5946c9d 100644 --- a/src/main/java/org/onap/clamp/dao/model/Loop.java +++ b/src/main/java/org/onap/clamp/dao/model/Loop.java @@ -196,4 +196,30 @@ public class Loop implements Serializable { loopLogs.add(log); log.setLoop(this); } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Loop other = (Loop) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + } diff --git a/src/main/java/org/onap/clamp/dao/model/LoopLog.java b/src/main/java/org/onap/clamp/dao/model/LoopLog.java index 8f7fd68b..ba88bbee 100644 --- a/src/main/java/org/onap/clamp/dao/model/LoopLog.java +++ b/src/main/java/org/onap/clamp/dao/model/LoopLog.java @@ -27,6 +27,7 @@ import com.google.gson.annotations.Expose; import java.io.Serializable; import java.time.Instant; +import java.time.temporal.ChronoUnit; import javax.persistence.Column; import javax.persistence.Entity; @@ -40,6 +41,14 @@ import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; +/** + * + * This class holds the logs created by the Clamp Backend. The Instant is always + * rounded to the nearest second as the nano seconds can't be stored in the + * database. The logs can be therefore exposed to the UI or the client doing + * some GET Loop on the backend. + * + */ @Entity @Table(name = "loop_logs") public class LoopLog implements Serializable { @@ -51,7 +60,7 @@ public class LoopLog implements Serializable { @Expose @Id @GeneratedValue(strategy = GenerationType.AUTO) - private long id; + private Long id; @Expose @Column(name = "log_type", nullable = false) @@ -68,13 +77,13 @@ public class LoopLog implements Serializable { @Expose @Column(name = "log_instant", nullable = false) - private Instant logInstant = Instant.now(); + private Instant logInstant = Instant.now().truncatedTo(ChronoUnit.SECONDS); - public long getId() { + public Long getId() { return id; } - public void setId(long id) { + public void setId(Long id) { this.id = id; } @@ -107,7 +116,32 @@ public class LoopLog implements Serializable { } public void setLogInstant(Instant logInstant) { - this.logInstant = logInstant; + this.logInstant = logInstant.truncatedTo(ChronoUnit.SECONDS); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + LoopLog other = (LoopLog) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; } } diff --git a/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java b/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java index 7fa4a55d..6014d8d5 100644 --- a/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java +++ b/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java @@ -123,4 +123,29 @@ public class MicroServicePolicy implements Serializable { this.usedByLoops = usedBy; } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + MicroServicePolicy other = (MicroServicePolicy) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + } diff --git a/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java b/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java index d66fd940..23f75741 100644 --- a/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java +++ b/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java @@ -87,4 +87,29 @@ public class OperationalPolicy implements Serializable { this.configurationsJson = configurationsJson; } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + OperationalPolicy other = (OperationalPolicy) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + } diff --git a/src/main/java/org/onap/clamp/dao/model/gson/converter/InstantDeserializer.java b/src/main/java/org/onap/clamp/dao/model/gson/converter/InstantDeserializer.java new file mode 100644 index 00000000..2d63e55f --- /dev/null +++ b/src/main/java/org/onap/clamp/dao/model/gson/converter/InstantDeserializer.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.dao.model.gson.converter; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; + +import java.lang.reflect.Type; +import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; +import java.util.Locale; + +public class InstantDeserializer implements JsonDeserializer<Instant> { + + DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(Locale.US) + .withZone(ZoneId.systemDefault()); + + @Override + public Instant deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { + return Instant.parse(json.getAsString()); + + } +}
\ No newline at end of file diff --git a/src/main/java/org/onap/clamp/dao/model/gson/converter/InstantSerializer.java b/src/main/java/org/onap/clamp/dao/model/gson/converter/InstantSerializer.java new file mode 100644 index 00000000..cdb439e4 --- /dev/null +++ b/src/main/java/org/onap/clamp/dao/model/gson/converter/InstantSerializer.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.dao.model.gson.converter; + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +import java.lang.reflect.Type; +import java.time.Instant; +import java.time.format.DateTimeFormatter; + +public class InstantSerializer implements JsonSerializer<Instant> { + + @Override + public JsonElement serialize(Instant src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive(DateTimeFormatter.ISO_INSTANT.format(src)); + } +}
\ No newline at end of file diff --git a/src/test/java/org/onap/clamp/clds/it/CldsDaoItCase.java b/src/test/java/org/onap/clamp/clds/it/CldsDaoItCase.java index d4172c24..50858f79 100644 --- a/src/test/java/org/onap/clamp/clds/it/CldsDaoItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/CldsDaoItCase.java @@ -57,7 +57,7 @@ import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest public class CldsDaoItCase { - protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsDao.class); + protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsDaoItCase.class); @Autowired public CldsDao cldsDao; private String bpmnText; diff --git a/src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java b/src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java new file mode 100644 index 00000000..496d8c0e --- /dev/null +++ b/src/test/java/org/onap/clamp/dao/model/LoopToJsonTest.java @@ -0,0 +1,103 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.dao.model; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.gson.Gson; + +import java.util.Map; +import java.util.Random; + +import org.junit.Test; +import org.onap.clamp.clds.util.JsonUtils; + +public class LoopToJsonTest { + + private OperationalPolicy getOperationalPolicy(String configJson, String name) { + OperationalPolicy opPolicy = new OperationalPolicy(); + opPolicy.setName(name); + opPolicy.setConfigurationsJson(new Gson().fromJson(configJson, Map.class)); + return opPolicy; + } + + private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson, + String dcaeId, String dcaeUrl) { + Loop loop = new Loop(); + loop.setName(name); + loop.setSvgRepresentation(svgRepresentation); + loop.setBlueprint(blueprint); + loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, Map.class)); + loop.setLastComputedState(LoopState.DESIGN); + loop.setDcaeDeploymentId(dcaeId); + loop.setDcaeDeploymentStatusUrl(dcaeUrl); + return loop; + } + + private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca, + String jsonProperties, boolean shared) { + MicroServicePolicy µService = new MicroServicePolicy(); + µService.setJsonRepresentation(new Gson().fromJson(jsonRepresentation, Map.class)); + µService.setPolicyTosca(policyTosca); + µService.setProperties(new Gson().fromJson(jsonProperties, Map.class)); + µService.setShared(shared); + + µService.setName(name); + return µService; + } + + private LoopLog getLoopLog(LogType type, String message) { + LoopLog log = new LoopLog(); + log.setLogType(type); + log.setMessage(message); + log.setId(Long.valueOf(new Random().nextInt())); + return log; + } + + @Test + public void LoopGsonTest() { + Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}", + "123456789", "https://dcaetest.org"); + OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest"); + loopTest.addOperationalPolicy(opPolicy); + MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}", + "YamlContent", "{\"param1\":\"value1\"}", true); + loopTest.addMicroServicePolicy(microServicePolicy); + LoopLog loopLog = getLoopLog(LogType.INFO, "test message"); + loopTest.addLog(loopLog); + + String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest); + assertThat(jsonSerialized).isNotNull().isNotEmpty(); + System.out.println(jsonSerialized); + Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class); + assertNotNull(loopTestDeserialized); + assertThat(loopTestDeserialized).isEqualToComparingFieldByField(loopTest); + assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy); + assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy); + assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog); + assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog, + "loop"); + } +} diff --git a/src/test/java/org/onap/clamp/it/dao/model/LoopItCase.java b/src/test/java/org/onap/clamp/it/dao/model/LoopItCase.java deleted file mode 100644 index 06f4a7f8..00000000 --- a/src/test/java/org/onap/clamp/it/dao/model/LoopItCase.java +++ /dev/null @@ -1,98 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP CLAMP - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights - * reserved. - * ================================================================================ - * Licensed 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. - * ============LICENSE_END============================================ - * =================================================================== - * - */ - -package org.onap.clamp.it.dao.model; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -import java.util.Map; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.onap.clamp.clds.Application; -import org.onap.clamp.dao.LoopsRepository; -import org.onap.clamp.dao.model.LogType; -import org.onap.clamp.dao.model.Loop; -import org.onap.clamp.dao.model.LoopLog; -import org.onap.clamp.dao.model.LoopState; -import org.onap.clamp.dao.model.MicroServicePolicy; -import org.onap.clamp.dao.model.OperationalPolicy; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -@RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(classes = Application.class) -public class LoopItCase { - - @Autowired - private LoopsRepository loopRepository; - - @Test - public void DecodeEncodeTest() { - Loop loopTest = new Loop(); - loopTest.setName("ClosedLoopTest"); - loopTest.setSvgRepresentation("representation"); - loopTest.setBlueprint("blueprint"); - loopTest.setGlobalPropertiesJson(new Gson().fromJson("{\"testName\":\"testValue\"}", Map.class)); - loopTest.setLastComputedState(LoopState.DESIGN); - loopTest.setBlueprint("yaml"); - - OperationalPolicy opPolicy = new OperationalPolicy(); - opPolicy.setName("OpPolicyTest"); - opPolicy.setConfigurationsJson(new Gson().fromJson("{\"testname\":\"testvalue\"}", Map.class)); - opPolicy.setLoop(loopTest); - loopTest.addOperationalPolicy(opPolicy); - - MicroServicePolicy µService = new MicroServicePolicy(); - µService.setJsonRepresentation(new Gson().fromJson("{\"testrepresentation\":\"value\"}", Map.class)); - µService.setPolicyTosca("tosca"); - µService.setProperties(new Gson().fromJson("{\"testparam\":\"testvalue\"}", Map.class)); - µService.setShared(true); - - µService.setName("ConfigPolicyTest"); - loopTest.addMicroServicePolicy(µService); - - LoopLog log = new LoopLog(); - log.setLogType(LogType.INFO); - log.setMessage("test message"); - - loopTest.addLog(log); - - Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create(); - String json = gson.toJson(loopTest); - assertNotNull(json); - Loop loopTestDecoded = gson.fromJson(json, Loop.class); - assertNotNull(loopTestDecoded); - Loop loop = gson.fromJson(json, Loop.class); - assertNotNull(loop); - - Loop loopInDb = loopRepository.save(loopTest); - assertNotNull(loopInDb); - assertTrue(loopRepository.findById(loopInDb.getName()).get().getName().equals("ClosedLoopTest")); - } -} diff --git a/src/test/java/org/onap/clamp/it/dao/model/LoopRepositoriesItCase.java b/src/test/java/org/onap/clamp/it/dao/model/LoopRepositoriesItCase.java new file mode 100644 index 00000000..67ab247f --- /dev/null +++ b/src/test/java/org/onap/clamp/it/dao/model/LoopRepositoriesItCase.java @@ -0,0 +1,159 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END============================================ + * =================================================================== + * + */ + +package org.onap.clamp.it.dao.model; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import java.time.Instant; +import java.util.Map; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.clamp.clds.Application; +import org.onap.clamp.dao.LoopLogRepository; +import org.onap.clamp.dao.LoopsRepository; +import org.onap.clamp.dao.MicroServicePolicyRepository; +import org.onap.clamp.dao.OperationalPolicyRepository; +import org.onap.clamp.dao.model.LogType; +import org.onap.clamp.dao.model.Loop; +import org.onap.clamp.dao.model.LoopLog; +import org.onap.clamp.dao.model.LoopState; +import org.onap.clamp.dao.model.MicroServicePolicy; +import org.onap.clamp.dao.model.OperationalPolicy; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = Application.class) +public class LoopRepositoriesItCase { + + private Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create(); + + @Autowired + private LoopsRepository loopRepository; + + @Autowired + private MicroServicePolicyRepository microServicePolicyRepository; + + @Autowired + private OperationalPolicyRepository operationalPolicyRepository; + + @Autowired + private LoopLogRepository loopLogRepository; + + private OperationalPolicy getOperationalPolicy(String configJson, String name) { + OperationalPolicy opPolicy = new OperationalPolicy(); + opPolicy.setName(name); + opPolicy.setConfigurationsJson(new Gson().fromJson(configJson, Map.class)); + return opPolicy; + } + + private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson, + String dcaeId, String dcaeUrl) { + Loop loop = new Loop(); + loop.setName(name); + loop.setSvgRepresentation(svgRepresentation); + loop.setBlueprint(blueprint); + loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, Map.class)); + loop.setLastComputedState(LoopState.DESIGN); + loop.setDcaeDeploymentId(dcaeId); + loop.setDcaeDeploymentStatusUrl(dcaeUrl); + return loop; + } + + private MicroServicePolicy getMicroServicePolicy(String name, String jsonRepresentation, String policyTosca, + String jsonProperties, boolean shared) { + MicroServicePolicy µService = new MicroServicePolicy(); + µService.setJsonRepresentation(new Gson().fromJson(jsonRepresentation, Map.class)); + µService.setPolicyTosca(policyTosca); + µService.setProperties(new Gson().fromJson(jsonProperties, Map.class)); + µService.setShared(shared); + + µService.setName(name); + return µService; + } + + private LoopLog getLoopLog(LogType type, String message) { + LoopLog log = new LoopLog(); + log.setLogType(type); + log.setMessage(message); + return log; + } + + @Test + @Transactional + public void CrudTest() { + Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}", + "123456789", "https://dcaetest.org"); + OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest"); + loopTest.addOperationalPolicy(opPolicy); + MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "{\"configtype\":\"json\"}", + "YamlContent", "{\"param1\":\"value1\"}", true); + loopTest.addMicroServicePolicy(microServicePolicy); + LoopLog loopLog = getLoopLog(LogType.INFO, "test message"); + loopTest.addLog(loopLog); + + // Attemp to save into the database the entire loop + Loop loopInDb = loopRepository.save(loopTest); + assertThat(loopInDb).isNotNull(); + assertThat(loopInDb.getName()).isEqualTo("ControlLoopTest"); + // Now set the ID in the previous model so that we can compare the objects + loopLog.setId(((LoopLog) loopInDb.getLoopLogs().toArray()[0]).getId()); + + assertThat(loopInDb).isEqualToComparingFieldByField(loopTest); + assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(true); + assertThat(operationalPolicyRepository.existsById(opPolicy.getName())).isEqualTo(true); + assertThat(microServicePolicyRepository.existsById(microServicePolicy.getName())).isEqualTo(true); + assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(true); + + // Now attempt to read from database + Loop loopInDbRetrieved = loopRepository.findById(loopTest.getName()).get(); + assertThat(loopInDbRetrieved).isEqualToComparingFieldByField(loopTest); + assertThat((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).isEqualToComparingFieldByField(loopLog); + assertThat((OperationalPolicy) loopInDbRetrieved.getOperationalPolicies().toArray()[0]) + .isEqualToComparingFieldByField(opPolicy); + assertThat((MicroServicePolicy) loopInDbRetrieved.getMicroServicePolicies().toArray()[0]) + .isEqualToComparingFieldByField(microServicePolicy); + + // Attempt an update + ((LoopLog) loopInDbRetrieved.getLoopLogs().toArray()[0]).setLogInstant(Instant.now()); + loopRepository.save(loopInDbRetrieved); + Loop loopInDbRetrievedUpdated = loopRepository.findById(loopTest.getName()).get(); + assertThat((LoopLog) loopInDbRetrievedUpdated.getLoopLogs().toArray()[0]) + .isEqualToComparingFieldByField(loopInDbRetrieved.getLoopLogs().toArray()[0]); + + // Attempt to delete the object and check it has well been cascaded + loopRepository.delete(loopInDbRetrieved); + assertThat(loopRepository.existsById(loopTest.getName())).isEqualTo(false); + assertThat(operationalPolicyRepository.existsById(opPolicy.getName())).isEqualTo(false); + assertThat(microServicePolicyRepository.existsById(microServicePolicy.getName())).isEqualTo(false); + assertThat(loopLogRepository.existsById(loopLog.getId())).isEqualTo(false); + + } +} diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 16331ba0..49ed897a 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -104,7 +104,7 @@ spring.datasource.cldsdb.initialSize=0 spring.datasource.cldsdb.testOnBorrow=true
spring.datasource.cldsdb.ignoreExceptionOnPreLoad=true
-spring.jpa.properties.javax.persistence.schema-generation.database.action=none +spring.jpa.properties.javax.persistence.schema-generation.database.action=drop-and-create #spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata #spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create #spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=create.sql |