diff options
90 files changed, 5677 insertions, 5061 deletions
diff --git a/.gitignore b/.gitignore index b54ca2afa..517a3f3af 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ ui-react/build *~ auto-save-list tramp +venv
\ No newline at end of file @@ -9,13 +9,13 @@ The ONAP CLAMP platform abstracts the details of these systems under the concept At a higher level, CLAMP is about supporting and managing the broad operational life cycle of VNFs/VMs and ultimately ONAP components itself. It will offer the ability to design, test, deploy and update control loop automation - both closed and open. Automating these functions would represent a significant saving on operational costs compared to traditional methods. # Developer Contact -Owner: ONAP CLAMP Dev team +Owner: ONAP Policy/CLAMP Dev team Mailing List : onap-discuss@lists.onap.org -Add the following prefix to Subject on the mailing list : [CLAMP] +Add the following prefix to Subject on the mailing list : [POLICY] See here to subscribe : https://wiki.onap.org/display/DW/Mailing+Lists # Wiki -https://wiki.onap.org/display/DW/CLAMP+Project +https://wiki.onap.org/display/DW/Policy+Framework+Project # Build Jenkins Job: ${jenkins-joblink} diff --git a/common/src/main/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtils.java b/common/src/main/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtils.java index 4b0e41f23..4ebd0aaa8 100644 --- a/common/src/main/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtils.java +++ b/common/src/main/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtils.java @@ -38,8 +38,10 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; * */ public class CommonUtils { - private static final String POLICY_TYPE_ID = "policy_type_id"; - private static final String POLICY_ID = "policy_id"; + + private CommonUtils() { + throw new IllegalStateException("Utility class"); + } /** * Prepare participant updates map. diff --git a/common/src/test/java/org/onap/policy/clamp/controlloop/common/rest/CoderHttpMessageConverterTest.java b/common/src/test/java/org/onap/policy/clamp/controlloop/common/rest/CoderHttpMessageConverterTest.java new file mode 100644 index 000000000..58015ae3d --- /dev/null +++ b/common/src/test/java/org/onap/policy/clamp/controlloop/common/rest/CoderHttpMessageConverterTest.java @@ -0,0 +1,60 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.common.rest; + +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException; +import org.onap.policy.clamp.controlloop.common.startstop.CommonCommandLineArguments; +import org.springframework.http.HttpInputMessage; +import org.springframework.http.HttpOutputMessage; + +class CoderHttpMessageConverterTest { + + + @Test + void coderHttpMesageConverterTest() throws ControlLoopRuntimeException, IOException { + var y = new CoderHttpMesageConverter<>("yaml"); + var j = new CoderHttpMesageConverter<>("json"); + + assertTrue(y.supports(CommonCommandLineArguments.class)); + assertTrue(j.supports(CommonCommandLineArguments.class)); + var testInputStream = new ByteArrayInputStream("testdata".getBytes()); + HttpInputMessage input = Mockito.mock(HttpInputMessage.class); + Mockito.when(input.getBody()).thenReturn(testInputStream); + assertThrows(ControlLoopRuntimeException.class, () -> { + y.readInternal(RequestResponseLoggingFilterTest.class, input); + }); + + var testOutputStream = new ByteArrayOutputStream(); + HttpOutputMessage output = Mockito.mock(HttpOutputMessage.class); + Mockito.when(output.getBody()).thenReturn(testOutputStream); + assertThrows(ControlLoopRuntimeException.class, () -> { + j.writeInternal(String.class, output); + }); + } +} diff --git a/common/src/test/java/org/onap/policy/clamp/controlloop/common/rest/RequestResponseLoggingFilterTest.java b/common/src/test/java/org/onap/policy/clamp/controlloop/common/rest/RequestResponseLoggingFilterTest.java new file mode 100644 index 000000000..7621c9219 --- /dev/null +++ b/common/src/test/java/org/onap/policy/clamp/controlloop/common/rest/RequestResponseLoggingFilterTest.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * Modifications Copyright (C) 2021 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.common.rest; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +import java.io.IOException; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +class RequestResponseLoggingFilterTest { + + @Test + void initTest() throws IOException, ServletException { + var e = new RequestResponseLoggingFilter(); + var res = Mockito.mock(HttpServletResponse.class); + var req = Mockito.mock(HttpServletRequest.class); + var chain = Mockito.mock(FilterChain.class); + Mockito.when(req + .getHeader(RequestResponseLoggingFilter.REQUEST_ID_NAME)) + .thenReturn("id"); + + assertDoesNotThrow(() -> e.doFilter(req, res, chain)); + } +} diff --git a/common/src/test/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtilsTest.java b/common/src/test/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtilsTest.java new file mode 100644 index 000000000..52e86cf61 --- /dev/null +++ b/common/src/test/java/org/onap/policy/clamp/controlloop/common/utils/CommonUtilsTest.java @@ -0,0 +1,151 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.common.utils; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates; +import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; +import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; + +class CommonUtilsTest { + + private ToscaConceptIdentifier id = new ToscaConceptIdentifier("id", "1.0.0"); + private ToscaConceptIdentifier idNode = new ToscaConceptIdentifier( + "org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", "0.0.0"); + + @Test + void testCommonUtilsParticipantUpdate() { + var clElement = new ControlLoopElement(); + List<ParticipantUpdates> participantUpdates = new ArrayList<>(); + assertThat(participantUpdates).isEmpty(); + + CommonUtils.prepareParticipantUpdate(clElement, participantUpdates); + assertThat(participantUpdates).isNotEmpty(); + assertEquals(clElement, participantUpdates.get(0).getControlLoopElementList().get(0)); + + CommonUtils.prepareParticipantUpdate(clElement, participantUpdates); + assertNotEquals(id, participantUpdates.get(0).getParticipantId()); + + clElement.setParticipantId(id); + clElement.setParticipantType(id); + CommonUtils.prepareParticipantUpdate(clElement, participantUpdates); + assertEquals(id, participantUpdates.get(1).getParticipantId()); + } + + @Test + void testCommonUtilsServiceTemplate() { + var clElement = new ControlLoopElement(); + var toscaServiceTemplate = getDummyToscaServiceTemplate(); + CommonUtils.setServiceTemplatePolicyInfo(clElement, toscaServiceTemplate); + assertEquals(getDummyToscaDataTypeMap(), clElement.getToscaServiceTemplateFragment().getDataTypes()); + } + + @Test + void testCommonUtilsDefinitionUpdate() { + var toscaServiceTemplate = getDummyToscaServiceTemplate(); + List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>(); + assertThat(participantDefinitionUpdates).isEmpty(); + + checkParticipantDefinitionUpdate(toscaServiceTemplate, participantDefinitionUpdates); + assertThat(participantDefinitionUpdates).isNotEmpty(); + assertEquals(id, participantDefinitionUpdates.get(0).getParticipantType()); + + checkParticipantDefinitionUpdate(toscaServiceTemplate, participantDefinitionUpdates); + assertEquals(idNode, participantDefinitionUpdates.get(0) + .getControlLoopElementDefinitionList().get(0) + .getClElementDefinitionId()); + } + + private ToscaServiceTemplate getDummyToscaServiceTemplate() { + var toscaServiceTemplate = new ToscaServiceTemplate(); + var policyTypes = getDummyPolicyTypesMap(); + toscaServiceTemplate.setPolicyTypes(policyTypes); + + var dataTypes = getDummyToscaDataTypeMap(); + dataTypes.put("onap.datatypes.ToscaConceptIdentifier", new ToscaDataType()); + toscaServiceTemplate.setDataTypes(dataTypes); + + var toscaTopologyTemplate = new ToscaTopologyTemplate(); + Map<String, ToscaPolicy> policy = new HashMap<>(); + toscaTopologyTemplate.setPolicies(List.of(policy)); + var nodeTemplates = getDummyNodeTemplates(); + toscaTopologyTemplate.setNodeTemplates(nodeTemplates); + + toscaServiceTemplate.setToscaTopologyTemplate(toscaTopologyTemplate); + toscaServiceTemplate.setDerivedFrom("tosca.nodetypes.Root"); + toscaServiceTemplate.setDescription("description"); + toscaServiceTemplate.setMetadata(null); + toscaServiceTemplate.setName("name"); + toscaServiceTemplate.setToscaDefinitionsVersion("1.0.0"); + toscaServiceTemplate.setVersion("1.0.1"); + return toscaServiceTemplate; + } + + private Map<String, ToscaPolicyType> getDummyPolicyTypesMap() { + Map<String, ToscaPolicyType> policyTypes = new HashMap<>(); + policyTypes.put("onap.policies.Match", new ToscaPolicyType()); + return policyTypes; + } + + private Map<String, ToscaDataType> getDummyToscaDataTypeMap() { + Map<String, ToscaDataType> dataTypes = new HashMap<>(); + dataTypes.put("onap.datatypes.ToscaConceptIdentifier", new ToscaDataType()); + return dataTypes; + } + + private Map<String, ToscaNodeTemplate> getDummyNodeTemplates() { + Map<String, ToscaNodeTemplate> nodeTemplates = new HashMap<>(); + var nodeTemplate = new ToscaNodeTemplate(); + nodeTemplate.setType("org.onap.policy.clamp.controlloop.ControlLoopElement"); + nodeTemplates.put("org.onap.dcae.controlloop.DCAEMicroserviceControlLoopParticipant", nodeTemplate); + return nodeTemplates; + } + + private void checkParticipantDefinitionUpdate( + ToscaServiceTemplate toscaServiceTemplate, + List<ParticipantDefinition> participantDefinitionUpdates) { + + for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate() + .getNodeTemplates().entrySet()) { + if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), + toscaServiceTemplate)) { + CommonUtils.prepareParticipantDefinitionUpdate(id, toscaInputEntry.getKey(), + toscaInputEntry.getValue(), participantDefinitionUpdates, null); + } + } + } +} diff --git a/models/pom.xml b/models/pom.xml index c534a16f4..4adf64d2a 100644 --- a/models/pom.xml +++ b/models/pom.xml @@ -32,4 +32,23 @@ <artifactId>policy-clamp-models</artifactId> <name>${project.artifactId}</name> + + <dependencies> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-data-jpa</artifactId> + <version>${version.springboot}</version> + <exclusions> + <exclusion> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-core</artifactId> + </exclusion> + <exclusion> + <groupId>org.hibernate.common</groupId> + <artifactId>common-annotations</artifactId> + </exclusion> + </exclusions> + </dependency> + </dependencies> + </project> diff --git a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ClElementStatisticsProvider.java b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ClElementStatisticsProvider.java index e5b062bc1..bdb0d4d9b 100644 --- a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ClElementStatisticsProvider.java +++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ClElementStatisticsProvider.java @@ -21,19 +21,20 @@ package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider; import java.time.Instant; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import javax.ws.rs.core.Response.Status; +import lombok.AllArgsConstructor; import lombok.NonNull; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaClElementStatistics; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ClElementStatisticsRepository; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfReferenceTimestampKey; import org.onap.policy.models.dao.PfFilterParameters; -import org.onap.policy.models.provider.PolicyModelsProviderParameters; -import org.onap.policy.models.provider.impl.AbstractModelsProvider; import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; /** * This class provides the provision of information on control loop element statistics in the database to callers. @@ -41,46 +42,34 @@ import org.springframework.stereotype.Component; * @author Ramesh Murugan Iyer (ramesh.murugan.iyer@est.tech) */ @Component -public class ClElementStatisticsProvider extends AbstractModelsProvider { +@Transactional +@AllArgsConstructor +public class ClElementStatisticsProvider { - /** - * Create a provider for control loop element statistics. - * - * @param parameters the parameters for database access - * @throws PfModelException on initiation errors - */ - public ClElementStatisticsProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException { - super(parameters); - this.init(); - } + private ClElementStatisticsRepository clElementStatisticsRepository; /** * Creates control loop element statistics. * * @param clElementStatisticsList a specification of the CL element statistics to create * @return the clElement statistics created - * @throws PfModelException on errors creating clElement statistics + * @throws PfModelException on initiation errors */ public List<ClElementStatistics> createClElementStatistics( @NonNull final List<ClElementStatistics> clElementStatisticsList) throws PfModelException { - List<JpaClElementStatistics> jpaClElementStatisticsList = ProviderUtils.getJpaAndValidate( - clElementStatisticsList, JpaClElementStatistics::new, "control loop element statistics"); + try { + var jpaClElementStatisticsList = ProviderUtils.getJpaAndValidate(clElementStatisticsList, + JpaClElementStatistics::new, "control loop element statistics"); - jpaClElementStatisticsList.forEach(jpaClElementStatistics -> getPfDao().create(jpaClElementStatistics)); + var jpaClElementStatisticsSaved = clElementStatisticsRepository.saveAll(jpaClElementStatisticsList); - // Return the created control loop element statistics - List<ClElementStatistics> elementStatistics = new ArrayList<>(clElementStatisticsList.size()); - - for (ClElementStatistics clElementStat : clElementStatisticsList) { - var jpaClElementStatistics = getPfDao().get(JpaClElementStatistics.class, - new PfReferenceTimestampKey(clElementStat.getParticipantId().getName(), - clElementStat.getParticipantId().getVersion(), clElementStat.getId().toString(), - clElementStat.getTimeStamp())); - elementStatistics.add(jpaClElementStatistics.toAuthorative()); + // Return the saved control loop element statistics + return asClElementStatisticsList(jpaClElementStatisticsSaved); + } catch (IllegalArgumentException e) { + throw new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error in save control loop element statistics", + e); } - - return elementStatistics; } /** @@ -101,22 +90,17 @@ public class ClElementStatisticsProvider extends AbstractModelsProvider { * @param id of the control loop element * @param timestamp timestamp of the statistics * @return the clElement statistics found - * @throws PfModelException on errors getting clElement statistics */ + @Transactional(readOnly = true) public List<ClElementStatistics> getClElementStatistics(final String name, final String version, final String id, - final Instant timestamp) throws PfModelException { - List<ClElementStatistics> clElementStatistics = new ArrayList<>(1); + final Instant timestamp) { if (name != null && version != null && timestamp != null && id != null) { - clElementStatistics.add(getPfDao() - .get(JpaClElementStatistics.class, new PfReferenceTimestampKey(name, version, id, timestamp)) - .toAuthorative()); - return clElementStatistics; + return asClElementStatisticsList(clElementStatisticsRepository + .findAllById(List.of(new PfReferenceTimestampKey(name, version, id, timestamp)))); } else if (name != null) { - clElementStatistics.addAll(getFilteredClElementStatistics(name, version, null, null, null, "DESC", 0)); - } else { - clElementStatistics.addAll(asClElementStatisticsList(getPfDao().getAll(JpaClElementStatistics.class))); + return getFilteredClElementStatistics(name, version, null, null, null, "DESC", 0); } - return clElementStatistics; + return asClElementStatisticsList(clElementStatisticsRepository.findAll()); } /** @@ -130,8 +114,8 @@ public class ClElementStatisticsProvider extends AbstractModelsProvider { * @param getRecordNum Total query count from database * @param filterMap the filters to apply to the get operation * @return the clElement statistics found - * @throws PfModelException on errors getting policies */ + @Transactional(readOnly = true) public List<ClElementStatistics> getFilteredClElementStatistics(final String name, final String version, final Instant startTimeStamp, final Instant endTimeStamp, Map<String, Object> filterMap, final String sortOrder, final int getRecordNum) { @@ -148,6 +132,7 @@ public class ClElementStatisticsProvider extends AbstractModelsProvider { .recordNum(getRecordNum) .build(); // @formatter:on - return asClElementStatisticsList(getPfDao().getFiltered(JpaClElementStatistics.class, filterParams)); + return asClElementStatisticsList( + clElementStatisticsRepository.getFiltered(JpaClElementStatistics.class, filterParams)); } } diff --git a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantStatisticsProvider.java b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantStatisticsProvider.java index fa27a41c2..06c7a11f8 100644 --- a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantStatisticsProvider.java +++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantStatisticsProvider.java @@ -21,36 +21,30 @@ package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider; import java.time.Instant; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import javax.ws.rs.core.Response.Status; +import lombok.AllArgsConstructor; import lombok.NonNull; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipantStatistics; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ParticipantStatisticsRepository; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfTimestampKey; import org.onap.policy.models.dao.PfFilterParameters; -import org.onap.policy.models.provider.PolicyModelsProviderParameters; -import org.onap.policy.models.provider.impl.AbstractModelsProvider; import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; /** * This class provides the provision of information on participant statistics in the database to callers. */ @Component -public class ParticipantStatisticsProvider extends AbstractModelsProvider { +@Transactional +@AllArgsConstructor +public class ParticipantStatisticsProvider { - /** - * Create a provider for control loops statistics. - * - * @param parameters the parameters for database access - * @throws PfModelException on initiation errors - */ - public ParticipantStatisticsProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException { - super(parameters); - this.init(); - } + private ParticipantStatisticsRepository participantStatisticsRepository; /** * Get Participant statistics. @@ -59,20 +53,18 @@ public class ParticipantStatisticsProvider extends AbstractModelsProvider { * @param version the version of the participant statistics to get, null to get all stats for a name * @param timestamp the time stamp for the stats to get * @return the participant statistics found - * @throws PfModelException on errors getting participant statistics */ + @Transactional(readOnly = true) public List<ParticipantStatistics> getParticipantStatistics(final String name, final String version, - final Instant timestamp) throws PfModelException { + final Instant timestamp) { if (name != null && version != null && timestamp != null) { - List<ParticipantStatistics> participantStatistics = new ArrayList<>(1); - participantStatistics.add(getPfDao() - .get(JpaParticipantStatistics.class, new PfTimestampKey(name, version, timestamp)).toAuthorative()); - return participantStatistics; + return asParticipantStatisticsList( + participantStatisticsRepository.findAllById(List.of(new PfTimestampKey(name, version, timestamp)))); } else if (name != null) { return getFilteredParticipantStatistics(name, version, timestamp, null, null, "DESC", 0); } else { - return asParticipantStatisticsList(getPfDao().getAll(JpaParticipantStatistics.class)); + return asParticipantStatisticsList(participantStatisticsRepository.findAll()); } } @@ -87,8 +79,8 @@ public class ParticipantStatisticsProvider extends AbstractModelsProvider { * @param getRecordNum Total query count from database * @param filterMap the filters to apply to the get operation * @return the participant statistics found - * @throws PfModelException on errors getting policies */ + @Transactional(readOnly = true) public List<ParticipantStatistics> getFilteredParticipantStatistics(final String name, final String version, final Instant startTimeStamp, final Instant endTimeStamp, Map<String, Object> filterMap, final String sortOrder, final int getRecordNum) { @@ -106,7 +98,8 @@ public class ParticipantStatisticsProvider extends AbstractModelsProvider { .build(); // @formatter:on - return asParticipantStatisticsList(getPfDao().getFiltered(JpaParticipantStatistics.class, filterParams)); + return asParticipantStatisticsList( + participantStatisticsRepository.getFiltered(JpaParticipantStatistics.class, filterParams)); } /** @@ -119,23 +112,17 @@ public class ParticipantStatisticsProvider extends AbstractModelsProvider { public List<ParticipantStatistics> createParticipantStatistics( @NonNull final List<ParticipantStatistics> participantStatisticsList) throws PfModelException { - List<JpaParticipantStatistics> jpaParticipantStatisticsList = ProviderUtils - .getJpaAndValidate(participantStatisticsList, JpaParticipantStatistics::new, "Participant Statistics"); - - jpaParticipantStatisticsList.forEach(jpaParticipantStatistics -> getPfDao().update(jpaParticipantStatistics)); + try { + var jpaParticipantStatisticsList = ProviderUtils.getJpaAndValidate(participantStatisticsList, + JpaParticipantStatistics::new, "Participant Statistics"); - // Return the created participant statistics - List<ParticipantStatistics> participantStatistics = new ArrayList<>(participantStatisticsList.size()); + var jpaParticipantStatisticsSaved = participantStatisticsRepository.saveAll(jpaParticipantStatisticsList); - for (ParticipantStatistics participantStatisticsItem : participantStatisticsList) { - var jpaParticipantStatistics = getPfDao().get(JpaParticipantStatistics.class, - new PfTimestampKey(participantStatisticsItem.getParticipantId().getName(), - participantStatisticsItem.getParticipantId().getVersion(), - participantStatisticsItem.getTimeStamp())); - participantStatistics.add(jpaParticipantStatistics.toAuthorative()); + // Return the saved participant statistics + return asParticipantStatisticsList(jpaParticipantStatisticsSaved); + } catch (IllegalArgumentException e) { + throw new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error in save participant statistics", e); } - - return participantStatistics; } /** diff --git a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/ClElementStatisticsRepository.java b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/ClElementStatisticsRepository.java new file mode 100644 index 000000000..1e07a4fe6 --- /dev/null +++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/ClElementStatisticsRepository.java @@ -0,0 +1,32 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository; + +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaClElementStatistics; +import org.onap.policy.models.base.PfReferenceTimestampKey; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ClElementStatisticsRepository + extends JpaRepository<JpaClElementStatistics, PfReferenceTimestampKey>, FilterRepository { + +} diff --git a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/FilterRepository.java b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/FilterRepository.java new file mode 100644 index 000000000..ce1f3d8c3 --- /dev/null +++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/FilterRepository.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository; + +import java.util.List; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.dao.PfFilterParametersIntfc; + +public interface FilterRepository { + + /** + * Get an object from the database, referred to by concept key. + * + * @param <T> the type of the object to get, a subclass of {@link PfConcept} + * @param someClass the class of the object to get, a subclass of {@link PfConcept}, if name is null, all concepts + * of type T are returned, if name is not null and version is null, all versions of that concept matching the + * name are returned. + * @param filterParams filter parameters + * @return the objects that was retrieved from the database + */ + <T extends PfConcept> List<T> getFiltered(Class<T> someClass, PfFilterParametersIntfc filterParams); + + /** + * Get an object from the database, referred to by concept key. + * + * @param <T> the type of the object to get, a subclass of {@link PfConcept} + * @param someClass the class of the object to get, a subclass of {@link PfConcept}, if name is null, all concepts + * of type T are returned, if name is not null and version is null, all versions of that concept matching the + * name are returned. + * @param name the name of the object to get, null returns all objects + * @param version the version the object to get, null returns all objects for a specified name + * @return the objects that was retrieved from the database + */ + <T extends PfConcept> List<T> getFiltered(Class<T> someClass, String name, String version); +} diff --git a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/FilterRepositoryImpl.java b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/FilterRepositoryImpl.java new file mode 100644 index 000000000..0dc8fc373 --- /dev/null +++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/FilterRepositoryImpl.java @@ -0,0 +1,51 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository; + +import java.util.List; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.dao.PfFilterParametersIntfc; +import org.onap.policy.models.dao.impl.ProxyDao; +import org.springframework.stereotype.Repository; + +@Repository +public class FilterRepositoryImpl implements FilterRepository { + + @PersistenceContext + private EntityManager entityManager; + + protected PfDao getPfDao() { + return new ProxyDao(entityManager); + } + + @Override + public <T extends PfConcept> List<T> getFiltered(Class<T> someClass, PfFilterParametersIntfc filterParams) { + return getPfDao().getFiltered(someClass, filterParams); + } + + @Override + public <T extends PfConcept> List<T> getFiltered(Class<T> someClass, String name, String version) { + return getPfDao().getFiltered(someClass, name, version); + } +} diff --git a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/ParticipantStatisticsRepository.java b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/ParticipantStatisticsRepository.java new file mode 100644 index 000000000..6fecd6143 --- /dev/null +++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/ParticipantStatisticsRepository.java @@ -0,0 +1,32 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository; + +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipantStatistics; +import org.onap.policy.models.base.PfTimestampKey; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ParticipantStatisticsRepository + extends JpaRepository<JpaParticipantStatistics, PfTimestampKey>, FilterRepository { + +} diff --git a/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ClElementStatisticsProviderTest.java b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ClElementStatisticsProviderTest.java index a9ef9148f..cf4136d3e 100644 --- a/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ClElementStatisticsProviderTest.java +++ b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ClElementStatisticsProviderTest.java @@ -23,19 +23,23 @@ package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provide import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.time.Instant; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaClElementStatistics; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ClElementStatisticsRepository; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; -import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; class ClElementStatisticsProviderTest { @@ -43,9 +47,6 @@ class ClElementStatisticsProviderTest { private static final Coder CODER = new StandardCoder(); private static final String CL_ELEMENT_STATS_JSON = "src/test/resources/providers/TestClElementStatistics.json"; - private static AtomicInteger dbNameCounter = new AtomicInteger(); - - private PolicyModelsProviderParameters parameters; private ClElementStatisticsProvider clElementStatisticsProvider; private ClElementStatisticsList inputClElementStats; private String originalJson = ResourceUtils.getResourceAsString(CL_ELEMENT_STATS_JSON); @@ -58,22 +59,23 @@ class ClElementStatisticsProviderTest { @BeforeEach void beforeSetupDao() throws Exception { - parameters = new PolicyModelsProviderParameters(); - parameters.setDatabaseDriver("org.h2.Driver"); - parameters.setName("PolicyProviderParameterGroup"); - parameters.setImplementation("org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl"); - parameters.setDatabaseUrl("jdbc:h2:mem:clElementTestDb" + dbNameCounter.getAndIncrement()); - parameters.setDatabaseUser("policy"); - parameters.setDatabasePassword("P01icY"); - parameters.setPersistenceUnit("ToscaConceptTest"); - - clElementStatisticsProvider = new ClElementStatisticsProvider(parameters); inputClElementStats = CODER.decode(originalJson, ClElementStatisticsList.class); - } + var clElementStatisticsRepository = mock(ClElementStatisticsRepository.class); + + var jpaClElementStatisticsList = ProviderUtils.getJpaAndValidate(inputClElementStats.getClElementStatistics(), + JpaClElementStatistics::new, "control loop element statistics"); + + for (var clElementStat : jpaClElementStatisticsList) { + when(clElementStatisticsRepository.findAllById(List.of(clElementStat.getKey()))) + .thenReturn(List.of(clElementStat)); + } + + when(clElementStatisticsRepository.saveAll(anyList())).thenReturn(jpaClElementStatisticsList); + + when(clElementStatisticsRepository.getFiltered(eq(JpaClElementStatistics.class), any())) + .thenReturn(List.of(jpaClElementStatisticsList.get(0))); - @AfterEach - void teardown() { - clElementStatisticsProvider.close(); + clElementStatisticsProvider = new ClElementStatisticsProvider(clElementStatisticsRepository); } @Test @@ -84,10 +86,10 @@ class ClElementStatisticsProviderTest { ClElementStatisticsList createdClElementStats = new ClElementStatisticsList(); createdClElementStats.setClElementStatistics( - clElementStatisticsProvider.createClElementStatistics(inputClElementStats.getClElementStatistics())); + clElementStatisticsProvider.createClElementStatistics(inputClElementStats.getClElementStatistics())); assertEquals(inputClElementStats.toString().replaceAll("\\s+", ""), - createdClElementStats.toString().replaceAll("\\s+", "")); + createdClElementStats.toString().replaceAll("\\s+", "")); } @Test @@ -104,9 +106,9 @@ class ClElementStatisticsProviderTest { Instant instant = inputClElementStats.getClElementStatistics().get(0).getTimeStamp(); String id = inputClElementStats.getClElementStatistics().get(0).getId().toString(); assertEquals(1, clElementStatisticsProvider - .getClElementStatistics(identifier.getName(), identifier.getVersion(), id, instant).size()); + .getClElementStatistics(identifier.getName(), identifier.getVersion(), id, instant).size()); assertEquals(1, clElementStatisticsProvider - .getFilteredClElementStatistics("name2", "1.0.1", null, null, null, "DESC", 1).size()); + .getFilteredClElementStatistics("name2", "1.0.1", null, null, null, "DESC", 1).size()); } } diff --git a/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantStatisticsProviderTest.java b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantStatisticsProviderTest.java index 8191dd618..c6e1f817d 100644 --- a/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantStatisticsProviderTest.java +++ b/models/src/test/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantStatisticsProviderTest.java @@ -23,19 +23,23 @@ package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provide import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.time.Instant; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipantStatistics; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ParticipantStatisticsRepository; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; -import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; class ParticipantStatisticsProviderTest { @@ -44,32 +48,29 @@ class ParticipantStatisticsProviderTest { private static final Coder CODER = new StandardCoder(); private static final String PARTICIPANT_STATS_JSON = "src/test/resources/providers/TestParticipantStatistics.json"; - private static AtomicInteger dbNameCounter = new AtomicInteger(); - - private PolicyModelsProviderParameters parameters; private ParticipantStatisticsProvider participantStatisticsProvider; private ParticipantStatisticsList inputParticipantStatistics; private String originalJson = ResourceUtils.getResourceAsString(PARTICIPANT_STATS_JSON); @BeforeEach void beforeSetupDao() throws Exception { + var participantStatisticsRepository = mock(ParticipantStatisticsRepository.class); + participantStatisticsProvider = new ParticipantStatisticsProvider(participantStatisticsRepository); + inputParticipantStatistics = CODER.decode(originalJson, ParticipantStatisticsList.class); - parameters = new PolicyModelsProviderParameters(); - parameters.setDatabaseDriver("org.h2.Driver"); - parameters.setName("PolicyProviderParameterGroup"); - parameters.setImplementation("org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl"); - parameters.setDatabaseUrl("jdbc:h2:mem:participantStatisticsProviderTestDb" + dbNameCounter.getAndIncrement()); - parameters.setDatabaseUser("policy"); - parameters.setDatabasePassword("P01icY"); - parameters.setPersistenceUnit("ToscaConceptTest"); + var jpaParticipantStatisticsList = + ProviderUtils.getJpaAndValidate(inputParticipantStatistics.getStatisticsList(), + JpaParticipantStatistics::new, "Participant Statistics"); - participantStatisticsProvider = new ParticipantStatisticsProvider(parameters); - inputParticipantStatistics = CODER.decode(originalJson, ParticipantStatisticsList.class); - } + for (var participantStat : jpaParticipantStatisticsList) { + when(participantStatisticsRepository.findAllById(List.of(participantStat.getKey()))) + .thenReturn(List.of(participantStat)); + } + + when(participantStatisticsRepository.getFiltered(eq(JpaParticipantStatistics.class), any())) + .thenReturn(List.of(jpaParticipantStatisticsList.get(0))); - @AfterEach - void teardown() { - participantStatisticsProvider.close(); + when(participantStatisticsRepository.saveAll(anyList())).thenReturn(jpaParticipantStatisticsList); } @Test diff --git a/packages/policy-clamp-tarball/src/main/resources/etc/ClRuntimeParameters.yaml b/packages/policy-clamp-tarball/src/main/resources/etc/ClRuntimeParameters.yaml index 1972f8773..1a57f2347 100644 --- a/packages/policy-clamp-tarball/src/main/resources/etc/ClRuntimeParameters.yaml +++ b/packages/policy-clamp-tarball/src/main/resources/etc/ClRuntimeParameters.yaml @@ -6,6 +6,16 @@ spring: http: converters: preferred-json-mapper: gson + datasource: + url: jdbc:mariadb://${mariadb.host:mariadb}:${mariadb.port:3306}/controlloop + driverClassName: org.mariadb.jdbc.Driver + username: policy_user + password: policy_user + hikari: + connectionTimeout: 30000 + idleTimeout: 600000 + maxLifetime: 1800000 + maximumPoolSize: 10 security: enable-csrf: false @@ -28,6 +38,7 @@ runtime: updateParameters: maxRetryCount: 3 maxWaitMs: 20000 + databasePlatform: org.eclipse.persistence.platform.database.MySQLPlatform databaseProviderParameters: name: PolicyProviderParameterGroup implementation: org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl diff --git a/packages/policy-clamp-tarball/src/main/resources/etc/PolicyParticipantParameters.yaml b/packages/policy-clamp-tarball/src/main/resources/etc/PolicyParticipantParameters.yaml index b735c1629..b357ae424 100644 --- a/packages/policy-clamp-tarball/src/main/resources/etc/PolicyParticipantParameters.yaml +++ b/packages/policy-clamp-tarball/src/main/resources/etc/PolicyParticipantParameters.yaml @@ -13,7 +13,7 @@ participant: port: 6969 userName: healthcheck password: zb!XztG34 - https: true + useHttps: true allowSelfSignedCerts: true policyPapParameters: clientName: pap @@ -21,7 +21,7 @@ participant: port: 6969 userName: healthcheck password: zb!XztG34 - https: true + useHttps: true allowSelfSignedCerts: true intermediaryParameters: reportingTimeIntervalMs: 120000 diff --git a/participant/participant-impl/participant-impl-policy/src/main/resources/config/application.yaml b/participant/participant-impl/participant-impl-policy/src/main/resources/config/application.yaml index a2236226e..5839aae66 100644 --- a/participant/participant-impl/participant-impl-policy/src/main/resources/config/application.yaml +++ b/participant/participant-impl/participant-impl-policy/src/main/resources/config/application.yaml @@ -13,7 +13,7 @@ participant: port: 6969 userName: healthcheck password: zb!XztG34 - https: true + useHttps: true allowSelfSignedCerts: true policyPapParameters: clientName: pap @@ -21,7 +21,7 @@ participant: port: 6969 userName: healthcheck password: zb!XztG34 - https: true + useHttps: true allowSelfSignedCerts: true intermediaryParameters: reportingTimeIntervalMs: 120000 diff --git a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java index c9848248e..9f6a31e28 100644 --- a/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java +++ b/participant/participant-impl/participant-impl-simulator/src/test/java/org/onap/policy/clamp/controlloop/participant/simulator/main/rest/TestListenerUtils.java @@ -191,7 +191,6 @@ public final class TestListenerUtils { ToscaServiceTemplate toscaServiceTemplate = testControlLoopRead(); // Add policies to the toscaServiceTemplate - List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>(); for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate() .getNodeTemplates().entrySet()) { diff --git a/participant/pom.xml b/participant/pom.xml index 72701f1a4..e679bb227 100644 --- a/participant/pom.xml +++ b/participant/pom.xml @@ -93,6 +93,12 @@ <groupId>org.onap.policy.clamp</groupId> <artifactId>policy-clamp-models</artifactId> <version>${project.version}</version> + <exclusions> + <exclusion> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-data-jpa</artifactId> + </exclusion> + </exclusions> </dependency> <!-- Actuator dependencies for participants added here to avoid http security conflicts in clamp/runtime --> <dependency> diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/Application.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/Application.java index a9b45c589..64d1393ac 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/Application.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/Application.java @@ -22,16 +22,21 @@ package org.onap.policy.clamp.controlloop.runtime; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.context.properties.ConfigurationPropertiesScan; import org.springframework.context.annotation.ComponentScan; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling @SpringBootApplication +@EnableJpaRepositories({"org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository"}) @ComponentScan({"org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider", "org.onap.policy.clamp.controlloop.runtime", "org.onap.policy.clamp.controlloop.common.rest"}) @ConfigurationPropertiesScan("org.onap.policy.clamp.controlloop.runtime.main.parameters") +@EntityScan({"org.onap.policy.models.tosca.simple.concepts", + "org.onap.policy.clamp.controlloop.models.controlloop.concepts"}) public class Application { public static void main(String[] args) { diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/config/JpaConfiguration.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/config/JpaConfiguration.java new file mode 100644 index 000000000..290b74e3a --- /dev/null +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/config/JpaConfiguration.java @@ -0,0 +1,119 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.controlloop.runtime.config; + +import java.util.HashMap; +import java.util.Map; +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; +import org.eclipse.persistence.config.BatchWriting; +import org.eclipse.persistence.config.PersistenceUnitProperties; +import org.eclipse.persistence.logging.SessionLog; +import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration; +import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; +import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter; +import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; +import org.springframework.transaction.jta.JtaTransactionManager; + +@Configuration +@EnableTransactionManagement +public class JpaConfiguration extends JpaBaseConfiguration { + + protected JpaConfiguration(DataSource dataSource, JpaProperties properties, + ObjectProvider<JtaTransactionManager> jtaTransactionManager) { + super(dataSource, properties, jtaTransactionManager); + } + + @Override + protected AbstractJpaVendorAdapter createJpaVendorAdapter() { + return new EclipseLinkJpaVendorAdapter(); + } + + @Override + protected Map<String, Object> getVendorProperties() { + return Map.of(PersistenceUnitProperties.BATCH_WRITING, BatchWriting.JDBC); + } + + /** + * Create EntityManagerFactory. + * + * @param builder EntityManagerFactoryBuilder + * @param dataSource DataSource + * @return LocalContainerEntityManagerFactoryBean + */ + @Bean("entityManagerFactory") + public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactory( + EntityManagerFactoryBuilder builder, DataSource dataSource, + ClRuntimeParameterGroup clRuntimeParameterGroup) { + + return builder.dataSource(dataSource) + .persistenceUnit(clRuntimeParameterGroup.getDatabaseProviderParameters().getPersistenceUnit()) + .properties(initJpaProperties()).build(); + } + + /** + * create a PlatformTransactionManager. + * + * @param emf EntityManagerFactory + * @return PlatformTransactionManager + */ + @Bean + public static PlatformTransactionManager transactionManager(EntityManagerFactory emf) { + final var transactionManager = new JpaTransactionManager(); + transactionManager.setEntityManagerFactory(emf); + return transactionManager; + } + + /** + * create Jpa Properties. + * + * @return JpaProperties + */ + @Bean + @Primary + public static JpaProperties properties(ClRuntimeParameterGroup clRuntimeParameterGroup) { + final var jpaProperties = new JpaProperties(); + jpaProperties.setShowSql(clRuntimeParameterGroup.isShowSql()); + jpaProperties.setDatabasePlatform(clRuntimeParameterGroup.getDatabasePlatform()); + return jpaProperties; + } + + private static Map<String, ?> initJpaProperties() { + final Map<String, Object> ret = new HashMap<>(); + // Add any JpaProperty you are interested in and is supported by your Database and JPA implementation + ret.put(PersistenceUnitProperties.BATCH_WRITING, BatchWriting.JDBC); + ret.put(PersistenceUnitProperties.LOGGING_LEVEL, SessionLog.INFO_LABEL); + ret.put(PersistenceUnitProperties.WEAVING, "false"); + ret.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.CREATE_ONLY); + ret.put(PersistenceUnitProperties.DDL_GENERATION_MODE, PersistenceUnitProperties.DDL_DATABASE_GENERATION); + return ret; + } +} diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ClRuntimeParameterGroup.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ClRuntimeParameterGroup.java index 86473caa8..9211ca211 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ClRuntimeParameterGroup.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/main/parameters/ClRuntimeParameterGroup.java @@ -22,6 +22,7 @@ package org.onap.policy.clamp.controlloop.runtime.main.parameters; import javax.validation.Valid; import javax.validation.constraints.Min; +import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import lombok.Getter; import lombok.Setter; @@ -65,4 +66,8 @@ public class ClRuntimeParameterGroup { private long participantDeregisterAckIntervalSec; private long participantUpdateIntervalSec; + @NotBlank + private String databasePlatform; + + private boolean showSql = false; } diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java index 2a1f9082f..f011d9392 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandler.java @@ -40,6 +40,7 @@ import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ServiceTemplateProvider; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopAck; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessage; @@ -54,7 +55,6 @@ import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantReg import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; -import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.slf4j.Logger; @@ -80,7 +80,7 @@ public class SupervisionHandler { private final ControlLoopProvider controlLoopProvider; private final ParticipantProvider participantProvider; private final MonitoringProvider monitoringProvider; - private final PolicyModelsProvider modelsProvider; + private final ServiceTemplateProvider serviceTemplateProvider; // Publishers for participant communication private final ControlLoopUpdatePublisher controlLoopUpdatePublisher; @@ -427,7 +427,7 @@ public class SupervisionHandler { private int getFirstStartPhase(ControlLoop controlLoop) { ToscaServiceTemplate toscaServiceTemplate = null; try { - toscaServiceTemplate = modelsProvider.getServiceTemplateList(null, null).get(0); + toscaServiceTemplate = serviceTemplateProvider.getServiceTemplateList(null, null).get(0); } catch (PfModelException e) { throw new PfModelRuntimeException(Status.BAD_REQUEST, "Canont load ToscaServiceTemplate from DB", e); } diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScanner.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScanner.java index 48e06010d..900a117bd 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScanner.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScanner.java @@ -34,13 +34,13 @@ import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ServiceTemplateProvider; import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopStateChangePublisher; import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopUpdatePublisher; import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantStatusReqPublisher; import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher; import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; @@ -63,7 +63,7 @@ public class SupervisionScanner { private final Map<ToscaConceptIdentifier, Integer> phaseMap = new HashMap<>(); private final ControlLoopProvider controlLoopProvider; - private final PolicyModelsProvider modelsProvider; + private final ServiceTemplateProvider serviceTemplateProvider; private final ControlLoopStateChangePublisher controlLoopStateChangePublisher; private final ControlLoopUpdatePublisher controlLoopUpdatePublisher; private final ParticipantProvider participantProvider; @@ -74,7 +74,7 @@ public class SupervisionScanner { * Constructor for instantiating SupervisionScanner. * * @param controlLoopProvider the provider to use to read control loops from the database - * @param modelsProvider the Policy Models Provider + * @param serviceTemplateProvider the Policy Models Provider * @param controlLoopStateChangePublisher the ControlLoop StateChange Publisher * @param controlLoopUpdatePublisher the ControlLoopUpdate Publisher * @param participantProvider the Participant Provider @@ -82,14 +82,15 @@ public class SupervisionScanner { * @param participantUpdatePublisher the Participant Update Publisher * @param clRuntimeParameterGroup the parameters for the control loop runtime */ - public SupervisionScanner(final ControlLoopProvider controlLoopProvider, PolicyModelsProvider modelsProvider, + public SupervisionScanner(final ControlLoopProvider controlLoopProvider, + ServiceTemplateProvider serviceTemplateProvider, final ControlLoopStateChangePublisher controlLoopStateChangePublisher, ControlLoopUpdatePublisher controlLoopUpdatePublisher, ParticipantProvider participantProvider, ParticipantStatusReqPublisher participantStatusReqPublisher, ParticipantUpdatePublisher participantUpdatePublisher, final ClRuntimeParameterGroup clRuntimeParameterGroup) { this.controlLoopProvider = controlLoopProvider; - this.modelsProvider = modelsProvider; + this.serviceTemplateProvider = serviceTemplateProvider; this.controlLoopStateChangePublisher = controlLoopStateChangePublisher; this.controlLoopUpdatePublisher = controlLoopUpdatePublisher; this.participantProvider = participantProvider; @@ -130,7 +131,7 @@ public class SupervisionScanner { } try { - var list = modelsProvider.getServiceTemplateList(null, null); + var list = serviceTemplateProvider.getServiceTemplateList(null, null); if (list != null && !list.isEmpty()) { ToscaServiceTemplate toscaServiceTemplate = list.get(0); diff --git a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ControlLoopUpdatePublisher.java b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ControlLoopUpdatePublisher.java index 06fbcd649..32c0638af 100644 --- a/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ControlLoopUpdatePublisher.java +++ b/runtime-controlloop/src/main/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/ControlLoopUpdatePublisher.java @@ -31,9 +31,9 @@ import org.onap.policy.clamp.controlloop.common.utils.CommonUtils; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ServiceTemplateProvider; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate; import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,7 +47,7 @@ import org.springframework.stereotype.Component; public class ControlLoopUpdatePublisher extends AbstractParticipantPublisher<ControlLoopUpdate> { private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopUpdatePublisher.class); - private final PolicyModelsProvider modelsProvider; + private final ServiceTemplateProvider serviceTemplateProvider; /** * Send ControlLoopUpdate to Participant. @@ -72,7 +72,7 @@ public class ControlLoopUpdatePublisher extends AbstractParticipantPublisher<Con controlLoopUpdateMsg.setTimestamp(Instant.now()); ToscaServiceTemplate toscaServiceTemplate; try { - toscaServiceTemplate = modelsProvider.getServiceTemplateList(null, null).get(0); + toscaServiceTemplate = serviceTemplateProvider.getServiceTemplateList(null, null).get(0); } catch (PfModelException pfme) { LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme); return; diff --git a/runtime-controlloop/src/main/resources/META-INF/persistence.xml b/runtime-controlloop/src/main/resources/META-INF/persistence.xml index e5d2cab11..a4e9a56e9 100644 --- a/runtime-controlloop/src/main/resources/META-INF/persistence.xml +++ b/runtime-controlloop/src/main/resources/META-INF/persistence.xml @@ -69,53 +69,5 @@ <shared-cache-mode>NONE</shared-cache-mode> </persistence-unit> - <persistence-unit name="ToscaConceptTest" transaction-type="RESOURCE_LOCAL"> - <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> - - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaCapabilityAssignment</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaCapabilityAssignments</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaCapabilityType</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaCapabilityTypes</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplates</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeType</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTypes</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaParameter</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaRelationshipType</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaRelationshipTypes</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaRequirement</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaRequirements</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate</class> - <class>org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoop</class> - <class>org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoopElement</class> - <class>org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipant</class> - <class>org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipantStatistics</class> - <class>org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaClElementStatistics</class> - <properties> - <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> - <property name="eclipselink.ddl-generation.output-mode" value="database" /> - <property name="eclipselink.logging.level" value="INFO" /> - - <!-- property name="eclipselink.logging.level" value="ALL" /> - <property name="eclipselink.logging.level.jpa" value="ALL" /> - <property name="eclipselink.logging.level.ddl" value="ALL" /> - <property name="eclipselink.logging.level.connection" value="ALL" /> - <property name="eclipselink.logging.level.sql" value="ALL" /> - <property name="eclipselink.logging.level.transaction" value="ALL" /> - <property name="eclipselink.logging.level.sequencing" value="ALL" /> - <property name="eclipselink.logging.level.server" value="ALL" /> - <property name="eclipselink.logging.level.query" value="ALL" /> - <property name="eclipselink.logging.level.properties" value="ALL" /--> - </properties> - <shared-cache-mode>NONE</shared-cache-mode> - </persistence-unit> </persistence> diff --git a/runtime-controlloop/src/main/resources/application.yaml b/runtime-controlloop/src/main/resources/application.yaml index aa9434b97..bae4b9695 100644 --- a/runtime-controlloop/src/main/resources/application.yaml +++ b/runtime-controlloop/src/main/resources/application.yaml @@ -6,6 +6,16 @@ spring: http: converters: preferred-json-mapper: gson + datasource: + url: jdbc:mariadb://${mariadb.host:localhost}:${mariadb.port:3306}/controlloop + driverClassName: org.mariadb.jdbc.Driver + username: policy + password: P01icY + hikari: + connectionTimeout: 30000 + idleTimeout: 600000 + maxLifetime: 1800000 + maximumPoolSize: 10 security: enable-csrf: false @@ -28,6 +38,7 @@ runtime: updateParameters: maxRetryCount: 4 maxWaitMs: 20000 + databasePlatform: org.eclipse.persistence.platform.database.MySQLPlatform databaseProviderParameters: name: PolicyProviderParameterGroup implementation: org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/commissioning/rest/CommissioningControllerTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/commissioning/rest/CommissioningControllerTest.java index 4a5a6e2a3..9d1435e7c 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/commissioning/rest/CommissioningControllerTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/commissioning/rest/CommissioningControllerTest.java @@ -39,12 +39,10 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ServiceTemplateProvider; import org.onap.policy.clamp.controlloop.models.messages.rest.commissioning.CommissioningResponse; import org.onap.policy.clamp.controlloop.runtime.instantiation.InstantiationUtils; -import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController; -import org.onap.policy.models.provider.PolicyModelsProvider; -import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.springframework.beans.factory.annotation.Autowired; @@ -70,7 +68,7 @@ class CommissioningControllerTest extends CommonRestController { private static ToscaServiceTemplate commonPropertiesServiceTemplate = new ToscaServiceTemplate(); @Autowired - private ClRuntimeParameterGroup clRuntimeParameterGroup; + private ServiceTemplateProvider serviceTemplateProvider; @LocalServerPort private int randomServerPort; @@ -282,37 +280,25 @@ class CommissioningControllerTest extends CommonRestController { Response resp = invocationBuilder.delete(); assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus()); - try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory() - .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) { - List<ToscaServiceTemplate> templatesInDB = modelsProvider.getServiceTemplateList(null, null); - assertThat(templatesInDB).isEmpty(); - } + List<ToscaServiceTemplate> templatesInDB = serviceTemplateProvider.getServiceTemplateList(null, null); + assertThat(templatesInDB).isEmpty(); } private synchronized void createEntryInDB() throws Exception { - try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory() - .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) { - deleteEntryInDB(commonPropertiesServiceTemplate.getName(), commonPropertiesServiceTemplate.getVersion()); - modelsProvider.createServiceTemplate(serviceTemplate); - } + deleteEntryInDB(commonPropertiesServiceTemplate.getName(), commonPropertiesServiceTemplate.getVersion()); + serviceTemplateProvider.createServiceTemplate(serviceTemplate); } // Delete entries from the DB after relevant tests private synchronized void deleteEntryInDB(String name, String version) throws Exception { - try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory() - .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) { - if (!modelsProvider.getServiceTemplateList(null, null).isEmpty()) { - modelsProvider.deleteServiceTemplate(name, version); - } + if (!serviceTemplateProvider.getServiceTemplateList(null, null).isEmpty()) { + serviceTemplateProvider.deleteServiceTemplate(name, version); } } private synchronized void createFullEntryInDbWithCommonProps() throws Exception { - try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory() - .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) { - deleteEntryInDB(commonPropertiesServiceTemplate.getName(), commonPropertiesServiceTemplate.getVersion()); - modelsProvider.createServiceTemplate(commonPropertiesServiceTemplate); - } + deleteEntryInDB(commonPropertiesServiceTemplate.getName(), commonPropertiesServiceTemplate.getVersion()); + serviceTemplateProvider.createServiceTemplate(commonPropertiesServiceTemplate); } } diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java index ae1c59851..564109e5d 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/ControlLoopInstantiationProviderTest.java @@ -26,6 +26,7 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; import java.io.IOException; import java.util.ArrayList; @@ -124,10 +125,8 @@ class ControlLoopInstantiationProviderTest { clProvider = new ControlLoopProvider(controlLoopParameters.getDatabaseProviderParameters()); participantProvider = new ParticipantProvider(controlLoopParameters.getDatabaseProviderParameters()); - var participantStatisticsProvider = - new ParticipantStatisticsProvider(controlLoopParameters.getDatabaseProviderParameters()); - var clElementStatisticsProvider = - new ClElementStatisticsProvider(controlLoopParameters.getDatabaseProviderParameters()); + var participantStatisticsProvider = Mockito.mock(ParticipantStatisticsProvider.class); + var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class); commissioningProvider = new CommissioningProvider(new ServiceTemplateProvider(modelsProvider), clProvider, null, participantProvider); var monitoringProvider = @@ -138,11 +137,11 @@ class ControlLoopInstantiationProviderTest { var participantRegisterAckPublisher = Mockito.mock(ParticipantRegisterAckPublisher.class); var participantDeregisterAckPublisher = Mockito.mock(ParticipantDeregisterAckPublisher.class); var participantUpdatePublisher = Mockito.mock(ParticipantUpdatePublisher.class); - var modelsProvider = Mockito.mock(PolicyModelsProvider.class); + var serviceTemplateProvider = Mockito.mock(ServiceTemplateProvider.class); - supervisionHandler = new SupervisionHandler(clProvider, participantProvider, monitoringProvider, modelsProvider, - controlLoopUpdatePublisher, controlLoopStateChangePublisher, participantRegisterAckPublisher, - participantDeregisterAckPublisher, participantUpdatePublisher); + supervisionHandler = new SupervisionHandler(clProvider, participantProvider, monitoringProvider, + serviceTemplateProvider, controlLoopUpdatePublisher, controlLoopStateChangePublisher, + participantRegisterAckPublisher, participantDeregisterAckPublisher, participantUpdatePublisher); } @BeforeEach diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/rest/InstantiationControllerTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/rest/InstantiationControllerTest.java index 2e01be289..e361ff469 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/rest/InstantiationControllerTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/instantiation/rest/InstantiationControllerTest.java @@ -37,17 +37,15 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop; import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoops; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ServiceTemplateProvider; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.ControlLoopPrimedResponse; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationCommand; import org.onap.policy.clamp.controlloop.models.messages.rest.instantiation.InstantiationResponse; import org.onap.policy.clamp.controlloop.runtime.instantiation.ControlLoopInstantiationProvider; import org.onap.policy.clamp.controlloop.runtime.instantiation.InstantiationUtils; -import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup; import org.onap.policy.clamp.controlloop.runtime.main.rest.InstantiationController; import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData; import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController; -import org.onap.policy.models.provider.PolicyModelsProvider; -import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.springframework.beans.factory.annotation.Autowired; @@ -86,7 +84,7 @@ class InstantiationControllerTest extends CommonRestController { private static ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(); @Autowired - private ClRuntimeParameterGroup clRuntimeParameterGroup; + private ServiceTemplateProvider serviceTemplateProvider; @Autowired private ControlLoopInstantiationProvider instantiationProvider; @@ -177,8 +175,8 @@ class InstantiationControllerTest extends CommonRestController { assertEquals(controlLoopFromRsc, controlLoopsFromDb.getControlLoopList().get(0)); } - invocationBuilder = super.sendRequest(PRIMING_ENDPOINT + "?name=" - + "PMSHInstance0Create" + "&version=" + "1.0.1"); + invocationBuilder = + super.sendRequest(PRIMING_ENDPOINT + "?name=" + "PMSHInstance0Create" + "&version=" + "1.0.1"); Response rawresp = invocationBuilder.buildGet().invoke(); assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); ControlLoopPrimedResponse primResponse = rawresp.readEntity(ControlLoopPrimedResponse.class); @@ -349,19 +347,13 @@ class InstantiationControllerTest extends CommonRestController { } private synchronized void deleteEntryInDB(String name, String version) throws Exception { - try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory() - .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) { - if (!modelsProvider.getServiceTemplateList(null, null).isEmpty()) { - modelsProvider.deleteServiceTemplate(name, version); - } + if (!serviceTemplateProvider.getServiceTemplateList(null, null).isEmpty()) { + serviceTemplateProvider.deleteServiceTemplate(name, version); } } private synchronized void createEntryInDB() throws Exception { - try (PolicyModelsProvider modelsProvider = new PolicyModelsProviderFactory() - .createPolicyModelsProvider(clRuntimeParameterGroup.getDatabaseProviderParameters())) { - deleteEntryInDB(serviceTemplate.getName(), serviceTemplate.getVersion()); - modelsProvider.createServiceTemplate(serviceTemplate); - } + deleteEntryInDB(serviceTemplate.getName(), serviceTemplate.getVersion()); + serviceTemplateProvider.createServiceTemplate(serviceTemplate); } } diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/TestMonitoringProvider.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/TestMonitoringProvider.java index 2fcbf1297..2233e862c 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/TestMonitoringProvider.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/TestMonitoringProvider.java @@ -24,13 +24,19 @@ package org.onap.policy.clamp.controlloop.runtime.monitoring; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyMap; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.File; import java.time.Instant; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.UUID; +import javax.ws.rs.core.Response; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -47,6 +53,7 @@ import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; class TestMonitoringProvider { @@ -61,14 +68,23 @@ class TestMonitoringProvider { "src/test/resources/rest/monitoring/TestClElementStatistics_Invalid.json"; private static final Coder CODER = new StandardCoder(); - private static final String LIST_IS_NULL = ".*StatisticsList is marked .*ull but is null"; + private static final String STAT_LIST_IS_NULL = ".*StatisticsList is marked .*ull but is null"; + private static final String PARTICIPANT_STAT_LIST_IS_NULL = + "participantStatisticsList is marked .*null but is null"; + private static final String NAME_IS_NULL = "name is marked .*null but is null"; + private static final String CL_LIST_IS_NULL = "clElementStatisticsList is marked .*null but is null"; + private static final String ID_VERSION1 = "1.001"; + private static final String ID_VERSION2 = "1.002"; + private static final String ID_NAME1 = "name1"; + private static final String ID_NAME2 = "name2"; + private static final String SORT_DESC = "DESC"; + private static final String ID_NAME3 = "testCLName"; + private static final String ID_INVALID_NAME = "invalidCLName"; private static ParticipantStatisticsList inputParticipantStatistics; private static ParticipantStatisticsList invalidParticipantInput; private static ClElementStatisticsList inputClElementStatistics; private static ClElementStatisticsList invalidClElementInput; - private ParticipantStatisticsProvider participantStatisticsProvider = null; - private ClElementStatisticsProvider clElementStatisticsProvider = null; private ControlLoopProvider clProvider = null; @BeforeAll @@ -84,12 +100,6 @@ class TestMonitoringProvider { @AfterEach void close() throws Exception { - if (participantStatisticsProvider != null) { - participantStatisticsProvider.close(); - } - if (clElementStatisticsProvider != null) { - clElementStatisticsProvider.close(); - } if (clProvider != null) { clProvider.close(); } @@ -97,20 +107,28 @@ class TestMonitoringProvider { @Test void testCreateParticipantStatistics() throws Exception { + var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class); + var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class); ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("createparStat"); - participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters()); - clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters()); clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters()); MonitoringProvider provider = new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider); + + when(participantStatisticsProvider.createParticipantStatistics(any())) + .thenReturn(inputParticipantStatistics.getStatisticsList()); + + when(participantStatisticsProvider.createParticipantStatistics(eq(null))) + .thenThrow(new PfModelRuntimeException(Response.Status.BAD_REQUEST, PARTICIPANT_STAT_LIST_IS_NULL)); + // Creating statistics data in db with null input + assertThatThrownBy(() -> { provider.createParticipantStatistics(null); - }).hasMessageMatching(LIST_IS_NULL); + }).hasMessageMatching(STAT_LIST_IS_NULL); assertThatThrownBy(() -> { provider.createParticipantStatistics(invalidParticipantInput.getStatisticsList()); - }).hasMessageMatching("participantStatisticsList is marked .*null but is null"); + }).hasMessageMatching(PARTICIPANT_STAT_LIST_IS_NULL); // Creating statistics data from input json ParticipantStatisticsList createResponse = @@ -123,32 +141,41 @@ class TestMonitoringProvider { @Test void testGetParticipantStatistics() throws Exception { + var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class); + when(participantStatisticsProvider.getFilteredParticipantStatistics(eq(ID_NAME1), any(), any(), any(), eq(null), + eq(SORT_DESC), eq(0))).thenReturn(List.of(inputParticipantStatistics.getStatisticsList().get(0))); + + when(participantStatisticsProvider.getFilteredParticipantStatistics(eq(ID_NAME1), any(), + eq(Instant.parse("2021-01-11T12:00:00.000Z")), eq(Instant.parse("2021-01-11T16:00:00.000Z")), eq(null), + eq(SORT_DESC), eq(0))).thenReturn(List.of()); + + when(participantStatisticsProvider.getFilteredParticipantStatistics(eq(ID_NAME2), any(), any(), any(), eq(null), + eq(SORT_DESC), eq(1))).thenReturn(List.of(inputParticipantStatistics.getStatisticsList().get(2))); + ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("getparStat"); - participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters()); - clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters()); clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters()); + var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class); MonitoringProvider provider = new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider); - provider.createParticipantStatistics(inputParticipantStatistics.getStatisticsList()); assertThatThrownBy(() -> { provider.fetchFilteredParticipantStatistics(null, null, 0, null, null); - }).hasMessageMatching("name is marked .*null but is null"); + }).hasMessageMatching(NAME_IS_NULL); // Fetch specific statistics record with name, version and record count ParticipantStatisticsList getResponse = - provider.fetchFilteredParticipantStatistics("name2", "1.001", 1, null, null); + provider.fetchFilteredParticipantStatistics(ID_NAME2, ID_VERSION1, 1, null, null); assertThat(getResponse.getStatisticsList()).hasSize(1); assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""), inputParticipantStatistics.getStatisticsList().get(2).toString().replaceAll("\\s+", "")); // Fetch statistics using timestamp - getResponse = provider.fetchFilteredParticipantStatistics("name1", "1.001", 0, null, + getResponse = provider.fetchFilteredParticipantStatistics(ID_NAME1, ID_VERSION1, 0, null, Instant.parse("2021-01-10T15:00:00.000Z")); assertThat(getResponse.getStatisticsList()).hasSize(1); - getResponse = provider.fetchFilteredParticipantStatistics("name1", "1.001", 0, + getResponse = provider.fetchFilteredParticipantStatistics(ID_NAME1, ID_VERSION1, 0, Instant.parse("2021-01-11T12:00:00.000Z"), Instant.parse("2021-01-11T16:00:00.000Z")); assertThat(getResponse.getStatisticsList()).isEmpty(); @@ -156,21 +183,27 @@ class TestMonitoringProvider { @Test void testCreateClElementStatistics() throws Exception { + var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class); + when(clElementStatisticsProvider.createClElementStatistics(any())) + .thenReturn(inputClElementStatistics.getClElementStatistics()); + + when(clElementStatisticsProvider.createClElementStatistics(eq(null))) + .thenThrow(new PfModelRuntimeException(Response.Status.BAD_REQUEST, CL_LIST_IS_NULL)); + ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("createelemstat"); - participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters()); - clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters()); clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters()); + var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class); MonitoringProvider provider = new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider); // Creating statistics data in db with null input assertThatThrownBy(() -> { provider.createClElementStatistics(null); - }).hasMessageMatching(LIST_IS_NULL); + }).hasMessageMatching(STAT_LIST_IS_NULL); assertThatThrownBy(() -> { provider.createClElementStatistics(invalidClElementInput.getClElementStatistics()); - }).hasMessageMatching("clElementStatisticsList is marked .*null but is null"); + }).hasMessageMatching(CL_LIST_IS_NULL); // Creating clElement statistics data from input json ClElementStatisticsList createResponse = @@ -183,42 +216,46 @@ class TestMonitoringProvider { @Test void testGetClElementStatistics() throws Exception { + var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class); + var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class); ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("getelemstat"); - participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters()); - clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters()); clProvider = new ControlLoopProvider(parameters.getDatabaseProviderParameters()); + when(clElementStatisticsProvider.getFilteredClElementStatistics(eq(ID_NAME1), any(), any(), any(), anyMap(), + eq(SORT_DESC), eq(0))) + .thenReturn(List.of(inputClElementStatistics.getClElementStatistics().get(0), + inputClElementStatistics.getClElementStatistics().get(1))); + MonitoringProvider provider = new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider); assertThatThrownBy(() -> { provider.fetchFilteredClElementStatistics(null, null, null, null, null, 0); - }).hasMessageMatching("name is marked .*null but is null"); + }).hasMessageMatching(NAME_IS_NULL); provider.createClElementStatistics(inputClElementStatistics.getClElementStatistics()); ClElementStatisticsList getResponse = - provider.fetchFilteredClElementStatistics("name1", null, null, null, null, 0); + provider.fetchFilteredClElementStatistics(ID_NAME1, null, null, null, null, 0); assertThat(getResponse.getClElementStatistics()).hasSize(2); assertEquals(getResponse.getClElementStatistics().get(0).toString().replaceAll("\\s+", ""), inputClElementStatistics.getClElementStatistics().get(0).toString().replaceAll("\\s+", "")); // Fetch specific statistics record with name, id and record count - getResponse = provider.fetchFilteredClElementStatistics("name1", "1.001", + getResponse = provider.fetchFilteredClElementStatistics(ID_NAME1, ID_VERSION1, "709c62b3-8918-41b9-a747-d21eb79c6c20", null, null, 0); assertThat(getResponse.getClElementStatistics()).hasSize(2); // Fetch statistics using timestamp - getResponse = provider.fetchFilteredClElementStatistics("name1", "1.001", null, + getResponse = provider.fetchFilteredClElementStatistics(ID_NAME1, ID_VERSION1, null, Instant.parse("2021-01-10T13:45:00.000Z"), null, 0); assertThat(getResponse.getClElementStatistics()).hasSize(2); } @Test void testGetParticipantStatsPerCL() throws Exception { - ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("getparStatCL"); - participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters()); - clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters()); + var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class); + var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class); var mockClProvider = Mockito.mock(ControlLoopProvider.class); var provider = new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, mockClProvider); @@ -227,56 +264,63 @@ class TestMonitoringProvider { var controlLoop = new ControlLoop(); var element = new ControlLoopElement(); - element.setParticipantId(new ToscaConceptIdentifier("name1", "1.001")); + element.setParticipantId(new ToscaConceptIdentifier(ID_NAME1, ID_VERSION1)); controlLoop.setElements(Map.of(UUID.randomUUID(), element)); - when(mockClProvider.getControlLoop(new ToscaConceptIdentifier("testName", "1.001"))) - .thenReturn(controlLoop); + when(mockClProvider.getControlLoop(new ToscaConceptIdentifier(ID_NAME2, ID_VERSION1))).thenReturn(controlLoop); + + when(participantStatisticsProvider.getFilteredParticipantStatistics(eq(ID_NAME1), eq(ID_VERSION1), any(), any(), + eq(null), eq(SORT_DESC), eq(0))) + .thenReturn(List.of(inputParticipantStatistics.getStatisticsList().get(0), + inputParticipantStatistics.getStatisticsList().get(1))); - ParticipantStatisticsList getResponse = provider.fetchParticipantStatsPerControlLoop("testName", "1.001"); + ParticipantStatisticsList getResponse = provider.fetchParticipantStatsPerControlLoop(ID_NAME2, ID_VERSION1); assertThat(getResponse.getStatisticsList()).hasSize(2); assertEquals(getResponse.getStatisticsList().get(0).toString().replaceAll("\\s+", ""), inputParticipantStatistics.getStatisticsList().get(0).toString().replaceAll("\\s+", "")); - assertThat(provider.fetchParticipantStatsPerControlLoop("invalidCLName", "1.002").getStatisticsList()) + assertThat(provider.fetchParticipantStatsPerControlLoop(ID_INVALID_NAME, ID_VERSION2).getStatisticsList()) .isEmpty(); } @Test void testClElementStatsPerCL() throws Exception { // Setup a dummy Control loop data - ControlLoopElement mockClElement = new ControlLoopElement(); + var mockClElement = new ControlLoopElement(); mockClElement.setId(inputClElementStatistics.getClElementStatistics().get(0).getId()); mockClElement.setParticipantId(new ToscaConceptIdentifier( inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getName(), inputClElementStatistics.getClElementStatistics().get(0).getParticipantId().getVersion())); - ControlLoop mockCL = new ControlLoop(); + var mockCL = new ControlLoop(); mockCL.setElements(new LinkedHashMap<>()); mockCL.getElements().put(mockClElement.getId(), mockClElement); - ClRuntimeParameterGroup parameters = CommonTestData.geParameterGroup("getelemstatPerCL"); - participantStatisticsProvider = new ParticipantStatisticsProvider(parameters.getDatabaseProviderParameters()); - clElementStatisticsProvider = new ClElementStatisticsProvider(parameters.getDatabaseProviderParameters()); - ControlLoopProvider mockClProvider = Mockito.mock(ControlLoopProvider.class); + var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class); + var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class); + var mockClProvider = Mockito.mock(ControlLoopProvider.class); var monitoringProvider = new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, mockClProvider); // Mock controlloop data to be returned for the given CL Id - when(mockClProvider.getControlLoop(new ToscaConceptIdentifier("testCLName", "1.001"))).thenReturn(mockCL); + when(mockClProvider.getControlLoop(new ToscaConceptIdentifier(ID_NAME3, ID_VERSION1))).thenReturn(mockCL); + + when(clElementStatisticsProvider.getFilteredClElementStatistics(eq(ID_NAME1), eq(ID_VERSION1), any(), any(), + anyMap(), eq(SORT_DESC), eq(0))) + .thenReturn(List.of(inputClElementStatistics.getClElementStatistics().get(0), + inputClElementStatistics.getClElementStatistics().get(1))); monitoringProvider.createClElementStatistics(inputClElementStatistics.getClElementStatistics()); ClElementStatisticsList getResponse = - monitoringProvider.fetchClElementStatsPerControlLoop("testCLName", "1.001"); + monitoringProvider.fetchClElementStatsPerControlLoop(ID_NAME3, ID_VERSION1); assertThat(getResponse.getClElementStatistics()).hasSize(2); assertEquals(getResponse.getClElementStatistics().get(1).toString().replaceAll("\\s+", ""), inputClElementStatistics.getClElementStatistics().get(1).toString().replaceAll("\\s+", "")); - assertThat( - monitoringProvider.fetchClElementStatsPerControlLoop("invalidCLName", "1.002").getClElementStatistics()) - .isEmpty(); + assertThat(monitoringProvider.fetchClElementStatsPerControlLoop(ID_INVALID_NAME, ID_VERSION2) + .getClElementStatistics()).isEmpty(); Map<String, ToscaConceptIdentifier> clElementIds = - monitoringProvider.getAllClElementsIdPerControlLoop("testCLName", "1.001"); + monitoringProvider.getAllClElementsIdPerControlLoop(ID_NAME3, ID_VERSION1); assertThat(clElementIds) .containsKey(inputClElementStatistics.getClElementStatistics().get(0).getId().toString()); } diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/rest/MonitoringQueryControllerTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/rest/MonitoringQueryControllerTest.java index 77742aea9..eaf824623 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/rest/MonitoringQueryControllerTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/monitoring/rest/MonitoringQueryControllerTest.java @@ -141,7 +141,12 @@ class MonitoringQueryControllerTest extends CommonRestController { assertNotNull(result1); assertThat(result1.getClElementStatistics()).hasSize(2); - assertEquals(result1.getClElementStatistics().get(0), clElementStatisticsList.getClElementStatistics().get(0)); + + var clElementStat0 = clElementStatisticsList.getClElementStatistics().get(0); + for (var clElement : result1.getClElementStatistics()) { + assertEquals(clElement.getParticipantId().asConceptKey(), clElementStat0.getParticipantId().asConceptKey()); + assertEquals(clElement.getId(), clElementStat0.getId()); + } // Filter statistics based on timestamp Invocation.Builder invokeRequest2 = super.sendRequest(CLELEMENT_STATS_ENDPOINT + "?name=" @@ -155,7 +160,7 @@ class MonitoringQueryControllerTest extends CommonRestController { assertNotNull(result2); assertThat(result2.getClElementStatistics()).hasSize(1); - assertEquals(result1.getClElementStatistics().get(0), clElementStatisticsList.getClElementStatistics().get(0)); + assertEquals(result2.getClElementStatistics().get(0), clElementStat0); } @Test @@ -178,7 +183,7 @@ class MonitoringQueryControllerTest extends CommonRestController { assertNotNull(result1); assertThat(result1.getStatisticsList()).hasSize(2); - assertEquals(result1.getStatisticsList().get(0), participantStatisticsList.getStatisticsList().get(0)); + assertThat(result1.getStatisticsList()).contains(participantStatisticsList.getStatisticsList().get(0)); // Filter statistics based on timestamp Invocation.Builder invokeRequest2 = super.sendRequest(PARTICIPANT_STATS_ENDPOINT + "?name=" @@ -191,7 +196,7 @@ class MonitoringQueryControllerTest extends CommonRestController { assertNotNull(result2); assertThat(result2.getStatisticsList()).hasSize(1); - assertEquals(result1.getStatisticsList().get(0), participantStatisticsList.getStatisticsList().get(0)); + assertEquals(result2.getStatisticsList().get(0), participantStatisticsList.getStatisticsList().get(0)); } @Test diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandlerTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandlerTest.java index bbc4deec1..a314b30a0 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandlerTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionHandlerTest.java @@ -41,6 +41,7 @@ import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ServiceTemplateProvider; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopAck; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType; @@ -56,7 +57,6 @@ import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantReg import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpdatePublisher; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; class SupervisionHandlerTest { @@ -265,13 +265,13 @@ class SupervisionHandlerTest { when(controlLoopProvider.getControlLoop(identifier)).thenReturn(controlLoop); - var modelsProvider = Mockito.mock(PolicyModelsProvider.class); - when(modelsProvider.getServiceTemplateList(any(), any())) + var serviceTemplateProvider = Mockito.mock(ServiceTemplateProvider.class); + when(serviceTemplateProvider.getServiceTemplateList(any(), any())) .thenReturn(List.of(InstantiationUtils.getToscaServiceTemplate(TOSCA_TEMPLATE_YAML))); - return new SupervisionHandler(controlLoopProvider, participantProvider, monitoringProvider, modelsProvider, - controlLoopUpdatePublisher, controlLoopStateChangePublisher, participantRegisterAckPublisher, - participantDeregisterAckPublisher, participantUpdatePublisher); + return new SupervisionHandler(controlLoopProvider, participantProvider, monitoringProvider, + serviceTemplateProvider, controlLoopUpdatePublisher, controlLoopStateChangePublisher, + participantRegisterAckPublisher, participantDeregisterAckPublisher, participantUpdatePublisher); } } diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScannerTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScannerTest.java index f884e0129..50672a84e 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScannerTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/SupervisionScannerTest.java @@ -39,6 +39,7 @@ import org.onap.policy.clamp.controlloop.models.controlloop.concepts.Participant import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantState; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ServiceTemplateProvider; import org.onap.policy.clamp.controlloop.runtime.instantiation.InstantiationUtils; import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopStateChangePublisher; import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ControlLoopUpdatePublisher; @@ -47,7 +48,6 @@ import org.onap.policy.clamp.controlloop.runtime.supervision.comm.ParticipantUpd import org.onap.policy.clamp.controlloop.runtime.util.CommonTestData; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; @@ -57,12 +57,12 @@ class SupervisionScannerTest { "src/test/resources/rest/servicetemplates/tosca-for-smoke-testing.yaml"; private static final String CONTROLLOOP_JSON = "src/test/resources/rest/controlloops/ControlLoopsSmoke.json"; - private static PolicyModelsProvider modelsProvider = mock(PolicyModelsProvider.class); + private static ServiceTemplateProvider serviceTemplateProvider = mock(ServiceTemplateProvider.class); @BeforeAll public static void setUpBeforeAll() throws Exception { ToscaServiceTemplate serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML); - when(modelsProvider.getServiceTemplateList(null, null)).thenReturn(List.of(serviceTemplate)); + when(serviceTemplateProvider.getServiceTemplateList(null, null)).thenReturn(List.of(serviceTemplate)); } @Test @@ -79,7 +79,7 @@ class SupervisionScannerTest { InstantiationUtils.getControlLoopsFromResource(CONTROLLOOP_JSON, "Crud").getControlLoopList(); when(controlLoopProvider.getControlLoops(null, null)).thenReturn(controlLoops); - var supervisionScanner = new SupervisionScanner(controlLoopProvider, modelsProvider, + var supervisionScanner = new SupervisionScanner(controlLoopProvider, serviceTemplateProvider, controlLoopStateChangePublisher, controlLoopUpdatePublisher, participantProvider, participantStatusReqPublisher, participantUpdatePublisher, clRuntimeParameterGroup); supervisionScanner.run(false); @@ -103,7 +103,7 @@ class SupervisionScannerTest { var participantUpdatePublisher = mock(ParticipantUpdatePublisher.class); var clRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner"); - var supervisionScanner = new SupervisionScanner(controlLoopProvider, modelsProvider, + var supervisionScanner = new SupervisionScanner(controlLoopProvider, serviceTemplateProvider, controlLoopStateChangePublisher, controlLoopUpdatePublisher, participantProvider, participantStatusReqPublisher, participantUpdatePublisher, clRuntimeParameterGroup); supervisionScanner.run(false); @@ -129,7 +129,7 @@ class SupervisionScannerTest { var participantUpdatePublisher = mock(ParticipantUpdatePublisher.class); var clRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner"); - var supervisionScanner = new SupervisionScanner(controlLoopProvider, modelsProvider, + var supervisionScanner = new SupervisionScanner(controlLoopProvider, serviceTemplateProvider, controlLoopStateChangePublisher, controlLoopUpdatePublisher, participantProvider, participantStatusReqPublisher, participantUpdatePublisher, clRuntimeParameterGroup); @@ -166,7 +166,7 @@ class SupervisionScannerTest { var participantUpdatePublisher = mock(ParticipantUpdatePublisher.class); var clRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanner"); - var supervisionScanner = new SupervisionScanner(controlLoopProvider, modelsProvider, + var supervisionScanner = new SupervisionScanner(controlLoopProvider, serviceTemplateProvider, controlLoopStateChangePublisher, controlLoopUpdatePublisher, participantProvider, participantStatusReqPublisher, participantUpdatePublisher, clRuntimeParameterGroup); @@ -200,7 +200,7 @@ class SupervisionScannerTest { var controlLoopStateChangePublisher = mock(ControlLoopStateChangePublisher.class); var participantUpdatePublisher = mock(ParticipantUpdatePublisher.class); - var supervisionScanner = new SupervisionScanner(controlLoopProvider, modelsProvider, + var supervisionScanner = new SupervisionScanner(controlLoopProvider, serviceTemplateProvider, controlLoopStateChangePublisher, controlLoopUpdatePublisher, participantProvider, participantStatusReqPublisher, participantUpdatePublisher, clRuntimeParameterGroup); diff --git a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/SupervisionMessagesTest.java b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/SupervisionMessagesTest.java index 4dfb8a9fd..93433f59c 100644 --- a/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/SupervisionMessagesTest.java +++ b/runtime-controlloop/src/test/java/org/onap/policy/clamp/controlloop/runtime/supervision/comm/SupervisionMessagesTest.java @@ -41,6 +41,7 @@ import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider; import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantStatisticsProvider; +import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ServiceTemplateProvider; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregister; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantDeregisterAck; import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegisterAck; @@ -53,7 +54,6 @@ import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.event.comm.TopicSink; import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; class SupervisionMessagesTest extends CommonRestController { @@ -64,7 +64,6 @@ class SupervisionMessagesTest extends CommonRestController { private static final String TOPIC = "my-topic"; private static SupervisionHandler supervisionHandler; private static ControlLoopProvider clProvider; - private static PolicyModelsProvider modelsProvider; /** * setup Db Provider Parameters. @@ -75,31 +74,27 @@ class SupervisionMessagesTest extends CommonRestController { public static void setupDbProviderParameters() throws PfModelException { ClRuntimeParameterGroup controlLoopParameters = CommonTestData.geParameterGroup("instantproviderdb"); - modelsProvider = CommonTestData.getPolicyModelsProvider(controlLoopParameters.getDatabaseProviderParameters()); clProvider = new ControlLoopProvider(controlLoopParameters.getDatabaseProviderParameters()); - var participantStatisticsProvider = - new ParticipantStatisticsProvider(controlLoopParameters.getDatabaseProviderParameters()); - var clElementStatisticsProvider = - new ClElementStatisticsProvider(controlLoopParameters.getDatabaseProviderParameters()); + var participantStatisticsProvider = mock(ParticipantStatisticsProvider.class); + var clElementStatisticsProvider = mock(ClElementStatisticsProvider.class); var monitoringProvider = new MonitoringProvider(participantStatisticsProvider, clElementStatisticsProvider, clProvider); var participantProvider = new ParticipantProvider(controlLoopParameters.getDatabaseProviderParameters()); - var modelsProvider = Mockito.mock(PolicyModelsProvider.class); + var serviceTemplateProvider = Mockito.mock(ServiceTemplateProvider.class); var controlLoopUpdatePublisher = Mockito.mock(ControlLoopUpdatePublisher.class); var controlLoopStateChangePublisher = Mockito.mock(ControlLoopStateChangePublisher.class); var participantRegisterAckPublisher = Mockito.mock(ParticipantRegisterAckPublisher.class); var participantDeregisterAckPublisher = Mockito.mock(ParticipantDeregisterAckPublisher.class); var participantUpdatePublisher = Mockito.mock(ParticipantUpdatePublisher.class); - supervisionHandler = new SupervisionHandler(clProvider, participantProvider, monitoringProvider, modelsProvider, - controlLoopUpdatePublisher, controlLoopStateChangePublisher, participantRegisterAckPublisher, - participantDeregisterAckPublisher, participantUpdatePublisher); + supervisionHandler = new SupervisionHandler(clProvider, participantProvider, monitoringProvider, + serviceTemplateProvider, controlLoopUpdatePublisher, controlLoopStateChangePublisher, + participantRegisterAckPublisher, participantDeregisterAckPublisher, participantUpdatePublisher); } @AfterAll public static void closeDbProvider() throws PfModelException { clProvider.close(); - modelsProvider.close(); } @Test diff --git a/runtime-controlloop/src/test/resources/META-INF/persistence.xml b/runtime-controlloop/src/test/resources/META-INF/persistence.xml index c4e2e8d55..fe460ff4b 100644 --- a/runtime-controlloop/src/test/resources/META-INF/persistence.xml +++ b/runtime-controlloop/src/test/resources/META-INF/persistence.xml @@ -19,64 +19,6 @@ ============LICENSE_END========================================================= --> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> - <persistence-unit name="CommissioningMariaDb" transaction-type="RESOURCE_LOCAL"> - <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> - - <class>org.onap.policy.models.base.PfConceptKey</class> - <class>org.onap.policy.models.dao.converters.CDataConditioner</class> - <class>org.onap.policy.models.dao.converters.Uuid2String</class> - <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdp</class> - <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup</class> - <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpStatistics</class> - <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaCapabilityAssignment</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaCapabilityAssignments</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaCapabilityType</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaCapabilityTypes</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplate</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTemplates</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeType</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaNodeTypes</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaParameter</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaRelationshipType</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaRelationshipTypes</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaRequirement</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaRequirements</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate</class> - <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaTrigger</class> - <class>org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoop</class> - <class>org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaControlLoopElement</class> - <class>org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipant</class> - <class>org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipantStatistics</class> - <class>org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaClElementStatistics</class> - - <properties> - <property name="eclipselink.ddl-generation" value="create-or-extend-tables" /> - <property name="eclipselink.ddl-generation.output-mode" value="database" /> - <property name="eclipselink.logging.level" value="INFO" /> - - <!-- property name="eclipselink.logging.level" value="ALL" /> - <property name="eclipselink.logging.level.jpa" value="ALL" /> - <property name="eclipselink.logging.level.ddl" value="ALL" /> - <property name="eclipselink.logging.level.connection" value="ALL" /> - <property name="eclipselink.logging.level.sql" value="ALL" /> - <property name="eclipselink.logging.level.transaction" value="ALL" /> - <property name="eclipselink.logging.level.sequencing" value="ALL" /> - <property name="eclipselink.logging.level.server" value="ALL" /> - <property name="eclipselink.logging.level.query" value="ALL" /> - <property name="eclipselink.logging.level.properties" value="ALL" /--> - </properties> - <shared-cache-mode>NONE</shared-cache-mode> - </persistence-unit> - <persistence-unit name="ToscaConceptTest" transaction-type="RESOURCE_LOCAL"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> @@ -109,7 +51,6 @@ <class>org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaClElementStatistics</class> <properties> - <property name="eclipselink.target-database" value="MySQL" /> <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> <property name="eclipselink.ddl-generation.output-mode" value="database" /> <property name="eclipselink.logging.level" value="INFO" /> diff --git a/runtime-controlloop/src/test/resources/application_test.properties b/runtime-controlloop/src/test/resources/application_test.properties index 4b0c3b7a5..26822db44 100644 --- a/runtime-controlloop/src/test/resources/application_test.properties +++ b/runtime-controlloop/src/test/resources/application_test.properties @@ -2,6 +2,16 @@ spring.security.user.name=runtimeUser spring.security.user.password=zb!XztG34 security.enable-csrf=false +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=policy +spring.datasource.password=P01icY + +spring.datasource.hikari.connectionTimeout=30000 +spring.datasource.hikari.idleTimeout=600000 +spring.datasource.hikari.maxLifetime=1800000 +spring.datasource.hikari.maximumPoolSize=3 + server.port=6969 server.servlet.context-path=/onap/controlloop server.error.path=/error @@ -9,6 +19,7 @@ server.error.path=/error runtime.supervisionScannerIntervalSec=1000 runtime.participantClUpdateIntervalSec=1000 runtime.participantClStateChangeIntervalSec=1000 +runtime.showSql=true runtime.participantParameters.heartBeatMs=20000 runtime.participantParameters.maxStatusWaitMs=100000 runtime.participantParameters.updateParameters.maxRetryCount=3 diff --git a/runtime/src/test/resources/http-cache/third_party_proxy.py b/runtime/src/test/resources/http-cache/third_party_proxy.py index 013388197..d6628535d 100644 --- a/runtime/src/test/resources/http-cache/third_party_proxy.py +++ b/runtime/src/test/resources/http-cache/third_party_proxy.py @@ -24,12 +24,11 @@ # =================================================================== # ### - import json + import requests import os import errno -import sys import SimpleHTTPServer import SocketServer import argparse @@ -45,7 +44,7 @@ parser.add_argument("--root", "-r", default=tempfile.mkdtemp(), type=str, he parser.add_argument("--temp", "-t", default=tempfile.mkdtemp(), type=str, help="Temp folder for the generated content") parser.add_argument("--proxy" , type=str, help="Url of the Act as a proxy. If not set, this script only uses the cache and will return a 404 if files aren't found") parser.add_argument("--port", "-P", type=int, default="8081", help="Port on which the proxy should listen to") -parser.add_argument("--verbose", "-v", type=bool, help="Print more information in case of error") +parser.add_argument("--verbose", "-v", type=bool, help="print(more information in case of error") parser.add_argument("--proxyaddress","-a", type=str, help="Address of this proxy, generally either third-party-proxy:8085 or localhost:8085 depending if started with docker-compose or not") options = parser.parse_args() @@ -88,7 +87,7 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): self.wfile.write(fc) def _write_cache(self,cached_file_folder, header_file, content_file, response): - os.makedirs(cached_file_folder, 0777) + os.makedirs(cached_file_folder, 0o777) with open(content_file, 'w') as f: f.write(response.raw.read()) with open(header_file, 'w') as f: @@ -108,9 +107,9 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): return "%s/.header" % (cached_file_folder,) def _create_cache(self, generated_json, cached_file_folder, cached_file_header, cached_file_content): - print "jsonGenerated: " + generated_json + print("jsonGenerated: " + generated_json) if not os.path.exists(cached_file_folder): - os.makedirs(cached_file_folder, 0775) + os.makedirs(cached_file_folder, 0o775) with open(cached_file_header, 'w+') as f: f.write("{\"Content-Length\": \"" + str(len(generated_json)) + "\", \"Content-Type\": \"application/json\"}") with open(cached_file_content, 'w+') as f: @@ -125,15 +124,15 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): if self.path.startswith("/dcae-service-types?asdcResourceId=") and http_type == "GET": if not _file_available: - print "self.path start with /dcae-service-types?asdcResourceId=, generating response json..." + print("self.path start with /dcae-service-types?asdcResourceId=, generating response json...") uuidGenerated = str(uuid.uuid4()) typeId = "typeId-" + uuidGenerated typeName = "typeName-" + uuidGenerated - print "typeId generated: " + typeName + " and typeName: "+ typeId + print("typeId generated: " + typeName + " and typeName: "+ typeId) jsonGenerated = "{\"totalCount\":1, \"items\":[{\"typeId\":\"" + typeId + "\", \"typeName\":\"" + typeName +"\"}]}" - print "jsonGenerated: " + jsonGenerated + print("jsonGenerated: " + jsonGenerated) - os.makedirs(cached_file_folder, 0777) + os.makedirs(cached_file_folder, 0o777) with open(cached_file_header, 'w') as f: f.write("{\"Content-Length\": \"" + str(len(jsonGenerated)) + "\", \"Content-Type\": \"application/json\"}") with open(cached_file_content, 'w') as f: @@ -141,12 +140,12 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): return True elif self.path.startswith("/dcae-operationstatus/install") and http_type == "GET": if not _file_available: - print "self.path start with /dcae-operationstatus/install, generating response json..." + print("self.path start with /dcae-operationstatus/install, generating response json...") jsonGenerated = "{\"operationType\": \"install\", \"status\": \"succeeded\"}" - print "jsonGenerated: " + jsonGenerated + print("jsonGenerated: " + jsonGenerated) try: - os.makedirs(cached_file_folder, 0777) + os.makedirs(cached_file_folder, 0o777) except OSError as e: if e.errno != errno.EEXIST: raise @@ -159,12 +158,12 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): return True elif self.path.startswith("/dcae-operationstatus/uninstall") and http_type == "GET": if not _file_available: - print "self.path start with /dcae-operationstatus/uninstall, generating response json..." + print("self.path start with /dcae-operationstatus/uninstall, generating response json...") jsonGenerated = "{\"operationType\": \"uninstall\", \"status\": \"succeeded\"}" - print "jsonGenerated: " + jsonGenerated + print("jsonGenerated: " + jsonGenerated) try: - os.makedirs(cached_file_folder, 0777) + os.makedirs(cached_file_folder, 0o777) except OSError as e: if e.errno != errno.EEXIST: raise @@ -177,63 +176,63 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): return True elif self.path.startswith("/sdc/v1/catalog/services/") and http_type == "POST": if not _file_available: - print "self.path start with /sdc/v1/catalog/services/, generating response json..." + print("self.path start with /sdc/v1/catalog/services/, generating response json...") jsondata = json.loads(self.data_string) jsonGenerated = "{\"artifactName\":\"" + jsondata['artifactName'] + "\",\"artifactType\":\"" + jsondata['artifactType'] + "\",\"artifactURL\":\"" + self.path + "\",\"artifactDescription\":\"" + jsondata['description'] + "\",\"artifactChecksum\":\"ZjJlMjVmMWE2M2M1OTM2MDZlODlmNTVmZmYzNjViYzM=\",\"artifactUUID\":\"" + str(uuid.uuid4()) + "\",\"artifactVersion\":\"1\"}" - print "jsonGenerated: " + jsonGenerated + print("jsonGenerated: " + jsonGenerated) - os.makedirs(cached_file_folder, 0777) + os.makedirs(cached_file_folder, 0o777) with open(cached_file_header, 'w') as f: f.write("{\"Content-Length\": \"" + str(len(jsonGenerated)) + "\", \"Content-Type\": \"application/json\"}") with open(cached_file_content, 'w') as f: f.write(jsonGenerated) return True elif self.path.startswith("/dcae-deployments/") and http_type == "PUT": - print "self.path start with /dcae-deployments/ DEPLOY, generating response json..." + print("self.path start with /dcae-deployments/ DEPLOY, generating response json...") #jsondata = json.loads(self.data_string) jsonGenerated = "{\"operationType\":\"install\",\"status\":\"processing\",\"links\":{\"status\":\"http:\/\/" + PROXY_ADDRESS + "\/dcae-operationstatus/install?test=test\"}}" - print "jsonGenerated: " + jsonGenerated + print("jsonGenerated: " + jsonGenerated) if not os.path.exists(cached_file_folder): - os.makedirs(cached_file_folder, 0777) + os.makedirs(cached_file_folder, 0o777) with open(cached_file_header, 'w+') as f: f.write("{\"Content-Length\": \"" + str(len(jsonGenerated)) + "\", \"Content-Type\": \"application/json\"}") with open(cached_file_content, 'w+') as f: f.write(jsonGenerated) return True elif self.path.startswith("/dcae-deployments/") and http_type == "DELETE": - print "self.path start with /dcae-deployments/ UNDEPLOY, generating response json..." + print("self.path start with /dcae-deployments/ UNDEPLOY, generating response json...") #jsondata = json.loads(self.data_string) jsonGenerated = "{\"operationType\":\"uninstall\",\"status\":\"processing\",\"links\":{\"status\":\"http:\/\/" + PROXY_ADDRESS + "\/dcae-operationstatus/uninstall?test=test\"}}" - print "jsonGenerated: " + jsonGenerated + print("jsonGenerated: " + jsonGenerated) if not os.path.exists(cached_file_folder): - os.makedirs(cached_file_folder, 0777) + os.makedirs(cached_file_folder, 0o777) with open(cached_file_header, 'w+') as f: f.write("{\"Content-Length\": \"" + str(len(jsonGenerated)) + "\", \"Content-Type\": \"application/json\"}") with open(cached_file_content, 'w+') as f: f.write(jsonGenerated) return True elif (self.path.startswith("/pdp/api/") and (http_type == "PUT" or http_type == "DELETE")) or (self.path.startswith("/pdp/api/policyEngineImport") and http_type == "POST"): - print "self.path start with /pdp/api/, copying body to response ..." + print("self.path start with /pdp/api/, copying body to response ...") if not os.path.exists(cached_file_folder): - os.makedirs(cached_file_folder, 0777) + os.makedirs(cached_file_folder, 0o777) with open(cached_file_header, 'w+') as f: f.write("{\"Content-Length\": \"" + str(len(self.data_string)) + "\", \"Content-Type\": \""+str(self.headers['Content-Type'])+"\"}") with open(cached_file_content, 'w+') as f: f.write(self.data_string) return True elif self.path.startswith("/policy/api/v1/policytypes/") and http_type == "POST": - print "self.path start with POST new policy API /pdp/api/, copying body to response ..." + print("self.path start with POST new policy API /pdp/api/, copying body to response ...") if not os.path.exists(cached_file_folder): - os.makedirs(cached_file_folder, 0777) + os.makedirs(cached_file_folder, 0o777) with open(cached_file_header, 'w+') as f: f.write("{\"Content-Length\": \"" + str(len(self.data_string)) + "\", \"Content-Type\": \""+str(self.headers['Content-Type'])+"\"}") with open(cached_file_content, 'w+') as f: f.write(self.data_string) return True elif self.path.startswith("/policy/api/v1/policytypes/") and http_type == "DELETE": - print "self.path start with DELETE new policy API /policy/api/v1/policytypes/ ..." + print("self.path start with DELETE new policy API /policy/api/v1/policytypes/ ...") if not os.path.exists(cached_file_folder): - os.makedirs(cached_file_folder, 0777) + os.makedirs(cached_file_folder, 0o777) with open(cached_file_header, 'w+') as f: f.write("{\"Content-Length\": \"" + str(len("")) + "\", \"Content-Type\": \""+str("")+"\"}") @@ -241,20 +240,20 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): f.write(self.data_string) return True elif self.path.startswith("/policy/pap/v1/pdps/policies") and http_type == "POST": - print "self.path start with POST new policy API /policy/pap/v1/pdps/ ..." + print("self.path start with POST new policy API /policy/pap/v1/pdps/ ...") if not os.path.exists(cached_file_folder): - os.makedirs(cached_file_folder, 0777) + os.makedirs(cached_file_folder, 0o777) with open(cached_file_header, 'w+') as f: f.write("{\"Content-Length\": \"" + str(len("")) + "\", \"Content-Type\": \""+str("")+"\"}") with open(cached_file_content, 'w+') as f: f.write(self.data_string) return True elif (self.path.startswith("/policy/pap/v1/policies/deployed/")) and http_type == "GET": - print "self.path start with /policy/api/v1/policytypes/, generating response json..." + print("self.path start with /policy/api/v1/policytypes/, generating response json...") jsonGenerated = "{\"policyTypeId\": \"onap.policies.controlloop.operational\",\"policyTypeVersion\": \"1.0.0\",\"policyId\": \"OPERATIONAL_z711F_v1_0_ResourceInstanceName1_tca\"}" - print "jsonGenerated: " + jsonGenerated + print("jsonGenerated: " + jsonGenerated) if not os.path.exists(cached_file_folder): - os.makedirs(cached_file_folder, 0777) + os.makedirs(cached_file_folder, 0o777) with open(cached_file_header, 'w') as f: f.write("{\"Content-Length\": \"" + str(len(jsonGenerated)) + "\", \"Content-Type\": \"application/json\"}") @@ -267,11 +266,11 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): cached_file_folder = '%s/%s' % (TMP_ROOT, self.path) cached_file_content = self._get_cached_content_file_name(cached_file_folder) cached_file_header = self._get_cached_header_file_name(cached_file_folder) - print "self.path start with /dcae-service-types, generating response json..." + print("self.path start with /dcae-service-types, generating response json...") response = "{\"links\": {\"previousLink\": {\"title\": \"string\",\"rel\": \"string\",\"uri\": \"string\",\"uriBuilder\": {},\"rels\": [\"string\"],\"params\": {\"additionalProp1\": \"string\",\"additionalProp2\": \"string\",\"additionalProp3\": \"string\"},\"type\": \"string\"},\"nextLink\": {\"title\": \"string\",\"rel\": \"string\",\"uri\": \"string\",\"uriBuilder\": {},\"rels\": [\"string\"],\"params\": {\"additionalProp1\": \"string\",\"additionalProp2\": \"string\",\"additionalProp3\": \"string\"},\"type\": \"string\"}},\"totalCount\": 1,\"items\": [{\"owner\": \"testOwner\",\"application\": \"testApplication\",\"component\": \"testComponent\",\"typeName\": \"testTypeName\",\"typeVersion\": 0,\"blueprintTemplate\": \"testBlueprintTemplate\",\"serviceIds\": [\"serviceId1\", \"serviceId2\"],\"vnfTypes\": [\"vnfType1\", \"vnfType2\"],\"serviceLocations\": [\"serviceLocation1\", \"serviceLocation2\"],\"asdcServiceId\": \"testAsdcServiceId\",\"asdcResourceId\": \"0\",\"asdcServiceURL\": \"testAsdcServiceURL\",\"typeId\": \"testtypeId\",\"selfLink\": {\"title\": \"selfLinkTitle\",\"rel\": \"selfLinkRel\",\"uri\": \"selfLinkUri\",\"uriBuilder\": {},\"rels\": [\"string\"],\"params\": {\"additionalProp1\": \"string\",\"additionalProp2\": \"string\",\"additionalProp3\": \"string\"},\"type\": \"string\"},\"created\": \"2020-01-22T09:38:15.436Z\",\"deactivated\": \"2020-01-22T09:38:15.437Z\"},{\"owner\": \"testOwner2\",\"application\": \"testApplication1\",\"component\": \"testComponent2\",\"typeName\": \"testTypeName2\",\"typeVersion\": 0,\"blueprintTemplate\": \"testBlueprintTemplate2\",\"serviceIds\": [\"serviceId3\", \"serviceId4\"],\"vnfTypes\": [\"vnfType13\", \"vnfType4\"],\"serviceLocations\": [\"serviceLocation3\", \"serviceLocation4\"],\"asdcServiceId\": \"testAsdcServiceId\",\"asdcResourceId\": \"1\",\"asdcServiceURL\": \"testAsdcServiceURL2\",\"typeId\": \"testtypeId2\",\"selfLink\": {\"title\": \"selfLinkTitle\",\"rel\": \"selfLinkRel\",\"uri\": \"selfLinkUri\",\"uriBuilder\": {},\"rels\": [\"string\"],\"params\": {\"additionalProp1\": \"string\",\"additionalProp2\": \"string\",\"additionalProp3\": \"string\"},\"type\": \"string\"},\"created\": \"2020-01-22T09:38:15.436Z\",\"deactivated\": \"2020-01-22T09:38:15.437Z\"}]}" - print "jsonGenerated: " + response + print("jsonGenerated: " + response) - os.makedirs(cached_file_folder, 0777) + os.makedirs(cached_file_folder, 0o777) with open(cached_file_header, 'w') as f: f.write("{\"Content-Length\": \"" + str(len(response)) + "\", \"Content-Type\": \"application/json\"}") with open(cached_file_content, 'w') as f: @@ -280,50 +279,55 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): elif (self.path.startswith("/onap/controlloop/v2/commission/toscaservicetemplate")) and http_type == "GET": if not _file_available: cached_file_folder = cached_file_folder.split('bridgeEndpoint')[0] - print ("cached file folder for onap is %s: ", cached_file_folder) - print "self.path start with /onap/controlloop/v2/commission/, generating response json..." + print(("cached file folder for onap is %s: ", cached_file_folder)) + print("self.path start with /onap/controlloop/v2/commission/, generating response json...") jsonGenerated = "{\"tosca_definitions_version\": \"tosca_simple_yaml_1_1_0\",\"data_types\": {},\"node_types\": {}, \"policy_types\": {}, \"topology_template\": {}, \"name\": \"ToscaServiceTemplateSimple\", \"version\": \"1.0.0\", \"metadata\": {}}" self._create_cache(jsonGenerated, cached_file_folder, cached_file_header, cached_file_content) return True elif (self.path.startswith("/onap/controlloop/v2/commission/toscaServiceTemplateSchema")) and http_type == "GET": if not _file_available: cached_file_folder = cached_file_folder.split('bridgeEndpoint')[0] - print ("cached file folder for onap is %s: ", cached_file_folder) - print "self.path start with /onap/controlloop/v2/commission/, generating response json..." + print(("cached file folder for onap is %s: ", cached_file_folder)) + print("self.path start with /onap/controlloop/v2/commission/, generating response json...") jsonGenerated = "{\"tosca_definitions_version\": \"tosca_simple_yaml_1_1_0\",\"data_types\": {},\"node_types\": {}, \"policy_types\": {}, \"topology_template\": {}, \"name\": \"ToscaServiceTemplateSimple\", \"version\": \"1.0.0\", \"metadata\": {}}" self._create_cache(jsonGenerated, cached_file_folder, cached_file_header, cached_file_content) return True elif (self.path.startswith("/onap/controlloop/v2/commission/elements")) and http_type == "GET": if not _file_available: - print "self.path start with /commission/elements Control Loop Elements, generating response json..." + print("self.path start with /commission/elements Control Loop Elements, generating response json...") jsonGenerated = "[{\"name\": ,\"org.onap.domain.pmsh.PMSH_DCAEMicroservice\": [{ \"version\": \"1.2.3\", \"derived_from\": null }]}]" self._create_cache(jsonGenerated, cached_file_folder, cached_file_header, cached_file_content) return True elif (self.path.startswith("/onap/controlloop/v2/commission")) and http_type == "GET": if not _file_available: - print "self.path start with /commission control loop definition, generating response json..." + print("self.path start with /commission control loop definition, generating response json...") #jsondata = json.loads(self.data_string) jsonGenerated = "[{\"name\": ,\"org.onap.domain.pmsh.PMSHControlLoopDefinition\": [{ \"version\": \"1.2.3\", \"derived_from\": null }]}]" self._create_cache(jsonGenerated, cached_file_folder, cached_file_header, cached_file_content) return True elif (self.path.startswith("/onap/controlloop/v2/commission/getCommonOrInstanceProperties")) and http_type == "GET": if not _file_available: - print "self.path start with /commission getting common properties, generating response json..." + print("self.path start with /commission getting common properties, generating response json...") with open("example/node_template.json", "r") as f: jsonGenerated = f.read() self._create_cache(jsonGenerated, cached_file_folder, cached_file_header, cached_file_content) return True elif (self.path.startswith("/onap/controlloop/v2/commission")) and http_type == "POST": - print "self.path start with POST /onap/controlloop/v2/commission, copying body to response ..." + print("self.path start with POST /onap/controlloop/v2/commission, copying body to response ...") if not os.path.exists(cached_file_folder): - os.makedirs(cached_file_folder, 0777) + os.makedirs(cached_file_folder, 0o777) with open(cached_file_header, 'w+') as f: f.write("{\"Content-Length\": \"" + str(len(self.data_string)) + "\", \"Content-Type\": \""+str(self.headers['Content-Type'])+"\"}") with open(cached_file_content, 'w+') as f: f.write(self.data_string) return True elif (self.path.startswith("/onap/controlloop/v2/commission")) and http_type == "DELETE": - print "self.path start with /commission Decommissioning, generating response json..." + print("self.path start with /commission Decommissioning, generating response json...") + jsonGenerated = "{\"errorDetails\": null,\"affectedControlLoopDefinitions\": [{ \"name\": \"ToscaServiceTemplateSimple\", \"version\": \"1.0.0\" }]}" + self._create_cache(jsonGenerated, cached_file_folder, cached_file_header, cached_file_content) + return True + elif (self.path.startswith("/onap/controlloop/v2/instantiation")) and http_type == "GET": + print("self.path start with /instantiation Retrieving Instantiation, generating response json...") jsonGenerated = "{\"errorDetails\": null,\"affectedControlLoopDefinitions\": [{ \"name\": \"ToscaServiceTemplateSimple\", \"version\": \"1.0.0\" }]}" self._create_cache(jsonGenerated, cached_file_folder, cached_file_header, cached_file_content) return True @@ -383,13 +387,13 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): self._send_content(cached_file_header, cached_file_content) if self.path.startswith("/dcae-service-types?asdcResourceId="): - print "DCAE case deleting folder created " + cached_file_folder + print("DCAE case deleting folder created " + cached_file_folder) shutil.rmtree(cached_file_folder, ignore_errors=False, onerror=None) elif self.path.startswith("/dcae-service-types"): - print "DCAE case deleting folder created " + cached_file_folder + print("DCAE case deleting folder created " + cached_file_folder) shutil.rmtree(cached_file_folder, ignore_errors=False, onerror=None) else: - print "NOT in DCAE case deleting folder created " + cached_file_folder + print("NOT in DCAE case deleting folder created " + cached_file_folder) def do_POST(self): cached_file_folder = "" @@ -553,7 +557,7 @@ class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): # Main code that start the HTTP server httpd = SocketServer.ForkingTCPServer(('', PORT), Proxy) httpd.allow_reuse_address = True -print "Listening on port "+ str(PORT) + "(Press Ctrl+C/Ctrl+Z to stop HTTPD Caching script)" -print "Caching folder " + CACHE_ROOT + ", Tmp folder for generated files " + TMP_ROOT +print("Listening on port "+ str(PORT) + "(Press Ctrl+C/Ctrl+Z to stop HTTPD Caching script)") +print("Caching folder " + CACHE_ROOT + ", Tmp folder for generated files " + TMP_ROOT) signal.signal(signal.SIGINT, signal_handler) httpd.serve_forever() diff --git a/runtime/ui-react/src/LoopUI.test.js b/runtime/ui-react/src/LoopUI.test.js index bfd6376e3..47ade444f 100644 --- a/runtime/ui-react/src/LoopUI.test.js +++ b/runtime/ui-react/src/LoopUI.test.js @@ -30,142 +30,144 @@ import LoopActionService from './api/LoopActionService'; import LoopService from './api/LoopService'; describe('Verify LoopUI', () => { - beforeEach(() => { - fetch.resetMocks(); - fetch.mockImplementation(() => { - return Promise.resolve({ - ok: true, - status: 200, - text: () => "testUser" - - }); - }); - }) - - const loopCache = new LoopCache({ - "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", - "components": { - "POLICY": { - "componentState": { - "stateName": "UNKNOWN", - "description": "The policies defined have NOT yet been created on the policy engine" - } - }, - "DCAE": { - "componentState": { - "stateName": "BLUEPRINT_DEPLOYED", - "description": "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop" - } - } - } - }); - - it('Test the render method', async () => { - const flushPromises = () => new Promise(setImmediate); - - const component = shallow(<LoopUI />) - component.setState({ - loopName: "testLoopName", - showSucAlert: false, - showFailAlert: false - }); - await flushPromises(); - expect(component).toMatchSnapshot(); - }); - - test('Test closeLoop method', () => { - const historyMock = { push: jest.fn() }; - const component = shallow(<LoopUI history={historyMock}/>) - const instance = component.instance(); - instance.closeLoop(); - - expect(component.state('loopName')).toEqual(OnapConstants.defaultLoopName); - expect(historyMock.push.mock.calls[0]).toEqual([ '/']); - }) - - test('Test loadLoop method refresh suc', async () => { - const historyMock = { push: jest.fn() }; - LoopService.getLoop = jest.fn().mockImplementation(() => { - return Promise.resolve({ - ok: true, - status: 200, - text: () => {} - }); - }); - - LoopActionService.refreshStatus = jest.fn().mockImplementation(() => { - return Promise.resolve({name: "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"}); - }); - - const flushPromises = () => new Promise(setImmediate); - const component = shallow(<LoopUI history={historyMock}/>) - const instance = component.instance(); - instance.loadLoop("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"); - - await flushPromises(); - - const resLoopCache = instance.getLoopCache(); - - expect(resLoopCache.getComponentStates()).toBeUndefined(); - expect(component.state('loopName')).toEqual("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"); - }) - - test('Test loadLoop method refresh fail', async () => { - const historyMock = { push: jest.fn() }; - LoopService.getLoop = jest.fn().mockImplementation(() => { - return Promise.resolve({ - name: "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", - "components": { - "POLICY": { - "componentState": { - "stateName": "UNKNOWN", - "description": "The policies defined have NOT yet been created on the policy engine" - } - }, - "DCAE": { - "componentState": { - "stateName": "BLUEPRINT_DEPLOYED", - "description": "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop" - } - } - }}); - }); - - LoopActionService.refreshStatus = jest.fn().mockImplementation(() => { - return Promise.reject({error: "whatever"}); - }); - - const flushPromises = () => new Promise(setImmediate); - const component = shallow(<LoopUI history={historyMock}/>) - const instance = component.instance(); - instance.loadLoop("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"); - - await flushPromises(); - - const resLoopCache = instance.getLoopCache(); - - expect(resLoopCache).toEqual(loopCache); - expect(component.state('loopName')).toEqual("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"); - }) - - test('Test alert methods', () => { - const component = shallow(<LoopUI />) - expect(component.state('showSucAlert')).toEqual(false); - - const instance = component.instance(); - instance.showSucAlert("testAlert"); - expect(component.state('showSucAlert')).toEqual(true); - expect(component.state('showFailAlert')).toEqual(false); - expect(component.state('showMessage')).toEqual("testAlert"); - - instance.disableAlert(); - - expect(component.state('showSucAlert')).toEqual(false); - expect(component.state('showFailAlert')).toEqual(false); - - instance.showFailAlert("testAlert2"); - expect(component.state('showSucAlert')).toEqual(false); - expect(component.state('showFailAlert')).toEqual(true); - expect(component.state('showMessage')).toEqual("testAlert2"); - }) + beforeEach(() => { + fetch.resetMocks(); + fetch.mockImplementation(() => { + return Promise.resolve({ + ok: true, + status: 200, + text: () => "testUser" + + }); + }); + }) + + const loopCache = new LoopCache({ + "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", + "components": { + "POLICY": { + "componentState": { + "stateName": "UNKNOWN", + "description": "The policies defined have NOT yet been created on the policy engine" + } + }, + "DCAE": { + "componentState": { + "stateName": "BLUEPRINT_DEPLOYED", + "description": "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop" + } + } + } + }); + + it('Test the render method', async () => { + const flushPromises = () => new Promise(setImmediate); + + const component = shallow(<LoopUI/>) + component.setState({ + loopName: "testLoopName", + showSucAlert: false, + showFailAlert: false + }); + await flushPromises(); + expect(component).toMatchSnapshot(); + }); + + test('Test closeLoop method', () => { + const historyMock = { push: jest.fn() }; + const component = shallow(<LoopUI history={ historyMock }/>) + const instance = component.instance(); + instance.closeLoop(); + + expect(component.state('loopName')).toEqual(OnapConstants.defaultLoopName); + expect(historyMock.push.mock.calls[0]).toEqual(['/']); + }) + + test('Test loadLoop method refresh suc', async () => { + const historyMock = { push: jest.fn() }; + LoopService.getLoop = jest.fn().mockImplementation(() => { + return Promise.resolve({ + ok: true, + status: 200, + text: () => { + } + }); + }); + + LoopActionService.refreshStatus = jest.fn().mockImplementation(() => { + return Promise.resolve({ name: "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca" }); + }); + + const flushPromises = () => new Promise(setImmediate); + const component = shallow(<LoopUI history={ historyMock }/>) + const instance = component.instance(); + instance.loadLoop("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"); + + await flushPromises(); + + const resLoopCache = instance.getLoopCache(); + + expect(resLoopCache.getComponentStates()).toBeUndefined(); + expect(component.state('loopName')).toEqual("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"); + }) + + test('Test loadLoop method refresh fail', async () => { + const historyMock = { push: jest.fn() }; + LoopService.getLoop = jest.fn().mockImplementation(() => { + return Promise.resolve({ + name: "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", + "components": { + "POLICY": { + "componentState": { + "stateName": "UNKNOWN", + "description": "The policies defined have NOT yet been created on the policy engine" + } + }, + "DCAE": { + "componentState": { + "stateName": "BLUEPRINT_DEPLOYED", + "description": "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop" + } + } + } + }); + }); + + LoopActionService.refreshStatus = jest.fn().mockImplementation(() => { + return Promise.reject({ error: "whatever" }); + }); + + const flushPromises = () => new Promise(setImmediate); + const component = shallow(<LoopUI history={ historyMock }/>) + const instance = component.instance(); + instance.loadLoop("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"); + + await flushPromises(); + + const resLoopCache = instance.getLoopCache(); + + expect(resLoopCache).toEqual(loopCache); + expect(component.state('loopName')).toEqual("LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca"); + }) + + test('Test alert methods', () => { + const component = shallow(<LoopUI/>) + expect(component.state('showSucAlert')).toEqual(false); + + const instance = component.instance(); + instance.showSucAlert("testAlert"); + expect(component.state('showSucAlert')).toEqual(true); + expect(component.state('showFailAlert')).toEqual(false); + expect(component.state('showMessage')).toEqual("testAlert"); + + instance.disableAlert(); + + expect(component.state('showSucAlert')).toEqual(false); + expect(component.state('showFailAlert')).toEqual(false); + + instance.showFailAlert("testAlert2"); + expect(component.state('showSucAlert')).toEqual(false); + expect(component.state('showFailAlert')).toEqual(true); + expect(component.state('showMessage')).toEqual("testAlert2"); + }) }); diff --git a/runtime/ui-react/src/NotFound.js b/runtime/ui-react/src/NotFound.js index d4b53fd71..fb6d10a8b 100644 --- a/runtime/ui-react/src/NotFound.js +++ b/runtime/ui-react/src/NotFound.js @@ -24,13 +24,13 @@ import React from 'react' export default class NotFound extends React.Component { - render () { - return ( + render() { + return ( <div id='main'> - <div class="divRow"><b>Page Not Found!</b></div> - <div class="divRow">Please cick <a href="/">here</a> to go back to the main page.</div> + <div class="divRow"><b>Page Not Found!</b></div> + <div class="divRow">Please cick <a href="/">here</a> to go back to the main page.</div> </div> - ); - } + ); + } } diff --git a/runtime/ui-react/src/NotFound.test.js b/runtime/ui-react/src/NotFound.test.js index 3a5fc107d..3b4dcb00d 100644 --- a/runtime/ui-react/src/NotFound.test.js +++ b/runtime/ui-react/src/NotFound.test.js @@ -26,11 +26,11 @@ import NotFound from './NotFound'; describe('Verify OnapClamp', () => { - it('Test the render method', () => { + it('Test the render method', () => { - const component = shallow(<NotFound />) + const component = shallow(<NotFound/>) - expect(component).toMatchSnapshot(); - }); + expect(component).toMatchSnapshot(); + }); }); diff --git a/runtime/ui-react/src/OnapClamp.js b/runtime/ui-react/src/OnapClamp.js index 506f6e09d..b8615ac17 100644 --- a/runtime/ui-react/src/OnapClamp.js +++ b/runtime/ui-react/src/OnapClamp.js @@ -27,13 +27,13 @@ import { ThemeProvider } from 'styled-components'; import { DefaultClampTheme } from './theme/globalStyle.js'; export default class OnapClamp extends LoopUI { - - render() { - console.info("Onap Clamp UI starting"); - return ( - <ThemeProvider theme={DefaultClampTheme}> - {super.render()} - </ThemeProvider>); - } + + render() { + console.info("Onap Clamp UI starting"); + return ( + <ThemeProvider theme={ DefaultClampTheme }> + { super.render() } + </ThemeProvider>); + } } diff --git a/runtime/ui-react/src/OnapClamp.test.js b/runtime/ui-react/src/OnapClamp.test.js index c3336a9ab..c711a9537 100644 --- a/runtime/ui-react/src/OnapClamp.test.js +++ b/runtime/ui-react/src/OnapClamp.test.js @@ -26,11 +26,11 @@ import OnapClamp from './OnapClamp'; describe('Verify OnapClamp', () => { - it('Test the render method', () => { + it('Test the render method', () => { - const component = shallow(<OnapClamp />) + const component = shallow(<OnapClamp/>) - expect(component).toMatchSnapshot(); - }); + expect(component).toMatchSnapshot(); + }); }); diff --git a/runtime/ui-react/src/api/LoopActionService.js b/runtime/ui-react/src/api/LoopActionService.js index 18ae90eff..6f3b22fa2 100644 --- a/runtime/ui-react/src/api/LoopActionService.js +++ b/runtime/ui-react/src/api/LoopActionService.js @@ -21,54 +21,54 @@ * */ -export default class LoopActionService{ +export default class LoopActionService { - static performAction(cl_name, uiAction) { - console.info("LoopActionService perform action: " + uiAction + " closedloopName=" + cl_name); - const svcAction = uiAction.toLowerCase(); - return fetch(window.location.pathname + "restservices/clds/v2/loop/" + svcAction + "/" + cl_name, { - method: 'PUT', - credentials: 'same-origin' - }) - .then(function (response) { - if (response.ok) { - return response; - } else { - return Promise.reject("Perform action failed with code:" + response.status); - } - }) - .then(function (data) { - console.info("Action Successful: " + uiAction); - return data; - }) - .catch(function(error) { - console.info("Action Failure: " + uiAction); - return Promise.reject(error); - }); - } + static performAction(cl_name, uiAction) { + console.info("LoopActionService perform action: " + uiAction + " closedloopName=" + cl_name); + const svcAction = uiAction.toLowerCase(); + return fetch(window.location.pathname + "restservices/clds/v2/loop/" + svcAction + "/" + cl_name, { + method: 'PUT', + credentials: 'same-origin' + }) + .then(function (response) { + if (response.ok) { + return response; + } else { + return Promise.reject("Perform action failed with code:" + response.status); + } + }) + .then(function (data) { + console.info("Action Successful: " + uiAction); + return data; + }) + .catch(function (error) { + console.info("Action Failure: " + uiAction); + return Promise.reject(error); + }); + } - static refreshStatus(cl_name) { - console.info("Refresh the status for closedloopName=" + cl_name); + static refreshStatus(cl_name) { + console.info("Refresh the status for closedloopName=" + cl_name); - return fetch(window.location.pathname + "restservices/clds/v2/loop/getstatus/" + cl_name, { - method: 'GET', - credentials: 'same-origin' - }) - .then(function (response) { - if (response.ok) { - return response.json(); - } else { - return Promise.reject("Refresh status failed with code:" + response.status); - } - }) - .then(function (data) { - console.info ("Refresh status Successful"); - return data; - }) - .catch(function(error) { - console.info ("Refresh status failed:", error); - return Promise.reject(error); - }); - } + return fetch(window.location.pathname + "restservices/clds/v2/loop/getstatus/" + cl_name, { + method: 'GET', + credentials: 'same-origin' + }) + .then(function (response) { + if (response.ok) { + return response.json(); + } else { + return Promise.reject("Refresh status failed with code:" + response.status); + } + }) + .then(function (data) { + console.info("Refresh status Successful"); + return data; + }) + .catch(function (error) { + console.info("Refresh status failed:", error); + return Promise.reject(error); + }); + } } diff --git a/runtime/ui-react/src/api/LoopCache.js b/runtime/ui-react/src/api/LoopCache.js index 3e19b4fc7..ac2da07f2 100644 --- a/runtime/ui-react/src/api/LoopCache.js +++ b/runtime/ui-react/src/api/LoopCache.js @@ -22,231 +22,231 @@ */ export default class LoopCache { - loopJsonCache; - - constructor(loopJson) { - this.loopJsonCache=loopJson; - } - - updateMicroServiceProperties(name, newMsProperties) { - for (var policy in this.loopJsonCache["microServicePolicies"]) { - if (this.loopJsonCache["microServicePolicies"][policy]["name"] === name) { - this.loopJsonCache["microServicePolicies"][policy]["configurationsJson"] = newMsProperties; - } - } - } - - updateMicroServicePdpGroup(name, pdpGroup, pdpSubgroup) { - for (var policy in this.loopJsonCache["microServicePolicies"]) { - if (this.loopJsonCache["microServicePolicies"][policy]["name"] === name) { - this.loopJsonCache["microServicePolicies"][policy]["pdpGroup"] = pdpGroup; - this.loopJsonCache["microServicePolicies"][policy]["pdpSubgroup"] = pdpSubgroup; - } - } - } - - updateGlobalProperties(newGlobalProperties) { - this.loopJsonCache["globalPropertiesJson"] = newGlobalProperties; - } - - updateOperationalPolicyProperties(name, newOpProperties) { - for (var policy in this.loopJsonCache["operationalPolicies"]) { - if (this.loopJsonCache["operationalPolicies"][policy]["name"] === name) { - this.loopJsonCache["operationalPolicies"][policy]["configurationsJson"] = newOpProperties; - } - } - } - - updateOperationalPolicyPdpGroup(name, pdpGroup, pdpSubgroup) { - for (var policy in this.loopJsonCache["operationalPolicies"]) { - if (this.loopJsonCache["operationalPolicies"][policy]["name"] === name) { - this.loopJsonCache["operationalPolicies"][policy]["pdpGroup"] = pdpGroup; - this.loopJsonCache["operationalPolicies"][policy]["pdpSubgroup"] = pdpSubgroup; - } - } - } - - getLoopName() { - return this.loopJsonCache["name"]; - } - - getOperationalPolicyJsonSchema() { - return this.loopJsonCache["operationalPolicies"]["0"]["jsonRepresentation"]; - } - - getOperationalPolicies() { - return this.loopJsonCache["operationalPolicies"]; - } - - getOperationalPoliciesNoJsonSchema() { - var operationalPolicies = JSON.parse(JSON.stringify(this.loopJsonCache["operationalPolicies"])); - delete operationalPolicies[0]["jsonRepresentation"]; - return operationalPolicies; - } - - getGlobalProperties() { - return this.loopJsonCache["globalPropertiesJson"]; - } - - getDcaeDeploymentProperties() { - return this.loopJsonCache["globalPropertiesJson"]["dcaeDeployParameters"]; - } - - getMicroServicePolicies() { - return this.loopJsonCache["microServicePolicies"]; - } - - getOperationalPolicyForName(name) { - var opProperties=this.getOperationalPolicies(); - for (var policy in opProperties) { - if (opProperties[policy]["name"] === name) { - return opProperties[policy]; - } - } - return null; - } - - getOperationalPolicyPropertiesForName(name) { - var opConfig = this.getOperationalPolicyForName(name); - if (opConfig !== null) { - return opConfig["configurationsJson"]; - } - return null; - } - - getOperationalPolicyJsonRepresentationForName(name) { - var opConfig = this.getOperationalPolicyForName(name); - if (opConfig !== null) { - return opConfig["jsonRepresentation"]; - } - return null; - } - - getOperationalPolicySupportedPdpGroup(name) { - var opConfig=this.getOperationalPolicyForName(name); - if (opConfig !== null) { - if (opConfig["policyModel"]["policyPdpGroup"] !== undefined && opConfig["policyModel"]["policyPdpGroup"]["supportedPdpGroups"] !== undefined) { - return opConfig["policyModel"]["policyPdpGroup"]["supportedPdpGroups"]; - } - } - return []; - } - - getOperationalPolicyPdpGroup(name) { - var opConfig=this.getOperationalPolicyForName(name); - if (opConfig !== null) { - return opConfig["pdpGroup"]; - } - return null; - } - - getOperationalPolicyPdpSubgroup(name) { - var opConfig=this.getOperationalPolicyForName(name); - if (opConfig !== null) { - return opConfig["pdpSubgroup"]; - } - return null; - } - - getMicroServiceSupportedPdpGroup(name) { - var microService=this.getMicroServiceForName(name); - if (microService !== null) { - if (microService["policyModel"]["policyPdpGroup"] !== undefined && microService["policyModel"]["policyPdpGroup"]["supportedPdpGroups"] !== undefined) { - return microService["policyModel"]["policyPdpGroup"]["supportedPdpGroups"]; - } - } - return []; - } - - getMicroServicePdpGroup(name) { - var microService=this.getMicroServiceForName(name); - if (microService !== null) { - return microService["pdpGroup"]; - } - return null; - } - - getMicroServicePdpSubgroup(name) { - var microService=this.getMicroServiceForName(name); - if (microService !== null) { - return microService["pdpSubgroup"]; - } - return null; - } - - getMicroServiceForName(name) { - var msProperties=this.getMicroServicePolicies(); - for (var policy in msProperties) { - if (msProperties[policy]["name"] === name) { - return msProperties[policy]; - } - } - return null; - } - - getMicroServicePropertiesForName(name) { - var msConfig = this.getMicroServiceForName(name); - if (msConfig !== null) { - return msConfig["configurationsJson"]; - } - return null; - } - - getMicroServiceJsonRepresentationForName(name) { - var msConfig = this.getMicroServiceForName(name); - if (msConfig !== null) { - return msConfig["jsonRepresentation"]; - } - return null; - } - - getResourceDetailsVfProperty() { - return this.loopJsonCache["modelService"]["resourceDetails"]["VF"]; - } - - getResourceDetailsVfModuleProperty() { - return this.loopJsonCache["modelService"]["resourceDetails"]["VFModule"]; - } - - getLoopLogsArray() { - return this.loopJsonCache.loopLogs; - } - - getComputedState() { - return this.loopJsonCache.lastComputedState; - } - - getComponentStates() { - return this.loopJsonCache.components; - } - - getTemplateName() { - if (this.getLoopTemplate() !== undefined) { - return this.getLoopTemplate().name; - } - return null; - } - - getLoopTemplate() { - return this.loopJsonCache["loopTemplate"]; - } - - isOpenLoopTemplate() { - var loopTemplate = this.getLoopTemplate(); - if(loopTemplate != null && loopTemplate["allowedLoopType"] === "OPEN") { - return true; - } - return false; - } - - getAllLoopElementModels() { - var loopTemplate = this.getLoopTemplate(); - var loopElementModels = []; - if(loopTemplate != null) { - for (var element of loopTemplate['loopElementModelsUsed']) { - loopElementModels.push(element['loopElementModel']) - } - } - return loopElementModels; - } + loopJsonCache; + + constructor(loopJson) { + this.loopJsonCache = loopJson; + } + + updateMicroServiceProperties(name, newMsProperties) { + for (var policy in this.loopJsonCache["microServicePolicies"]) { + if (this.loopJsonCache["microServicePolicies"][policy]["name"] === name) { + this.loopJsonCache["microServicePolicies"][policy]["configurationsJson"] = newMsProperties; + } + } + } + + updateMicroServicePdpGroup(name, pdpGroup, pdpSubgroup) { + for (var policy in this.loopJsonCache["microServicePolicies"]) { + if (this.loopJsonCache["microServicePolicies"][policy]["name"] === name) { + this.loopJsonCache["microServicePolicies"][policy]["pdpGroup"] = pdpGroup; + this.loopJsonCache["microServicePolicies"][policy]["pdpSubgroup"] = pdpSubgroup; + } + } + } + + updateGlobalProperties(newGlobalProperties) { + this.loopJsonCache["globalPropertiesJson"] = newGlobalProperties; + } + + updateOperationalPolicyProperties(name, newOpProperties) { + for (var policy in this.loopJsonCache["operationalPolicies"]) { + if (this.loopJsonCache["operationalPolicies"][policy]["name"] === name) { + this.loopJsonCache["operationalPolicies"][policy]["configurationsJson"] = newOpProperties; + } + } + } + + updateOperationalPolicyPdpGroup(name, pdpGroup, pdpSubgroup) { + for (var policy in this.loopJsonCache["operationalPolicies"]) { + if (this.loopJsonCache["operationalPolicies"][policy]["name"] === name) { + this.loopJsonCache["operationalPolicies"][policy]["pdpGroup"] = pdpGroup; + this.loopJsonCache["operationalPolicies"][policy]["pdpSubgroup"] = pdpSubgroup; + } + } + } + + getLoopName() { + return this.loopJsonCache["name"]; + } + + getOperationalPolicyJsonSchema() { + return this.loopJsonCache["operationalPolicies"]["0"]["jsonRepresentation"]; + } + + getOperationalPolicies() { + return this.loopJsonCache["operationalPolicies"]; + } + + getOperationalPoliciesNoJsonSchema() { + var operationalPolicies = JSON.parse(JSON.stringify(this.loopJsonCache["operationalPolicies"])); + delete operationalPolicies[0]["jsonRepresentation"]; + return operationalPolicies; + } + + getGlobalProperties() { + return this.loopJsonCache["globalPropertiesJson"]; + } + + getDcaeDeploymentProperties() { + return this.loopJsonCache["globalPropertiesJson"]["dcaeDeployParameters"]; + } + + getMicroServicePolicies() { + return this.loopJsonCache["microServicePolicies"]; + } + + getOperationalPolicyForName(name) { + var opProperties = this.getOperationalPolicies(); + for (var policy in opProperties) { + if (opProperties[policy]["name"] === name) { + return opProperties[policy]; + } + } + return null; + } + + getOperationalPolicyPropertiesForName(name) { + var opConfig = this.getOperationalPolicyForName(name); + if (opConfig !== null) { + return opConfig["configurationsJson"]; + } + return null; + } + + getOperationalPolicyJsonRepresentationForName(name) { + var opConfig = this.getOperationalPolicyForName(name); + if (opConfig !== null) { + return opConfig["jsonRepresentation"]; + } + return null; + } + + getOperationalPolicySupportedPdpGroup(name) { + var opConfig = this.getOperationalPolicyForName(name); + if (opConfig !== null) { + if (opConfig["policyModel"]["policyPdpGroup"] !== undefined && opConfig["policyModel"]["policyPdpGroup"]["supportedPdpGroups"] !== undefined) { + return opConfig["policyModel"]["policyPdpGroup"]["supportedPdpGroups"]; + } + } + return []; + } + + getOperationalPolicyPdpGroup(name) { + var opConfig = this.getOperationalPolicyForName(name); + if (opConfig !== null) { + return opConfig["pdpGroup"]; + } + return null; + } + + getOperationalPolicyPdpSubgroup(name) { + var opConfig = this.getOperationalPolicyForName(name); + if (opConfig !== null) { + return opConfig["pdpSubgroup"]; + } + return null; + } + + getMicroServiceSupportedPdpGroup(name) { + var microService = this.getMicroServiceForName(name); + if (microService !== null) { + if (microService["policyModel"]["policyPdpGroup"] !== undefined && microService["policyModel"]["policyPdpGroup"]["supportedPdpGroups"] !== undefined) { + return microService["policyModel"]["policyPdpGroup"]["supportedPdpGroups"]; + } + } + return []; + } + + getMicroServicePdpGroup(name) { + var microService = this.getMicroServiceForName(name); + if (microService !== null) { + return microService["pdpGroup"]; + } + return null; + } + + getMicroServicePdpSubgroup(name) { + var microService = this.getMicroServiceForName(name); + if (microService !== null) { + return microService["pdpSubgroup"]; + } + return null; + } + + getMicroServiceForName(name) { + var msProperties = this.getMicroServicePolicies(); + for (var policy in msProperties) { + if (msProperties[policy]["name"] === name) { + return msProperties[policy]; + } + } + return null; + } + + getMicroServicePropertiesForName(name) { + var msConfig = this.getMicroServiceForName(name); + if (msConfig !== null) { + return msConfig["configurationsJson"]; + } + return null; + } + + getMicroServiceJsonRepresentationForName(name) { + var msConfig = this.getMicroServiceForName(name); + if (msConfig !== null) { + return msConfig["jsonRepresentation"]; + } + return null; + } + + getResourceDetailsVfProperty() { + return this.loopJsonCache["modelService"]["resourceDetails"]["VF"]; + } + + getResourceDetailsVfModuleProperty() { + return this.loopJsonCache["modelService"]["resourceDetails"]["VFModule"]; + } + + getLoopLogsArray() { + return this.loopJsonCache.loopLogs; + } + + getComputedState() { + return this.loopJsonCache.lastComputedState; + } + + getComponentStates() { + return this.loopJsonCache.components; + } + + getTemplateName() { + if (this.getLoopTemplate() !== undefined) { + return this.getLoopTemplate().name; + } + return null; + } + + getLoopTemplate() { + return this.loopJsonCache["loopTemplate"]; + } + + isOpenLoopTemplate() { + var loopTemplate = this.getLoopTemplate(); + if (loopTemplate != null && loopTemplate["allowedLoopType"] === "OPEN") { + return true; + } + return false; + } + + getAllLoopElementModels() { + var loopTemplate = this.getLoopTemplate(); + var loopElementModels = []; + if (loopTemplate != null) { + for (var element of loopTemplate['loopElementModelsUsed']) { + loopElementModels.push(element['loopElementModel']) + } + } + return loopElementModels; + } } diff --git a/runtime/ui-react/src/api/LoopCacheMockFile.json b/runtime/ui-react/src/api/LoopCacheMockFile.json index cb9ed87b0..e5a670218 100644 --- a/runtime/ui-react/src/api/LoopCacheMockFile.json +++ b/runtime/ui-react/src/api/LoopCacheMockFile.json @@ -8,7 +8,9 @@ "policy_id": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca" } }, - "loopTemplate": {"name": "loopTemplateTest"}, + "loopTemplate": { + "name": "loopTemplateTest" + }, "modelService": { "serviceDetails": { "serviceType": "", @@ -105,12 +107,20 @@ { "name": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca", "modelType": "onap.policies.monitoring.cdap.tca.hi.lo.app", - "configurationsJson": {"domain": "measurementsForVfScaling"}, + "configurationsJson": { + "domain": "measurementsForVfScaling" + }, "shared": false, "pdpGroup": "pdpGroupTest", "pdpSubgroup": "pdpSubgroupTest", - "policyModel": {"policyPdpGroup": {"supportedPdpGroups": "supportedPdpGroupsTest"}}, - "jsonRepresentation": {"schema": {}} + "policyModel": { + "policyPdpGroup": { + "supportedPdpGroups": "supportedPdpGroupsTest" + } + }, + "jsonRepresentation": { + "schema": {} + } } ], "loopLogs": [ diff --git a/runtime/ui-react/src/api/LoopService.js b/runtime/ui-react/src/api/LoopService.js index 96bb8a0a7..16f1bec9e 100644 --- a/runtime/ui-react/src/api/LoopService.js +++ b/runtime/ui-react/src/api/LoopService.js @@ -21,224 +21,224 @@ */ export default class LoopService { - static getLoopNames() { - return fetch(window.location.pathname + 'restservices/clds/v2/loop/getAllNames', { method: 'GET', credentials: 'same-origin' }) - .then(function (response) { - console.debug("GetLoopNames response received: ", response.status); - if (response.ok) { - return response.json(); - } else { - console.error("GetLoopNames query failed"); - return {}; - } - }) - .catch(function (error) { - console.error("GetLoopNames error received", error); - return {}; - }); - } + static getLoopNames() { + return fetch(window.location.pathname + 'restservices/clds/v2/loop/getAllNames', { method: 'GET', credentials: 'same-origin' }) + .then(function (response) { + console.debug("GetLoopNames response received: ", response.status); + if (response.ok) { + return response.json(); + } else { + console.error("GetLoopNames query failed"); + return {}; + } + }) + .catch(function (error) { + console.error("GetLoopNames error received", error); + return {}; + }); + } - static createLoop(loopName, templateName) { - return fetch(window.location.pathname + 'restservices/clds/v2/loop/create/' + loopName + '?templateName=' + templateName, { - method: 'POST', - headers: { - "Content-Type": "application/json" - }, - credentials: 'same-origin' - }) - .then(function (response) { - console.debug("CreateLoop response received: ", response.status); - return response.json(); - }) - .catch(function (error) { - console.error("CreateLoop error received", error); - return ""; - }); - } + static createLoop(loopName, templateName) { + return fetch(window.location.pathname + 'restservices/clds/v2/loop/create/' + loopName + '?templateName=' + templateName, { + method: 'POST', + headers: { + "Content-Type": "application/json" + }, + credentials: 'same-origin' + }) + .then(function (response) { + console.debug("CreateLoop response received: ", response.status); + return response.json(); + }) + .catch(function (error) { + console.error("CreateLoop error received", error); + return ""; + }); + } - static getLoop(loopName) { - return fetch(window.location.pathname + 'restservices/clds/v2/loop/' + loopName, { - method: 'GET', - headers: { - "Content-Type": "application/json" - }, - credentials: 'same-origin' - }) - .then(function (response) { - console.debug("GetLoop response received: ", response.status); - if (response.ok) { - return response.json(); - } else { - console.error("GetLoop query failed"); - return {}; - } - }) - .catch(function (error) { - console.error("GetLoop error received", error); - return {}; - }); - } + static getLoop(loopName) { + return fetch(window.location.pathname + 'restservices/clds/v2/loop/' + loopName, { + method: 'GET', + headers: { + "Content-Type": "application/json" + }, + credentials: 'same-origin' + }) + .then(function (response) { + console.debug("GetLoop response received: ", response.status); + if (response.ok) { + return response.json(); + } else { + console.error("GetLoop query failed"); + return {}; + } + }) + .catch(function (error) { + console.error("GetLoop error received", error); + return {}; + }); + } - static setMicroServiceProperties(loopName, jsonData) { - return fetch(window.location.pathname + 'restservices/clds/v2/loop/updateMicroservicePolicy/' + loopName, { - method: 'POST', - credentials: 'same-origin', - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify(jsonData) - }) - .then(function (response) { - console.debug("updateMicroservicePolicy response received: ", response.status); - if (response.ok) { - return response.text(); - } else { - console.error("updateMicroservicePolicy query failed"); - return ""; - } - }) - .catch(function (error) { - console.error("updateMicroservicePolicy error received", error); - return ""; - }); - } - - static setOperationalPolicyProperties(loopName, jsonData) { - return fetch(window.location.pathname + 'restservices/clds/v2/loop/updateOperationalPolicies/' + loopName, { - method: 'POST', - credentials: 'same-origin', - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify(jsonData) - }) - .then(function (response) { - console.debug("updateOperationalPolicies response received: ", response.status); - if (response.ok) { - return response.text(); - } else { - console.error("updateOperationalPolicies query failed"); - return ""; - } - }) - .catch(function (error) { - console.error("updateOperationalPolicies error received", error); - return ""; - }); - } + static setMicroServiceProperties(loopName, jsonData) { + return fetch(window.location.pathname + 'restservices/clds/v2/loop/updateMicroservicePolicy/' + loopName, { + method: 'POST', + credentials: 'same-origin', + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify(jsonData) + }) + .then(function (response) { + console.debug("updateMicroservicePolicy response received: ", response.status); + if (response.ok) { + return response.text(); + } else { + console.error("updateMicroservicePolicy query failed"); + return ""; + } + }) + .catch(function (error) { + console.error("updateMicroservicePolicy error received", error); + return ""; + }); + } - static updateGlobalProperties(loopName, jsonData) { - return fetch(window.location.pathname + 'restservices/clds/v2/loop/updateGlobalProperties/' + loopName, { - method: 'POST', - credentials: 'same-origin', - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify(jsonData) - }) - .then(function (response) { - console.debug("updateGlobalProperties response received: ", response.status); - if (response.ok) { - return response.text(); - } else { - console.error("updateGlobalProperties query failed"); - return ""; - } - }) - .catch(function (error) { - console.error("updateGlobalProperties error received", error); - return ""; - }); - } + static setOperationalPolicyProperties(loopName, jsonData) { + return fetch(window.location.pathname + 'restservices/clds/v2/loop/updateOperationalPolicies/' + loopName, { + method: 'POST', + credentials: 'same-origin', + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify(jsonData) + }) + .then(function (response) { + console.debug("updateOperationalPolicies response received: ", response.status); + if (response.ok) { + return response.text(); + } else { + console.error("updateOperationalPolicies query failed"); + return ""; + } + }) + .catch(function (error) { + console.error("updateOperationalPolicies error received", error); + return ""; + }); + } - static refreshOperationalPolicyJson(loopName,operationalPolicyName) { - return fetch(window.location.pathname + 'restservices/clds/v2/loop/refreshOperationalPolicyJsonSchema/' + loopName + '/' + operationalPolicyName, { - method: 'PUT', - headers: { - "Content-Type": "application/json" - }, - credentials: 'same-origin' - }) - .then(function (response) { - console.debug("Refresh Operational Policy Json Schema response received: ", response.status); - if (response.ok) { - return response.json(); - } else { - console.error("Refresh Operational Policy Json Schema query failed"); - return {}; - } - }) - .catch(function (error) { - console.error("Refresh Operational Policy Json Schema error received", error); - return {}; - }); - } + static updateGlobalProperties(loopName, jsonData) { + return fetch(window.location.pathname + 'restservices/clds/v2/loop/updateGlobalProperties/' + loopName, { + method: 'POST', + credentials: 'same-origin', + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify(jsonData) + }) + .then(function (response) { + console.debug("updateGlobalProperties response received: ", response.status); + if (response.ok) { + return response.text(); + } else { + console.error("updateGlobalProperties query failed"); + return ""; + } + }) + .catch(function (error) { + console.error("updateGlobalProperties error received", error); + return ""; + }); + } - static refreshMicroServicePolicyJson(loopName,microServicePolicyName) { - return fetch(window.location.pathname + 'restservices/clds/v2/loop/refreshMicroServicePolicyJsonSchema/' + loopName + '/' + microServicePolicyName, { - method: 'PUT', - headers: { - "Content-Type": "application/json" - }, - credentials: 'same-origin' - }) - .then(function (response) { - console.debug("Refresh Operational Policy Json Schema response received: ", response.status); - if (response.ok) { - return response.json(); - } else { - console.error("Refresh Operational Policy Json Schema query failed"); - return {}; - } - }) - .catch(function (error) { - console.error("Refresh Operational Policy Json Schema error received", error); - return {}; - }); - } + static refreshOperationalPolicyJson(loopName, operationalPolicyName) { + return fetch(window.location.pathname + 'restservices/clds/v2/loop/refreshOperationalPolicyJsonSchema/' + loopName + '/' + operationalPolicyName, { + method: 'PUT', + headers: { + "Content-Type": "application/json" + }, + credentials: 'same-origin' + }) + .then(function (response) { + console.debug("Refresh Operational Policy Json Schema response received: ", response.status); + if (response.ok) { + return response.json(); + } else { + console.error("Refresh Operational Policy Json Schema query failed"); + return {}; + } + }) + .catch(function (error) { + console.error("Refresh Operational Policy Json Schema error received", error); + return {}; + }); + } - static addOperationalPolicyType(loopName, policyType, policyVersion) { - return fetch(window.location.pathname + 'restservices/clds/v2/loop/addOperationaPolicy/' + loopName + '/policyModel/' + policyType +'/' + policyVersion , { - method: 'PUT', - headers: { - "Content-Type": "application/json" - }, - credentials: 'same-origin' - }) - .then(function (response) { - console.debug("Add Operational Policy response received: ", response.status); - if (response.ok) { - return response.json(); - } else { - return response.text(); - } - }) - .catch(function (error) { - console.error("Add Operational Policy query failed"); - throw new Error(error); - }) - } + static refreshMicroServicePolicyJson(loopName, microServicePolicyName) { + return fetch(window.location.pathname + 'restservices/clds/v2/loop/refreshMicroServicePolicyJsonSchema/' + loopName + '/' + microServicePolicyName, { + method: 'PUT', + headers: { + "Content-Type": "application/json" + }, + credentials: 'same-origin' + }) + .then(function (response) { + console.debug("Refresh Operational Policy Json Schema response received: ", response.status); + if (response.ok) { + return response.json(); + } else { + console.error("Refresh Operational Policy Json Schema query failed"); + return {}; + } + }) + .catch(function (error) { + console.error("Refresh Operational Policy Json Schema error received", error); + return {}; + }); + } - static removeOperationalPolicyType(loopName, policyType, policyVersion, policyName) { - return fetch(window.location.pathname + 'restservices/clds/v2/loop/removeOperationaPolicy/' + loopName + '/policyModel/' + policyType +'/' + policyVersion + '/' + policyName , { - method: 'PUT', - headers: { - "Content-Type": "application/json" - }, - credentials: 'same-origin' - }) - .then(function (response) { - console.debug("Remove Operational Policy response received: ", response.status); - if (response.ok) { - return response.json(); - } else { - console.error("Remove Operational Policy query failed"); - return {}; - } - }) - .catch(function (error) { - console.error("Remove Operational Policy error received", error); - return {}; - }); - } + static addOperationalPolicyType(loopName, policyType, policyVersion) { + return fetch(window.location.pathname + 'restservices/clds/v2/loop/addOperationaPolicy/' + loopName + '/policyModel/' + policyType + '/' + policyVersion, { + method: 'PUT', + headers: { + "Content-Type": "application/json" + }, + credentials: 'same-origin' + }) + .then(function (response) { + console.debug("Add Operational Policy response received: ", response.status); + if (response.ok) { + return response.json(); + } else { + return response.text(); + } + }) + .catch(function (error) { + console.error("Add Operational Policy query failed"); + throw new Error(error); + }) + } + + static removeOperationalPolicyType(loopName, policyType, policyVersion, policyName) { + return fetch(window.location.pathname + 'restservices/clds/v2/loop/removeOperationaPolicy/' + loopName + '/policyModel/' + policyType + '/' + policyVersion + '/' + policyName, { + method: 'PUT', + headers: { + "Content-Type": "application/json" + }, + credentials: 'same-origin' + }) + .then(function (response) { + console.debug("Remove Operational Policy response received: ", response.status); + if (response.ok) { + return response.json(); + } else { + console.error("Remove Operational Policy query failed"); + return {}; + } + }) + .catch(function (error) { + console.error("Remove Operational Policy error received", error); + return {}; + }); + } } diff --git a/runtime/ui-react/src/api/PoliciesListCacheMockFile.json b/runtime/ui-react/src/api/PoliciesListCacheMockFile.json index 5c6bd5b6b..14d5cc653 100644 --- a/runtime/ui-react/src/api/PoliciesListCacheMockFile.json +++ b/runtime/ui-react/src/api/PoliciesListCacheMockFile.json @@ -212,4 +212,4 @@ } } ] -}
\ No newline at end of file +} diff --git a/runtime/ui-react/src/api/PolicyService.js b/runtime/ui-react/src/api/PolicyService.js index f2886b30a..cf4f0fdba 100644 --- a/runtime/ui-react/src/api/PolicyService.js +++ b/runtime/ui-react/src/api/PolicyService.js @@ -23,126 +23,130 @@ export default class PolicyService { static getPoliciesList() { return fetch(window.location.pathname + 'restservices/clds/v2/policies', { - method: 'GET', - credentials: 'same-origin' - }) - .then(function(response) { - console.debug("getPoliciesList response received: ", response.status); - if (response.ok) { - console.info("getPoliciesList query successful"); - return response.json(); - } else { - return response.text().then(responseBody => { - throw new Error("HTTP " + response.status + "," + responseBody); - }) - } - }) - .catch(function(error) { - console.error("getPoliciesList error occurred ", error); - alert("getPoliciesList error occurred " + error); - return undefined; - }); + method: 'GET', + credentials: 'same-origin' + }) + .then(function (response) { + console.debug("getPoliciesList response received: ", response.status); + if (response.ok) { + console.info("getPoliciesList query successful"); + return response.json(); + } else { + return response.text().then(responseBody => { + throw new Error("HTTP " + response.status + "," + responseBody); + }) + } + }) + .catch(function (error) { + console.error("getPoliciesList error occurred ", error); + alert("getPoliciesList error occurred " + error); + return undefined; + }); } + static createNewPolicy(policyModelType, policyModelVersion, policyName, policyVersion, policyJson) { return fetch(window.location.pathname + 'restservices/clds/v2/policies/' + policyModelType + '/' - + policyModelVersion + '/' + policyName + '/' + policyVersion, { - method: 'POST', - credentials: 'same-origin', - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify(policyJson) - }) - .then(function (response) { - console.debug("createNewPolicy response received: ", response.status); - if (response.ok) { - console.info("createNewPolicy query successful"); - return response.text(); - } else { - return response.text().then(responseBody => { - throw new Error("HTTP " + response.status + "," + responseBody); - }) - } - }) - .catch(function (error) { - console.error("createNewPolicy error occurred ", error); - alert ("createNewPolicy error occurred " + error); - return undefined; - }); + + policyModelVersion + '/' + policyName + '/' + policyVersion, { + method: 'POST', + credentials: 'same-origin', + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify(policyJson) + }) + .then(function (response) { + console.debug("createNewPolicy response received: ", response.status); + if (response.ok) { + console.info("createNewPolicy query successful"); + return response.text(); + } else { + return response.text().then(responseBody => { + throw new Error("HTTP " + response.status + "," + responseBody); + }) + } + }) + .catch(function (error) { + console.error("createNewPolicy error occurred ", error); + alert("createNewPolicy error occurred " + error); + return undefined; + }); } + static deletePolicy(policyModelType, policyModelVersion, policyName, policyVersion) { return fetch(window.location.pathname + 'restservices/clds/v2/policies/' + policyModelType + '/' - + policyModelVersion + '/' + policyName + '/' + policyVersion, { - method: 'DELETE', - credentials: 'same-origin' - }) - .then(function (response) { - console.debug("deletePolicy response received: ", response.status); - if (response.ok) { - console.info("deletePolicy query successful"); - return response.text(); - } else { - return response.text().then(responseBody => { - throw new Error("HTTP " + response.status + "," + responseBody); - }) - } - }) - .catch(function (error) { - console.error("deletePolicy error occurred ", error); - alert ("deletePolicy error occurred " + error); - return undefined; - }); + + policyModelVersion + '/' + policyName + '/' + policyVersion, { + method: 'DELETE', + credentials: 'same-origin' + }) + .then(function (response) { + console.debug("deletePolicy response received: ", response.status); + if (response.ok) { + console.info("deletePolicy query successful"); + return response.text(); + } else { + return response.text().then(responseBody => { + throw new Error("HTTP " + response.status + "," + responseBody); + }) + } + }) + .catch(function (error) { + console.error("deletePolicy error occurred ", error); + alert("deletePolicy error occurred " + error); + return undefined; + }); } + static updatePdpDeployment(pdpOperationsList) { return fetch(window.location.pathname + 'restservices/clds/v2/policies/pdpDeployment', { - method: 'PUT', - credentials: 'same-origin', - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify(pdpOperationsList) - }) - .then(function (response) { - console.debug("updatePdpDeployment response received: ", response.status); - if (response.ok) { - console.info("updatePdpDeployment query successful"); - return response.text(); - } else { - return response.text().then(responseBody => { - throw new Error("HTTP " + response.status + "," + responseBody); - }) - } - }) - .catch(function (error) { - console.error("updatePdpDeployment error occurred ", error); - alert ("updatePdpDeployment error occurred " + error); - return undefined; - }); + method: 'PUT', + credentials: 'same-origin', + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify(pdpOperationsList) + }) + .then(function (response) { + console.debug("updatePdpDeployment response received: ", response.status); + if (response.ok) { + console.info("updatePdpDeployment query successful"); + return response.text(); + } else { + return response.text().then(responseBody => { + throw new Error("HTTP " + response.status + "," + responseBody); + }) + } + }) + .catch(function (error) { + console.error("updatePdpDeployment error occurred ", error); + alert("updatePdpDeployment error occurred " + error); + return undefined; + }); } + static sendNewPolicyModel(newPolicyModel) { - return fetch(window.location.pathname + 'restservices/clds/v2/policies/policytype', { - method: 'POST', - credentials: 'same-origin', - headers: { - "Content-Type": "plain/text" - }, - body: newPolicyModel + return fetch(window.location.pathname + 'restservices/clds/v2/policies/policytype', { + method: 'POST', + credentials: 'same-origin', + headers: { + "Content-Type": "plain/text" + }, + body: newPolicyModel + }) + .then(function (response) { + console.debug("sendNewPolicyModel response received: ", response.status); + if (response.ok) { + console.info("sendNewPolicyModel query successful"); + return response.text(); + } else { + return response.text().then(responseBody => { + throw new Error("HTTP " + response.status + "," + responseBody); }) - .then(function (response) { - console.debug("sendNewPolicyModel response received: ", response.status); - if (response.ok) { - console.info("sendNewPolicyModel query successful"); - return response.text(); - } else { - return response.text().then(responseBody => { - throw new Error("HTTP " + response.status + "," + responseBody); - }) - } - }) - .catch(function (error) { - console.error("sendNewPolicyModel error occurred ", error); - alert ("sendNewPolicyModel error occurred " + error); - return undefined; - }); - } + } + }) + .catch(function (error) { + console.error("sendNewPolicyModel error occurred ", error); + alert("sendNewPolicyModel error occurred " + error); + return undefined; + }); + } } diff --git a/runtime/ui-react/src/api/PolicyToscaService.js b/runtime/ui-react/src/api/PolicyToscaService.js index e2a1f45e4..6c74149d9 100644 --- a/runtime/ui-react/src/api/PolicyToscaService.js +++ b/runtime/ui-react/src/api/PolicyToscaService.js @@ -39,42 +39,42 @@ export default class PolicyToscaService { } static getToscaPolicyModelYaml(policyModelType, policyModelVersion) { - return fetch(window.location.pathname + 'restservices/clds/v2/policyToscaModels/yaml/' + policyModelType + "/" + policyModelVersion, { - method: 'GET', - credentials: 'same-origin' - }) - .then(function (response) { - console.debug("getToscaPolicyModelYaml response received: ", response.status); - if (response.ok) { - return response.json(); - } else { - console.error("getToscaPolicyModelYaml query failed"); - return ""; - } - }) - .catch(function (error) { - console.error("getToscaPolicyModelYaml error received", error); - return ""; - }); + return fetch(window.location.pathname + 'restservices/clds/v2/policyToscaModels/yaml/' + policyModelType + "/" + policyModelVersion, { + method: 'GET', + credentials: 'same-origin' + }) + .then(function (response) { + console.debug("getToscaPolicyModelYaml response received: ", response.status); + if (response.ok) { + return response.json(); + } else { + console.error("getToscaPolicyModelYaml query failed"); + return ""; + } + }) + .catch(function (error) { + console.error("getToscaPolicyModelYaml error received", error); + return ""; + }); } static getToscaPolicyModel(policyModelType, policyModelVersion) { - return fetch(window.location.pathname + 'restservices/clds/v2/policyToscaModels/' + policyModelType + "/" + policyModelVersion, { - method: 'GET', - credentials: 'same-origin' - }) - .then(function (response) { - console.debug("getToscaPolicyModel response received: ", response.status); - if (response.ok) { - return response.json(); - } else { - console.error("getToscaPolicyModel query failed"); - return {}; - } - }) - .catch(function (error) { - console.error("getToscaPolicyModel error received", error); - return {}; - }); + return fetch(window.location.pathname + 'restservices/clds/v2/policyToscaModels/' + policyModelType + "/" + policyModelVersion, { + method: 'GET', + credentials: 'same-origin' + }) + .then(function (response) { + console.debug("getToscaPolicyModel response received: ", response.status); + if (response.ok) { + return response.json(); + } else { + console.error("getToscaPolicyModel query failed"); + return {}; + } + }) + .catch(function (error) { + console.error("getToscaPolicyModel error received", error); + return {}; + }); } } diff --git a/runtime/ui-react/src/api/TemplateService.js b/runtime/ui-react/src/api/TemplateService.js index 08436f2b8..a6e386eac 100644 --- a/runtime/ui-react/src/api/TemplateService.js +++ b/runtime/ui-react/src/api/TemplateService.js @@ -22,176 +22,176 @@ export default class TemplateService { - static getLoopNames() { - return fetch(window.location.pathname + 'restservices/clds/v2/loop/getAllNames', { method: 'GET', credentials: 'same-origin' }) - .then(function (response) { - console.debug("getLoopNames response received: ", response.status); - if (response.ok) { - return response.json(); - } else { - console.error("getLoopNames query failed"); - return {}; - } - }) - .catch(function (error) { - console.error("getLoopNames error received", error); - return {}; - }); + static getLoopNames() { + return fetch(window.location.pathname + 'restservices/clds/v2/loop/getAllNames', { method: 'GET', credentials: 'same-origin' }) + .then(function (response) { + console.debug("getLoopNames response received: ", response.status); + if (response.ok) { + return response.json(); + } else { + console.error("getLoopNames query failed"); + return {}; } + }) + .catch(function (error) { + console.error("getLoopNames error received", error); + return {}; + }); + } - static getAllLoopTemplates() { - return fetch(window.location.pathname + 'restservices/clds/v2/templates', { method: 'GET', credentials: 'same-origin', }) - .then(function (response) { - console.debug("getAllLoopTemplates response received: ", response.status); - if (response.ok) { - return response.json(); - } else { - console.error("getAllLoopTemplates query failed"); - return {}; - } - }) - .catch(function (error) { - console.error("getAllLoopTemplates error received", error); - return {}; - }); - } + static getAllLoopTemplates() { + return fetch(window.location.pathname + 'restservices/clds/v2/templates', { method: 'GET', credentials: 'same-origin', }) + .then(function (response) { + console.debug("getAllLoopTemplates response received: ", response.status); + if (response.ok) { + return response.json(); + } else { + console.error("getAllLoopTemplates query failed"); + return {}; + } + }) + .catch(function (error) { + console.error("getAllLoopTemplates error received", error); + return {}; + }); + } - static getDictionary() { - return fetch(window.location.pathname + 'restservices/clds/v2/dictionary/', { method: 'GET', credentials: 'same-origin', }) - .then(function (response) { - console.debug("getDictionary response received: ", response.status); - if (response.ok) { - return response.json(); - } else { - console.error("getDictionary query failed"); - return {}; - } - }) - .catch(function (error) { - console.error("getDictionary error received", error); - return {}; - }); - } + static getDictionary() { + return fetch(window.location.pathname + 'restservices/clds/v2/dictionary/', { method: 'GET', credentials: 'same-origin', }) + .then(function (response) { + console.debug("getDictionary response received: ", response.status); + if (response.ok) { + return response.json(); + } else { + console.error("getDictionary query failed"); + return {}; + } + }) + .catch(function (error) { + console.error("getDictionary error received", error); + return {}; + }); + } - static getDictionaryElements(dictionaryName) { - return fetch(window.location.pathname + 'restservices/clds/v2/dictionary/' + dictionaryName, { - method: 'GET', - headers: { - "Content-Type": "application/json", - }, - credentials: 'same-origin', - }) - .then(function (response) { - console.debug("getDictionaryElements response received: ", response.status); - if (response.ok) { - return response.json(); - } else { - console.error("getDictionaryElements query failed"); - return {}; - } - }) - .catch(function (error) { - console.error("getDictionaryElements error received", error); - return {}; - }); - } + static getDictionaryElements(dictionaryName) { + return fetch(window.location.pathname + 'restservices/clds/v2/dictionary/' + dictionaryName, { + method: 'GET', + headers: { + "Content-Type": "application/json", + }, + credentials: 'same-origin', + }) + .then(function (response) { + console.debug("getDictionaryElements response received: ", response.status); + if (response.ok) { + return response.json(); + } else { + console.error("getDictionaryElements query failed"); + return {}; + } + }) + .catch(function (error) { + console.error("getDictionaryElements error received", error); + return {}; + }); + } - static insDictionary(jsonData) { - console.log("dictionaryName is", jsonData.name) - return fetch(window.location.pathname + 'restservices/clds/v2/dictionary/', { - method: 'PUT', - credentials: 'same-origin', - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(jsonData) - }) - .then(function (response) { - console.debug("insDictionary response received: ", response.status); - if (response.ok) { - return response.status; - } else { - var errorMessage = response.status; - console.error("insDictionary query failed", response.status); - return errorMessage; - } - }) - .catch(function (error) { - console.error("insDictionary error received", error); - return ""; - }); - } + static insDictionary(jsonData) { + console.log("dictionaryName is", jsonData.name) + return fetch(window.location.pathname + 'restservices/clds/v2/dictionary/', { + method: 'PUT', + credentials: 'same-origin', + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(jsonData) + }) + .then(function (response) { + console.debug("insDictionary response received: ", response.status); + if (response.ok) { + return response.status; + } else { + var errorMessage = response.status; + console.error("insDictionary query failed", response.status); + return errorMessage; + } + }) + .catch(function (error) { + console.error("insDictionary error received", error); + return ""; + }); + } - static insDictionaryElements(jsonData) { - console.log("dictionaryName is", jsonData.name) - return fetch(window.location.pathname + 'restservices/clds/v2/dictionary/' + jsonData.name, { - method: 'PUT', - credentials: 'same-origin', - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(jsonData) - }) - .then(function (response) { - console.debug("insDictionary response received: ", response.status); - if (response.ok) { - return response.status; - } else { - var errorMessage = response.status; - console.error("insDictionary query failed", response.status); - return errorMessage; - } - }) - .catch(function (error) { - console.error("insDictionary error received", error); - return ""; - }); - } + static insDictionaryElements(jsonData) { + console.log("dictionaryName is", jsonData.name) + return fetch(window.location.pathname + 'restservices/clds/v2/dictionary/' + jsonData.name, { + method: 'PUT', + credentials: 'same-origin', + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(jsonData) + }) + .then(function (response) { + console.debug("insDictionary response received: ", response.status); + if (response.ok) { + return response.status; + } else { + var errorMessage = response.status; + console.error("insDictionary query failed", response.status); + return errorMessage; + } + }) + .catch(function (error) { + console.error("insDictionary error received", error); + return ""; + }); + } - static deleteDictionary(dictionaryName) { - console.log("inside templaemenu service", dictionaryName) - return fetch(window.location.pathname + 'restservices/clds/v2/dictionary/' + dictionaryName, { - method: 'DELETE', - headers: { - "Content-Type": "application/json", - }, - credentials: 'same-origin', - }) - .then(function (response) { - console.debug("deleteDictionary response received: ", response.status); - if (response.ok) { - return response.status; - } else { - console.error("deleteDictionary query failed"); - return {}; - } - }) - .catch(function (error) { - console.error("deleteDictionary error received", error); - return {}; - }); - } + static deleteDictionary(dictionaryName) { + console.log("inside templaemenu service", dictionaryName) + return fetch(window.location.pathname + 'restservices/clds/v2/dictionary/' + dictionaryName, { + method: 'DELETE', + headers: { + "Content-Type": "application/json", + }, + credentials: 'same-origin', + }) + .then(function (response) { + console.debug("deleteDictionary response received: ", response.status); + if (response.ok) { + return response.status; + } else { + console.error("deleteDictionary query failed"); + return {}; + } + }) + .catch(function (error) { + console.error("deleteDictionary error received", error); + return {}; + }); + } - static deleteDictionaryElements(dictionaryData) { - return fetch(window.location.pathname + 'restservices/clds/v2/dictionary/' + dictionaryData.name + '/elements/' + dictionaryData.shortName , { - method: 'DELETE', - headers: { - "Content-Type": "application/json", - }, - credentials: 'same-origin', - }) - .then(function (response) { - console.debug("deleteDictionary response received: ", response.status); - if (response.ok) { - return response.status; - } else { - console.error("deleteDictionary query failed"); - return {}; - } - }) - .catch(function (error) { - console.error("deleteDictionary error received", error); - return {}; - }); - } - } + static deleteDictionaryElements(dictionaryData) { + return fetch(window.location.pathname + 'restservices/clds/v2/dictionary/' + dictionaryData.name + '/elements/' + dictionaryData.shortName, { + method: 'DELETE', + headers: { + "Content-Type": "application/json", + }, + credentials: 'same-origin', + }) + .then(function (response) { + console.debug("deleteDictionary response received: ", response.status); + if (response.ok) { + return response.status; + } else { + console.error("deleteDictionary query failed"); + return {}; + } + }) + .catch(function (error) { + console.error("deleteDictionary error received", error); + return {}; + }); + } +} diff --git a/runtime/ui-react/src/api/UserService.js b/runtime/ui-react/src/api/UserService.js index 5fb4aa6b4..bcf46ea64 100644 --- a/runtime/ui-react/src/api/UserService.js +++ b/runtime/ui-react/src/api/UserService.js @@ -22,53 +22,54 @@ */ export default class UserService { - static notLoggedUserName='Anonymous'; - static login() { - return fetch(window.location.pathname + 'restservices/clds/v1/user/getUser', { - method: 'GET', - credentials: 'same-origin' - }) - .then(function (response) { - console.debug("getUser response received, status code:", response.status); - if (response.ok) { - return response.text(); - } else { - console.error("getUser response is nok"); - return UserService.notLoggedUserName; - } - }) - .then(function (data) { - console.info ("User connected:",data) - return data; - }) - .catch(function(error) { - console.warn("getUser error received, user set to: ",UserService.notLoggedUserName); - console.error("getUser error:",error); - return UserService.notLoggedUserName; - }); - } + static notLoggedUserName = 'Anonymous'; - static getUserInfo() { - return fetch(window.location.pathname + 'restservices/clds/v2/clampInformation', { - method: 'GET', - credentials: 'same-origin' - }) - .then(function (response) { - console.debug("getUserInfo response received, status code:", response.status); - if (response.ok) { - return response.json(); - } else { - return {} - } - }) - .then(function (data) { - console.info ("User info received:",data) - return data; - }) - .catch(function(error) { - console.warn("getUserInfo error received, user set to: ",UserService.notLoggedUserName); - console.error("getUserInfo error:",error); - return {}; - }); - } + static login() { + return fetch(window.location.pathname + 'restservices/clds/v1/user/getUser', { + method: 'GET', + credentials: 'same-origin' + }) + .then(function (response) { + console.debug("getUser response received, status code:", response.status); + if (response.ok) { + return response.text(); + } else { + console.error("getUser response is nok"); + return UserService.notLoggedUserName; + } + }) + .then(function (data) { + console.info("User connected:", data) + return data; + }) + .catch(function (error) { + console.warn("getUser error received, user set to: ", UserService.notLoggedUserName); + console.error("getUser error:", error); + return UserService.notLoggedUserName; + }); + } + + static getUserInfo() { + return fetch(window.location.pathname + 'restservices/clds/v2/clampInformation', { + method: 'GET', + credentials: 'same-origin' + }) + .then(function (response) { + console.debug("getUserInfo response received, status code:", response.status); + if (response.ok) { + return response.json(); + } else { + return {} + } + }) + .then(function (data) { + console.info("User info received:", data) + return data; + }) + .catch(function (error) { + console.warn("getUserInfo error received, user set to: ", UserService.notLoggedUserName); + console.error("getUserInfo error:", error); + return {}; + }); + } } diff --git a/runtime/ui-react/src/components/dialogs/Loop/CreateLoopModal.js b/runtime/ui-react/src/components/dialogs/Loop/CreateLoopModal.js index 5663360a0..690dcbbdf 100644 --- a/runtime/ui-react/src/components/dialogs/Loop/CreateLoopModal.js +++ b/runtime/ui-react/src/components/dialogs/Loop/CreateLoopModal.js @@ -34,157 +34,160 @@ import LoopCache from '../../../api/LoopCache'; import SvgGenerator from '../../loop_viewer/svg/SvgGenerator'; const ModalStyled = styled(Modal)` - background-color: transparent; + background-color: transparent; ` const ErrMsgStyled = styled.div` - color: red; + color: red; ` export default class CreateLoopModal extends React.Component { - constructor(props, context) { - super(props, context); + constructor(props, context) { + super(props, context); - this.getAllLoopTemplates = this.getAllLoopTemplates.bind(this); - this.handleCreate = this.handleCreate.bind(this); - this.handleModelName = this.handleModelName.bind(this); - this.handleClose = this.handleClose.bind(this); - this.handleDropDownListChange = this.handleDropDownListChange.bind(this); - this.renderSvg = this.renderSvg.bind(this); - this.state = { - show: true, - chosenTemplateName: '', - modelInputErrMsg: '', - modelName: '', - templateNames: [], - fakeLoopCacheWithTemplate: new LoopCache({}) - }; - } + this.getAllLoopTemplates = this.getAllLoopTemplates.bind(this); + this.handleCreate = this.handleCreate.bind(this); + this.handleModelName = this.handleModelName.bind(this); + this.handleClose = this.handleClose.bind(this); + this.handleDropDownListChange = this.handleDropDownListChange.bind(this); + this.renderSvg = this.renderSvg.bind(this); + this.state = { + show: true, + chosenTemplateName: '', + modelInputErrMsg: '', + modelName: '', + templateNames: [], + fakeLoopCacheWithTemplate: new LoopCache({}) + }; + } - async componentDidMount() { - await this.getAllLoopTemplates(); - await this.getModelNames(); - } + async componentDidMount() { + await this.getAllLoopTemplates(); + await this.getModelNames(); + } - handleClose() { - this.setState({ show: false }); - this.props.history.push('/'); - } + handleClose() { + this.setState({ show: false }); + this.props.history.push('/'); + } - handleDropDownListChange(e) { - if (typeof e.value !== "undefined") { - this.setState({ - fakeLoopCacheWithTemplate: - new LoopCache({ - "loopTemplate":e.templateObject, - "name": "fakeLoop" - }), - chosenTemplateName: e.value - }) - } else { - this.setState({ fakeLoopCacheWithTemplate: new LoopCache({}) }) - } - } + handleDropDownListChange(e) { + if (typeof e.value !== "undefined") { + this.setState({ + fakeLoopCacheWithTemplate: + new LoopCache({ + "loopTemplate": e.templateObject, + "name": "fakeLoop" + }), + chosenTemplateName: e.value + }) + } else { + this.setState({ fakeLoopCacheWithTemplate: new LoopCache({}) }) + } + } - getAllLoopTemplates() { - TemplateService.getAllLoopTemplates().then(templatesData => { - const templateOptions = templatesData.map((templateData) => { return { label: templateData.name, value: templateData.name, templateObject: templateData } }); - this.setState({ - templateNames: templateOptions }) - }); - } + getAllLoopTemplates() { + TemplateService.getAllLoopTemplates().then(templatesData => { + const templateOptions = templatesData.map((templateData) => { + return { label: templateData.name, value: templateData.name, templateObject: templateData } + }); + this.setState({ + templateNames: templateOptions + }) + }); + } - getModelNames() { - TemplateService.getLoopNames().then(loopNames => { - if (!loopNames) { - loopNames = []; - } - // Remove LOOP_ prefix - let trimmedLoopNames = loopNames.map(str => str.replace('LOOP_', '')); - this.setState({ modelNames: trimmedLoopNames }); - }); - } + getModelNames() { + TemplateService.getLoopNames().then(loopNames => { + if (!loopNames) { + loopNames = []; + } + // Remove LOOP_ prefix + let trimmedLoopNames = loopNames.map(str => str.replace('LOOP_', '')); + this.setState({ modelNames: trimmedLoopNames }); + }); + } - handleCreate() { - if (!this.state.modelName) { - alert("A model name is required"); - return; - } - console.debug("Create Model " + this.state.modelName + ", Template " + this.state.chosenTemplateName + " is chosen"); - this.setState({ show: false }); - LoopService.createLoop("LOOP_" + this.state.modelName, this.state.chosenTemplateName).then(text => { - console.debug("CreateLoop response received: ", text); - try { - this.props.history.push('/'); - this.props.loadLoopFunction("LOOP_" + this.state.modelName); - } catch(err) { - alert(text); - this.props.history.push('/'); - } - }) - .catch(error => { - console.debug("Create Loop failed"); - }); - } + handleCreate() { + if (!this.state.modelName) { + alert("A model name is required"); + return; + } + console.debug("Create Model " + this.state.modelName + ", Template " + this.state.chosenTemplateName + " is chosen"); + this.setState({ show: false }); + LoopService.createLoop("LOOP_" + this.state.modelName, this.state.chosenTemplateName).then(text => { + console.debug("CreateLoop response received: ", text); + try { + this.props.history.push('/'); + this.props.loadLoopFunction("LOOP_" + this.state.modelName); + } catch (err) { + alert(text); + this.props.history.push('/'); + } + }) + .catch(error => { + console.debug("Create Loop failed"); + }); + } - handleModelName(event) { - if (this.state.modelNames.includes(event.target.value)) { - this.setState({ - modelInputErrMsg: 'A model named "' + event.target.value + '" already exists. Please pick another name.', - modelName: event.target.value - }); - return; - } else { - this.setState({ - modelInputErrMsg: '', - modelName: event.target.value - }); - } - } + handleModelName(event) { + if (this.state.modelNames.includes(event.target.value)) { + this.setState({ + modelInputErrMsg: 'A model named "' + event.target.value + '" already exists. Please pick another name.', + modelName: event.target.value + }); + return; + } else { + this.setState({ + modelInputErrMsg: '', + modelName: event.target.value + }); + } + } - renderSvg() { - return ( - <SvgGenerator loopCache={this.state.fakeLoopCacheWithTemplate} clickable={false} generatedFrom={SvgGenerator.GENERATED_FROM_TEMPLATE}/> - ); - } + renderSvg() { + return ( + <SvgGenerator loopCache={ this.state.fakeLoopCacheWithTemplate } clickable={ false } generatedFrom={ SvgGenerator.GENERATED_FROM_TEMPLATE }/> + ); + } - render() { - return ( - <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} > - <Modal.Header closeButton> - <Modal.Title>Create Model</Modal.Title> - </Modal.Header> - <Modal.Body> - <Form.Group as={Row} controlId="formPlaintextEmail"> - <Form.Label column sm="2">Template Name:</Form.Label> - <Col sm="10"> - <Select onChange={this.handleDropDownListChange} options={this.state.templateNames} /> - </Col> - </Form.Group> - <Form.Group as={Row} style={{alignItems: 'center'}} controlId="formSvgPreview"> - <Form.Label column sm="2">Model Preview:</Form.Label> - <Col sm="10"> - {this.renderSvg()} - </Col> - </Form.Group> - <Form.Group as={Row} controlId="formPlaintextEmail"> - <Form.Label column sm="2">Model Name:</Form.Label> - <input sm="5" type="text" style={{width: '50%', marginLeft: '1em' }} - value={this.state.modelName} - onChange={this.handleModelName} - /> - <span sm="5"/> - </Form.Group> - <Form.Group as={Row} controlId="formPlaintextEmail"> - <Form.Label column sm="2"> </Form.Label> - <ErrMsgStyled>{this.state.modelInputErrMsg}</ErrMsgStyled> - </Form.Group> - </Modal.Body> - <Modal.Footer> - <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button> - <Button variant="primary" type="submit" onClick={this.handleCreate}>Create</Button> - </Modal.Footer> - </ModalStyled> - ); - } + render() { + return ( + <ModalStyled size="xl" show={ this.state.show } onHide={ this.handleClose } backdrop="static" keyboard={ false }> + <Modal.Header closeButton> + <Modal.Title>Create Model</Modal.Title> + </Modal.Header> + <Modal.Body> + <Form.Group as={ Row } controlId="formPlaintextEmail"> + <Form.Label column sm="2">Template Name:</Form.Label> + <Col sm="10"> + <Select onChange={ this.handleDropDownListChange } options={ this.state.templateNames }/> + </Col> + </Form.Group> + <Form.Group as={ Row } style={ { alignItems: 'center' } } controlId="formSvgPreview"> + <Form.Label column sm="2">Model Preview:</Form.Label> + <Col sm="10"> + { this.renderSvg() } + </Col> + </Form.Group> + <Form.Group as={ Row } controlId="formPlaintextEmail"> + <Form.Label column sm="2">Model Name:</Form.Label> + <input sm="5" type="text" style={ { width: '50%', marginLeft: '1em' } } + value={ this.state.modelName } + onChange={ this.handleModelName } + /> + <span sm="5"/> + </Form.Group> + <Form.Group as={ Row } controlId="formPlaintextEmail"> + <Form.Label column sm="2"> </Form.Label> + <ErrMsgStyled>{ this.state.modelInputErrMsg }</ErrMsgStyled> + </Form.Group> + </Modal.Body> + <Modal.Footer> + <Button variant="secondary" type="null" onClick={ this.handleClose }>Cancel</Button> + <Button variant="primary" type="submit" onClick={ this.handleCreate }>Create</Button> + </Modal.Footer> + </ModalStyled> + ); + } } diff --git a/runtime/ui-react/src/components/dialogs/Loop/CreateLoopModal.test.js b/runtime/ui-react/src/components/dialogs/Loop/CreateLoopModal.test.js index 8ef53b412..3d9483334 100644 --- a/runtime/ui-react/src/components/dialogs/Loop/CreateLoopModal.test.js +++ b/runtime/ui-react/src/components/dialogs/Loop/CreateLoopModal.test.js @@ -27,7 +27,10 @@ import LoopService from '../../../api/LoopService'; import TemplateService from '../../../api/TemplateService'; let errorMessage = ''; -window.alert = jest.fn().mockImplementation((mesg) => { errorMessage = mesg ; return }); +window.alert = jest.fn().mockImplementation((mesg) => { + errorMessage = mesg; + return +}); describe('Verify CreateLoopModal', () => { @@ -35,7 +38,7 @@ describe('Verify CreateLoopModal', () => { it('Test the render method', async () => { const flushPromises = () => new Promise(setImmediate); TemplateService.getAllLoopTemplates = jest.fn().mockImplementation(() => { - return Promise.resolve([{"name":"template1"},{"name":"template2"}]); + return Promise.resolve([{ "name": "template1" }, { "name": "template2" }]); }); TemplateService.getLoopNames = jest.fn().mockImplementation(() => { return Promise.resolve([]); @@ -45,21 +48,25 @@ describe('Verify CreateLoopModal', () => { expect(component).toMatchSnapshot(); await flushPromises(); component.update(); - expect(component.state('templateNames')).toStrictEqual([{"label": "template1", "value": "template1", "templateObject": {"name": "template1"}}, {"label": "template2", "value": "template2","templateObject": {"name": "template2"}}]); + expect(component.state('templateNames')).toStrictEqual([{ "label": "template1", "value": "template1", "templateObject": { "name": "template1" } }, { + "label": "template2", + "value": "template2", + "templateObject": { "name": "template2" } + }]); }); it('handleDropdownListChange event', async () => { - const flushPromises = () => new Promise(setImmediate); + const flushPromises = () => new Promise(setImmediate); const component = shallow(<CreateLoopModal/>); - component.find('StateManager').simulate('change', {value: 'template1', templateObject: {"name":"template1"} }); + component.find('StateManager').simulate('change', { value: 'template1', templateObject: { "name": "template1" } }); await flushPromises(); component.update(); expect(component.state('chosenTemplateName')).toEqual("template1"); expect(component.state('fakeLoopCacheWithTemplate').getLoopTemplate()['name']).toEqual("template1"); expect(component.state('fakeLoopCacheWithTemplate').getLoopName()).toEqual("fakeLoop"); - component.find('StateManager').simulate('change',{value: 'template2', templateObject: {"name":"template2"} }); + component.find('StateManager').simulate('change', { value: 'template2', templateObject: { "name": "template2" } }); await flushPromises(); component.update(); expect(component.state('chosenTemplateName')).toEqual("template2"); @@ -70,12 +77,12 @@ describe('Verify CreateLoopModal', () => { it('handleModelName event', async () => { const flushPromises = () => new Promise(setImmediate); TemplateService.getAllLoopTemplates = jest.fn().mockImplementation(() => { - return Promise.resolve([{"name":"template1"},{"name":"template2"}]); + return Promise.resolve([{ "name": "template1" }, { "name": "template2" }]); }); TemplateService.getLoopNames = jest.fn().mockImplementation(() => { return Promise.resolve([]); }); - const event = {target: {value : "model1"} }; + const event = { target: { value: "model1" } }; const component = shallow(<CreateLoopModal/>); await flushPromises(); component.find('input').simulate('change', event); @@ -84,21 +91,21 @@ describe('Verify CreateLoopModal', () => { }); it('Test handleClose', () => { - const historyMock = { push: jest.fn() }; - const handleClose = jest.spyOn(CreateLoopModal.prototype,'handleClose'); - const component = shallow(<CreateLoopModal history={historyMock} />) + const historyMock = { push: jest.fn() }; + const handleClose = jest.spyOn(CreateLoopModal.prototype, 'handleClose'); + const component = shallow(<CreateLoopModal history={ historyMock }/>) component.find('[variant="secondary"]').prop('onClick')(); expect(handleClose).toHaveBeenCalledTimes(1); expect(component.state('show')).toEqual(false); - expect(historyMock.push.mock.calls[0]).toEqual([ '/']); - + expect(historyMock.push.mock.calls[0]).toEqual(['/']); + handleClose.mockClear(); }); it('Test handleCreate Fail', () => { - const handleCreate = jest.spyOn(CreateLoopModal.prototype,'handleCreate'); + const handleCreate = jest.spyOn(CreateLoopModal.prototype, 'handleCreate'); const component = shallow(<CreateLoopModal/>) component.find('[variant="primary"]').prop('onClick')(); @@ -110,32 +117,33 @@ describe('Verify CreateLoopModal', () => { }); it('Test handleCreate Suc', async () => { - const flushPromises = () => new Promise(setImmediate); + const flushPromises = () => new Promise(setImmediate); const historyMock = { push: jest.fn() }; - const loadLoopFunction = jest.fn(); - + const loadLoopFunction = jest.fn(); + LoopService.createLoop = jest.fn().mockImplementation(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => {} - }); - }); - - const handleCreate = jest.spyOn(CreateLoopModal.prototype,'handleCreate'); - const component = shallow(<CreateLoopModal history={historyMock} loadLoopFunction={loadLoopFunction}/>) + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + } + }); + }); + + const handleCreate = jest.spyOn(CreateLoopModal.prototype, 'handleCreate'); + const component = shallow(<CreateLoopModal history={ historyMock } loadLoopFunction={ loadLoopFunction }/>) component.setState({ - modelName: "modelNameTest", - chosenTemplateName: "template1" - }); + modelName: "modelNameTest", + chosenTemplateName: "template1" + }); component.find('[variant="primary"]').prop('onClick')(); - await flushPromises(); - component.update(); + await flushPromises(); + component.update(); expect(handleCreate).toHaveBeenCalledTimes(1); expect(component.state('show')).toEqual(false); - expect(historyMock.push.mock.calls[0]).toEqual([ '/']); + expect(historyMock.push.mock.calls[0]).toEqual(['/']); handleCreate.mockClear(); }); diff --git a/runtime/ui-react/src/components/dialogs/Loop/DeployLoopModal.js b/runtime/ui-react/src/components/dialogs/Loop/DeployLoopModal.js index 803cfa9cb..5fff58631 100644 --- a/runtime/ui-react/src/components/dialogs/Loop/DeployLoopModal.js +++ b/runtime/ui-react/src/components/dialogs/Loop/DeployLoopModal.js @@ -32,142 +32,147 @@ import styled from 'styled-components'; import Spinner from 'react-bootstrap/Spinner' const StyledSpinnerDiv = styled.div` - justify-content: center !important; - display: flex !important; + justify-content: center !important; + display: flex !important; `; const ModalStyled = styled(Modal)` - background-color: transparent; + background-color: transparent; ` const FormStyled = styled(Form.Group)` - padding: .25rem 1.5rem; + padding: .25rem 1.5rem; ` export default class DeployLoopModal extends React.Component { - - constructor(props, context) { - super(props, context); - - this.handleSave = this.handleSave.bind(this); - this.handleClose = this.handleClose.bind(this); - this.handleChange = this.handleChange.bind(this); - this.refreshStatus = this.refreshStatus.bind(this); - this.renderDeployParam = this.renderDeployParam.bind(this); - this.renderSpinner = this.renderSpinner.bind(this); - - const propertiesJson = JSON.parse(JSON.stringify(this.props.loopCache.getGlobalProperties())); - this.state = { - loopCache: this.props.loopCache, - temporaryPropertiesJson: propertiesJson, - show: true, - key: this.getInitialKeyValue(propertiesJson) - }; - } - getInitialKeyValue(temporaryPropertiesJson) { - const deployJsonList = temporaryPropertiesJson["dcaeDeployParameters"]; - return Object.keys(deployJsonList).find((obj) => Object.keys(deployJsonList).indexOf(obj) === 0); - } - componentWillReceiveProps(newProps) { - this.setState({ - loopName: newProps.loopCache.getLoopName(), - show: true - }); - } - - handleClose(){ - this.setState({ show: false }); - this.props.history.push('/'); - } - - renderSpinner() { - if (this.state.deploying) { - return ( - <StyledSpinnerDiv> - <Spinner animation="border" role="status"> - <span className="sr-only">Loading...</span> - </Spinner> - </StyledSpinnerDiv> - ); - } else { - return (<div></div>); - } - } - - handleSave() { - const loopName = this.props.loopCache.getLoopName(); - // save the global propserties - this.setState({ deploying: true }); - LoopService.updateGlobalProperties(loopName, this.state.temporaryPropertiesJson).then(resp => { - LoopActionService.performAction(loopName, "deploy").then(pars => { - this.props.showSucAlert("Action deploy successfully performed"); - // refresh status and update loop logs - this.refreshStatus(loopName); - }) - .catch(error => { - this.props.showFailAlert("Action deploy failed"); - // refresh status and update loop logs - this.refreshStatus(loopName); - }); - }); - } - - refreshStatus(loopName) { - LoopActionService.refreshStatus(loopName).then(data => { - this.props.updateLoopFunction(data); - this.setState({ show: false, deploying: false }); - this.props.history.push('/'); - }) - .catch(error => { - this.props.showFailAlert("Refresh status failed"); - this.setState({ show: false, deploying: false }); - this.props.history.push('/'); - }); - } - handleChange(event) { - let deploymentParam = this.state.temporaryPropertiesJson["dcaeDeployParameters"]; - deploymentParam[this.state.key][event.target.name] = event.target.value; - - this.setState({temporaryPropertiesJson:{dcaeDeployParameters: deploymentParam}}); - } - renderDeployParamTabs() { - if (typeof (this.state.temporaryPropertiesJson) === "undefined") { - return ""; - } - - const deployJsonList = this.state.temporaryPropertiesJson["dcaeDeployParameters"]; - var indents = []; - Object.keys(deployJsonList).forEach(item => - indents.push(<Tab key={item} eventKey={item} title={item}> - {this.renderDeployParam(deployJsonList[item])} - </Tab>) - ); - return indents; - } - renderDeployParam(deployJson) { - var indents = []; - Object.keys(deployJson).forEach(item => - indents.push(<FormStyled key={item}> - <Form.Label>{item}</Form.Label> - <Form.Control type="text" name={item} onChange={this.handleChange} defaultValue={deployJson[item]}></Form.Control> - </FormStyled>)); - return indents; - } - render() { - return ( - <ModalStyled size="lg" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} > - <Modal.Header closeButton> - <Modal.Title>Deployment parameters</Modal.Title> - </Modal.Header> - <Tabs id="controlled-tab-example" activeKey={this.state.key} onSelect={key => this.setState({ key })}> - {this.renderDeployParamTabs()} - </Tabs> - {this.renderSpinner()} - <Modal.Footer> - <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button> - <Button variant="primary" type="submit" onClick={this.handleSave}>Deploy</Button> - </Modal.Footer> - </ModalStyled> - ); - } + constructor(props, context) { + super(props, context); + + this.handleSave = this.handleSave.bind(this); + this.handleClose = this.handleClose.bind(this); + this.handleChange = this.handleChange.bind(this); + this.refreshStatus = this.refreshStatus.bind(this); + this.renderDeployParam = this.renderDeployParam.bind(this); + this.renderSpinner = this.renderSpinner.bind(this); + + const propertiesJson = JSON.parse(JSON.stringify(this.props.loopCache.getGlobalProperties())); + this.state = { + loopCache: this.props.loopCache, + temporaryPropertiesJson: propertiesJson, + show: true, + key: this.getInitialKeyValue(propertiesJson) + }; + } + + getInitialKeyValue(temporaryPropertiesJson) { + const deployJsonList = temporaryPropertiesJson["dcaeDeployParameters"]; + return Object.keys(deployJsonList).find((obj) => Object.keys(deployJsonList).indexOf(obj) === 0); + } + + componentWillReceiveProps(newProps) { + this.setState({ + loopName: newProps.loopCache.getLoopName(), + show: true + }); + } + + handleClose() { + this.setState({ show: false }); + this.props.history.push('/'); + } + + renderSpinner() { + if (this.state.deploying) { + return ( + <StyledSpinnerDiv> + <Spinner animation="border" role="status"> + <span className="sr-only">Loading...</span> + </Spinner> + </StyledSpinnerDiv> + ); + } else { + return (<div></div>); + } + } + + handleSave() { + const loopName = this.props.loopCache.getLoopName(); + // save the global propserties + this.setState({ deploying: true }); + LoopService.updateGlobalProperties(loopName, this.state.temporaryPropertiesJson).then(resp => { + LoopActionService.performAction(loopName, "deploy").then(pars => { + this.props.showSucAlert("Action deploy successfully performed"); + // refresh status and update loop logs + this.refreshStatus(loopName); + }) + .catch(error => { + this.props.showFailAlert("Action deploy failed"); + // refresh status and update loop logs + this.refreshStatus(loopName); + }); + }); + } + + refreshStatus(loopName) { + LoopActionService.refreshStatus(loopName).then(data => { + this.props.updateLoopFunction(data); + this.setState({ show: false, deploying: false }); + this.props.history.push('/'); + }) + .catch(error => { + this.props.showFailAlert("Refresh status failed"); + this.setState({ show: false, deploying: false }); + this.props.history.push('/'); + }); + } + + handleChange(event) { + let deploymentParam = this.state.temporaryPropertiesJson["dcaeDeployParameters"]; + deploymentParam[this.state.key][event.target.name] = event.target.value; + + this.setState({ temporaryPropertiesJson: { dcaeDeployParameters: deploymentParam } }); + } + + renderDeployParamTabs() { + if (typeof (this.state.temporaryPropertiesJson) === "undefined") { + return ""; + } + + const deployJsonList = this.state.temporaryPropertiesJson["dcaeDeployParameters"]; + var indents = []; + Object.keys(deployJsonList).forEach(item => + indents.push(<Tab key={ item } eventKey={ item } title={ item }> + { this.renderDeployParam(deployJsonList[item]) } + </Tab>) + ); + return indents; + } + + renderDeployParam(deployJson) { + var indents = []; + Object.keys(deployJson).forEach(item => + indents.push(<FormStyled key={ item }> + <Form.Label>{ item }</Form.Label> + <Form.Control type="text" name={ item } onChange={ this.handleChange } defaultValue={ deployJson[item] }></Form.Control> + </FormStyled>)); + return indents; + } + + render() { + return ( + <ModalStyled size="lg" show={ this.state.show } onHide={ this.handleClose } backdrop="static" keyboard={ false }> + <Modal.Header closeButton> + <Modal.Title>Deployment parameters</Modal.Title> + </Modal.Header> + <Tabs id="controlled-tab-example" activeKey={ this.state.key } onSelect={ key => this.setState({ key }) }> + { this.renderDeployParamTabs() } + </Tabs> + { this.renderSpinner() } + <Modal.Footer> + <Button variant="secondary" type="null" onClick={ this.handleClose }>Cancel</Button> + <Button variant="primary" type="submit" onClick={ this.handleSave }>Deploy</Button> + </Modal.Footer> + </ModalStyled> + ); + } } diff --git a/runtime/ui-react/src/components/dialogs/Loop/DeployLoopModal.test.js b/runtime/ui-react/src/components/dialogs/Loop/DeployLoopModal.test.js index 84dbfd1f6..0c68f237c 100644 --- a/runtime/ui-react/src/components/dialogs/Loop/DeployLoopModal.test.js +++ b/runtime/ui-react/src/components/dialogs/Loop/DeployLoopModal.test.js @@ -28,85 +28,87 @@ import LoopActionService from '../../../api/LoopActionService'; import LoopService from '../../../api/LoopService'; describe('Verify DeployLoopModal', () => { - const loopCache = new LoopCache({ - "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", - "globalPropertiesJson": { - "dcaeDeployParameters": { - "testMs": { - "location_id": "", - "policy_id": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca" - } - } - } - }); + const loopCache = new LoopCache({ + "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", + "globalPropertiesJson": { + "dcaeDeployParameters": { + "testMs": { + "location_id": "", + "policy_id": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca" + } + } + } + }); - it('Test the render method', () => { - const component = shallow( - <DeployLoopModal loopCache={loopCache}/> - ) + it('Test the render method', () => { + const component = shallow( + <DeployLoopModal loopCache={ loopCache }/> + ) - expect(component).toMatchSnapshot(); - }); - - it('Test handleClose', () => { - const historyMock = { push: jest.fn() }; - const handleClose = jest.spyOn(DeployLoopModal.prototype,'handleClose'); - const component = shallow(<DeployLoopModal history={historyMock} loopCache={loopCache}/>) + expect(component).toMatchSnapshot(); + }); - component.find('[variant="secondary"]').prop('onClick')(); + it('Test handleClose', () => { + const historyMock = { push: jest.fn() }; + const handleClose = jest.spyOn(DeployLoopModal.prototype, 'handleClose'); + const component = shallow(<DeployLoopModal history={ historyMock } loopCache={ loopCache }/>) - expect(handleClose).toHaveBeenCalledTimes(1); - expect(historyMock.push.mock.calls[0]).toEqual([ '/']); - }); + component.find('[variant="secondary"]').prop('onClick')(); - it('Test handleSave successful', async () => { - const flushPromises = () => new Promise(setImmediate); - const historyMock = { push: jest.fn() }; - const updateLoopFunction = jest.fn(); - const showSucAlert = jest.fn(); - const showFailAlert = jest.fn(); - const handleSave = jest.spyOn(DeployLoopModal.prototype,'handleSave'); - LoopService.updateGlobalProperties = jest.fn().mockImplementation(() => { - return Promise.resolve({ - ok: true, - status: 200, - text: () => "OK" - }); - }); - LoopActionService.performAction = jest.fn().mockImplementation(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => {} - }); - }); - LoopActionService.refreshStatus = jest.fn().mockImplementation(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => {} - }); - }); + expect(handleClose).toHaveBeenCalledTimes(1); + expect(historyMock.push.mock.calls[0]).toEqual(['/']); + }); - const component = shallow(<DeployLoopModal history={historyMock} - loopCache={loopCache} updateLoopFunction={updateLoopFunction} showSucAlert={showSucAlert} showFailAlert={showFailAlert} />) + it('Test handleSave successful', async () => { + const flushPromises = () => new Promise(setImmediate); + const historyMock = { push: jest.fn() }; + const updateLoopFunction = jest.fn(); + const showSucAlert = jest.fn(); + const showFailAlert = jest.fn(); + const handleSave = jest.spyOn(DeployLoopModal.prototype, 'handleSave'); + LoopService.updateGlobalProperties = jest.fn().mockImplementation(() => { + return Promise.resolve({ + ok: true, + status: 200, + text: () => "OK" + }); + }); + LoopActionService.performAction = jest.fn().mockImplementation(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + } + }); + }); + LoopActionService.refreshStatus = jest.fn().mockImplementation(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + } + }); + }); - component.find('[variant="primary"]').prop('onClick')(); - await flushPromises(); - component.update(); + const component = shallow(<DeployLoopModal history={ historyMock } + loopCache={ loopCache } updateLoopFunction={ updateLoopFunction } showSucAlert={ showSucAlert } showFailAlert={ showFailAlert }/>) - expect(handleSave).toHaveBeenCalledTimes(1); - expect(component.state('show')).toEqual(false); - expect(historyMock.push.mock.calls[0]).toEqual([ '/']); - handleSave.mockClear(); - }); + component.find('[variant="primary"]').prop('onClick')(); + await flushPromises(); + component.update(); - it('Onchange event', () => { - const event = { target: { name: "location_id", value: "testLocation"} }; - const component = shallow(<DeployLoopModal loopCache={loopCache}/>); + expect(handleSave).toHaveBeenCalledTimes(1); + expect(component.state('show')).toEqual(false); + expect(historyMock.push.mock.calls[0]).toEqual(['/']); + handleSave.mockClear(); + }); - component.find('[name="location_id"]').simulate('change', event); - component.update(); - expect(component.state('temporaryPropertiesJson').dcaeDeployParameters.testMs.location_id).toEqual("testLocation"); - }); -});
\ No newline at end of file + it('Onchange event', () => { + const event = { target: { name: "location_id", value: "testLocation" } }; + const component = shallow(<DeployLoopModal loopCache={ loopCache }/>); + + component.find('[name="location_id"]').simulate('change', event); + component.update(); + expect(component.state('temporaryPropertiesJson').dcaeDeployParameters.testMs.location_id).toEqual("testLocation"); + }); +}); diff --git a/runtime/ui-react/src/components/dialogs/Loop/LoopPropertiesModal.js b/runtime/ui-react/src/components/dialogs/Loop/LoopPropertiesModal.js index acd0acade..69177134b 100644 --- a/runtime/ui-react/src/components/dialogs/Loop/LoopPropertiesModal.js +++ b/runtime/ui-react/src/components/dialogs/Loop/LoopPropertiesModal.js @@ -28,91 +28,91 @@ import styled from 'styled-components'; import LoopService from '../../../api/LoopService'; const ModalStyled = styled(Modal)` - background-color: transparent; + background-color: transparent; ` export default class LoopPropertiesModal extends React.Component { - state = { - show: true, - loopCache: this.props.loopCache, - temporaryPropertiesJson: JSON.parse(JSON.stringify(this.props.loopCache.getGlobalProperties())) - }; + state = { + show: true, + loopCache: this.props.loopCache, + temporaryPropertiesJson: JSON.parse(JSON.stringify(this.props.loopCache.getGlobalProperties())) + }; - constructor(props, context) { - super(props, context); + constructor(props, context) { + super(props, context); - this.handleClose = this.handleClose.bind(this); - this.handleSave = this.handleSave.bind(this); - this.handleChange = this.handleChange.bind(this); + this.handleClose = this.handleClose.bind(this); + this.handleSave = this.handleSave.bind(this); + this.handleChange = this.handleChange.bind(this); - this.renderDcaeParameters = this.renderDcaeParameters.bind(this); - this.renderAllParameters = this.renderAllParameters.bind(this); - this.getDcaeParameters = this.getDcaeParameters.bind(this); - this.readOnly = props.readOnly !== undefined ? props.readOnly : false; - } + this.renderDcaeParameters = this.renderDcaeParameters.bind(this); + this.renderAllParameters = this.renderAllParameters.bind(this); + this.getDcaeParameters = this.getDcaeParameters.bind(this); + this.readOnly = props.readOnly !== undefined ? props.readOnly : false; + } - componentWillReceiveProps(newProps) { - this.setState({ - loopCache: newProps.loopCache, - temporaryPropertiesJson: JSON.parse(JSON.stringify(newProps.loopCache.getGlobalProperties())) - }); - } + componentWillReceiveProps(newProps) { + this.setState({ + loopCache: newProps.loopCache, + temporaryPropertiesJson: JSON.parse(JSON.stringify(newProps.loopCache.getGlobalProperties())) + }); + } - handleClose() { - this.props.history.push('/'); - } + handleClose() { + this.props.history.push('/'); + } - handleSave(event) { - LoopService.updateGlobalProperties(this.state.loopCache.getLoopName(), this.state.temporaryPropertiesJson).then(resp => { - this.setState({ show: false }); - this.props.history.push('/'); - this.props.loadLoopFunction(this.state.loopCache.getLoopName()); - }); - } + handleSave(event) { + LoopService.updateGlobalProperties(this.state.loopCache.getLoopName(), this.state.temporaryPropertiesJson).then(resp => { + this.setState({ show: false }); + this.props.history.push('/'); + this.props.loadLoopFunction(this.state.loopCache.getLoopName()); + }); + } - handleChange(event) { - this.setState({temporaryPropertiesJson:{[event.target.name]: JSON.parse(event.target.value)}}); - } + handleChange(event) { + this.setState({ temporaryPropertiesJson: { [event.target.name]: JSON.parse(event.target.value) } }); + } - renderAllParameters() { - return (<Modal.Body> - <Form> - {this.renderDcaeParameters()} - </Form> - </Modal.Body> - ); - } + renderAllParameters() { + return (<Modal.Body> + <Form> + { this.renderDcaeParameters() } + </Form> + </Modal.Body> + ); + } - getDcaeParameters() { - if (typeof (this.state.temporaryPropertiesJson) !== "undefined") { - return JSON.stringify(this.state.temporaryPropertiesJson["dcaeDeployParameters"]); - } else { - return ""; - } + getDcaeParameters() { + if (typeof (this.state.temporaryPropertiesJson) !== "undefined") { + return JSON.stringify(this.state.temporaryPropertiesJson["dcaeDeployParameters"]); + } else { + return ""; + } - } + } - renderDcaeParameters() { - return ( - <Form.Group > - <Form.Label>Deploy Parameters</Form.Label> - <Form.Control as="textarea" rows="3" name="dcaeDeployParameters" onChange={this.handleChange} defaultValue={this.getDcaeParameters()}></Form.Control> - </Form.Group> - ); - } + renderDcaeParameters() { + return ( + <Form.Group> + <Form.Label>Deploy Parameters</Form.Label> + <Form.Control as="textarea" rows="3" name="dcaeDeployParameters" onChange={ this.handleChange } defaultValue={ this.getDcaeParameters() }></Form.Control> + </Form.Group> + ); + } - render() { - return ( - <ModalStyled size="lg" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} > - <Modal.Header closeButton> - <Modal.Title>Model Properties</Modal.Title> - </Modal.Header> - {this.renderAllParameters()} - <Modal.Footer> - <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button> - <Button variant="primary" type="submit" disabled={this.readOnly} onClick={this.handleSave}>Save Changes</Button> - </Modal.Footer> - </ModalStyled> - ); - } + render() { + return ( + <ModalStyled size="lg" show={ this.state.show } onHide={ this.handleClose } backdrop="static" keyboard={ false }> + <Modal.Header closeButton> + <Modal.Title>Model Properties</Modal.Title> + </Modal.Header> + { this.renderAllParameters() } + <Modal.Footer> + <Button variant="secondary" type="null" onClick={ this.handleClose }>Cancel</Button> + <Button variant="primary" type="submit" disabled={ this.readOnly } onClick={ this.handleSave }>Save Changes</Button> + </Modal.Footer> + </ModalStyled> + ); + } } diff --git a/runtime/ui-react/src/components/dialogs/Loop/LoopPropertiesModal.test.js b/runtime/ui-react/src/components/dialogs/Loop/LoopPropertiesModal.test.js index 5bbefe228..a9c5903a4 100644 --- a/runtime/ui-react/src/components/dialogs/Loop/LoopPropertiesModal.test.js +++ b/runtime/ui-react/src/components/dialogs/Loop/LoopPropertiesModal.test.js @@ -27,82 +27,84 @@ import LoopCache from '../../../api/LoopCache'; import LoopService from '../../../api/LoopService'; describe('Verify LoopPropertiesModal', () => { - const loopCache = new LoopCache({ - "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", - "globalPropertiesJson": { - "dcaeDeployParameters": { - "location_id": "", - "policy_id": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca" - } - } - }); + const loopCache = new LoopCache({ + "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", + "globalPropertiesJson": { + "dcaeDeployParameters": { + "location_id": "", + "policy_id": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca" + } + } + }); - it('Test the render method', () => { - const component = shallow( - <LoopPropertiesModal loopCache={loopCache}/> - ) - component.setState({ show: true, - temporaryPropertiesJson: { - "dcaeDeployParameters": { - "location_id": "", - "policy_id": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca" - } - } - }); + it('Test the render method', () => { + const component = shallow( + <LoopPropertiesModal loopCache={ loopCache }/> + ) + component.setState({ + show: true, + temporaryPropertiesJson: { + "dcaeDeployParameters": { + "location_id": "", + "policy_id": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca" + } + } + }); - expect(component.state('temporaryPropertiesJson')).toEqual({ - "dcaeDeployParameters": { - "location_id": "", - "policy_id": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca"} - }); - expect(component.state('show')).toEqual(true); + expect(component.state('temporaryPropertiesJson')).toEqual({ + "dcaeDeployParameters": { + "location_id": "", + "policy_id": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca" + } + }); + expect(component.state('show')).toEqual(true); - expect(component).toMatchSnapshot(); - }); + expect(component).toMatchSnapshot(); + }); - it('Test handleClose', () => { - const historyMock = { push: jest.fn() }; - const handleClose = jest.spyOn(LoopPropertiesModal.prototype,'handleClose'); - const component = shallow(<LoopPropertiesModal history={historyMock} loopCache={loopCache}/>) + it('Test handleClose', () => { + const historyMock = { push: jest.fn() }; + const handleClose = jest.spyOn(LoopPropertiesModal.prototype, 'handleClose'); + const component = shallow(<LoopPropertiesModal history={ historyMock } loopCache={ loopCache }/>) - component.find('[variant="secondary"]').prop('onClick')(); + component.find('[variant="secondary"]').prop('onClick')(); - expect(handleClose).toHaveBeenCalledTimes(1); - expect(historyMock.push.mock.calls[0]).toEqual([ '/']); - }); + expect(handleClose).toHaveBeenCalledTimes(1); + expect(historyMock.push.mock.calls[0]).toEqual(['/']); + }); - it('Test handleSave successful', async () => { - const flushPromises = () => new Promise(setImmediate); - const historyMock = { push: jest.fn() }; - const loadLoopFunction = jest.fn(); - const handleSave = jest.spyOn(LoopPropertiesModal.prototype,'handleSave'); - LoopService.updateGlobalProperties = jest.fn().mockImplementation(() => { - return Promise.resolve({ - ok: true, - status: 200, - text: () => "OK" - }); - }); + it('Test handleSave successful', async () => { + const flushPromises = () => new Promise(setImmediate); + const historyMock = { push: jest.fn() }; + const loadLoopFunction = jest.fn(); + const handleSave = jest.spyOn(LoopPropertiesModal.prototype, 'handleSave'); + LoopService.updateGlobalProperties = jest.fn().mockImplementation(() => { + return Promise.resolve({ + ok: true, + status: 200, + text: () => "OK" + }); + }); - const component = shallow(<LoopPropertiesModal history={historyMock} - loopCache={loopCache} loadLoopFunction={loadLoopFunction} />) + const component = shallow(<LoopPropertiesModal history={ historyMock } + loopCache={ loopCache } loadLoopFunction={ loadLoopFunction }/>) - component.find('[variant="primary"]').prop('onClick')(); - await flushPromises(); - component.update(); + component.find('[variant="primary"]').prop('onClick')(); + await flushPromises(); + component.update(); - expect(handleSave).toHaveBeenCalledTimes(1); - expect(component.state('show')).toEqual(false); - expect(historyMock.push.mock.calls[0]).toEqual([ '/']); - }); + expect(handleSave).toHaveBeenCalledTimes(1); + expect(component.state('show')).toEqual(false); + expect(historyMock.push.mock.calls[0]).toEqual(['/']); + }); - it('Onchange event', () => { - const event = {target:{name:"dcaeDeployParameters", value:"{\"location_id\": \"testLocation\",\"policy_id\": \"TCA_h2NMX_v1_0_ResourceInstanceName1_tca\"}"}}; - const component = shallow(<LoopPropertiesModal loopCache={loopCache}/>); + it('Onchange event', () => { + const event = { target: { name: "dcaeDeployParameters", value: "{\"location_id\": \"testLocation\",\"policy_id\": \"TCA_h2NMX_v1_0_ResourceInstanceName1_tca\"}" } }; + const component = shallow(<LoopPropertiesModal loopCache={ loopCache }/>); - component.find('FormControl').simulate('change', event); - component.update(); + component.find('FormControl').simulate('change', event); + component.update(); - expect(component.state('temporaryPropertiesJson').dcaeDeployParameters.location_id).toEqual("testLocation"); - }); + expect(component.state('temporaryPropertiesJson').dcaeDeployParameters.location_id).toEqual("testLocation"); + }); }); diff --git a/runtime/ui-react/src/components/dialogs/Loop/ModifyLoopModal.js b/runtime/ui-react/src/components/dialogs/Loop/ModifyLoopModal.js index f6c0d2ede..0d203418b 100644 --- a/runtime/ui-react/src/components/dialogs/Loop/ModifyLoopModal.js +++ b/runtime/ui-react/src/components/dialogs/Loop/ModifyLoopModal.js @@ -40,208 +40,223 @@ import Tab from 'react-bootstrap/Tab'; import Alert from 'react-bootstrap/Alert'; const ModalStyled = styled(Modal)` - background-color: transparent; + background-color: transparent; ` const TextModal = styled.textarea` - margin-top: 20px; - white-space:pre; - background-color: ${props => props.theme.toscaTextareaBackgroundColor}; - text-align: justify; - font-size: ${props => props.theme.toscaTextareaFontSize}; - width: 100%; - height: 300px; + margin-top: 20px; + white-space: pre; + background-color: ${ props => props.theme.toscaTextareaBackgroundColor }; + text-align: justify; + font-size: ${ props => props.theme.toscaTextareaFontSize }; + width: 100%; + height: 300px; ` const cellStyle = { border: '1px solid black' }; -const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' }; -const rowHeaderStyle = {backgroundColor:'#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black'}; +const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' }; +const rowHeaderStyle = { backgroundColor: '#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black' }; export default class ModifyLoopModal extends React.Component { - state = { - show: true, - loopCache: this.props.loopCache, - content: 'Please select Tosca model to view the details', - selectedRowData: {}, - toscaPolicyModelsData: [], - selectedPolicyModelsData: [], - key: 'add', - showFailAlert: false, - toscaColumns: [ - { title: "#", field: "index", render: rowData => rowData.tableData.id + 1, - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { title: "Policy Model Type", field: "policyModelType", - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { title: "Policy Acronym", field: "policyAcronym", - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { title: "Policy Name", field: "policyName", - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { title: "Version", field: "version", - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { title: "Uploaded By", field: "updatedBy", - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { title: "Uploaded Date", field: "updatedDate", editable: 'never', - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { title: "Created Date", field: "createdDate", editable: 'never', - cellStyle: cellStyle, - headerStyle: headerStyle - } - ], - tableIcons: { - FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />), - LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />), - NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />), - PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />), - ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />), - Search: forwardRef((props, ref) => <Search {...props} ref={ref} />), - SortArrow: forwardRef((props, ref) => <ArrowUpward {...props} ref={ref} />) - } - }; + state = { + show: true, + loopCache: this.props.loopCache, + content: 'Please select Tosca model to view the details', + selectedRowData: {}, + toscaPolicyModelsData: [], + selectedPolicyModelsData: [], + key: 'add', + showFailAlert: false, + toscaColumns: [ + { + title: "#", field: "index", render: rowData => rowData.tableData.id + 1, + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Policy Model Type", field: "policyModelType", + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Policy Acronym", field: "policyAcronym", + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Policy Name", field: "policyName", + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Version", field: "version", + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Uploaded By", field: "updatedBy", + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Uploaded Date", field: "updatedDate", editable: 'never', + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Created Date", field: "createdDate", editable: 'never', + cellStyle: cellStyle, + headerStyle: headerStyle + } + ], + tableIcons: { + FirstPage: forwardRef((props, ref) => <FirstPage { ...props } ref={ ref }/>), + LastPage: forwardRef((props, ref) => <LastPage { ...props } ref={ ref }/>), + NextPage: forwardRef((props, ref) => <ChevronRight { ...props } ref={ ref }/>), + PreviousPage: forwardRef((props, ref) => <ChevronLeft { ...props } ref={ ref }/>), + ResetSearch: forwardRef((props, ref) => <Clear { ...props } ref={ ref }/>), + Search: forwardRef((props, ref) => <Search { ...props } ref={ ref }/>), + SortArrow: forwardRef((props, ref) => <ArrowUpward { ...props } ref={ ref }/>) + } + }; - constructor(props, context) { - super(props, context); - this.handleClose = this.handleClose.bind(this); - this.initializeToscaPolicyModelsInfo = this.initializeToscaPolicyModelsInfo.bind(this); - this.handleYamlContent = this.handleYamlContent.bind(this); - this.getToscaPolicyModelYaml = this.getToscaPolicyModelYaml.bind(this); - this.handleAdd = this.handleAdd.bind(this); - this.handleRemove = this.handleRemove.bind(this); - this.initializeToscaPolicyModelsInfo(); - } + constructor(props, context) { + super(props, context); + this.handleClose = this.handleClose.bind(this); + this.initializeToscaPolicyModelsInfo = this.initializeToscaPolicyModelsInfo.bind(this); + this.handleYamlContent = this.handleYamlContent.bind(this); + this.getToscaPolicyModelYaml = this.getToscaPolicyModelYaml.bind(this); + this.handleAdd = this.handleAdd.bind(this); + this.handleRemove = this.handleRemove.bind(this); + this.initializeToscaPolicyModelsInfo(); + } - initializeToscaPolicyModelsInfo() { - var operationalPolicies = this.state.loopCache.getOperationalPolicies(); - var selectedPolicyModels = []; - for (var policy in operationalPolicies) { - var newRow = operationalPolicies[policy]["policyModel"]; - newRow["policyName"] = operationalPolicies[policy].name; - selectedPolicyModels.push(newRow); - } + initializeToscaPolicyModelsInfo() { + var operationalPolicies = this.state.loopCache.getOperationalPolicies(); + var selectedPolicyModels = []; + for (var policy in operationalPolicies) { + var newRow = operationalPolicies[policy]["policyModel"]; + newRow["policyName"] = operationalPolicies[policy].name; + selectedPolicyModels.push(newRow); + } - PolicyToscaService.getToscaPolicyModels().then(allToscaModels => { - this.setState({ toscaPolicyModelsData: allToscaModels, - selectedPolicyModelsData: selectedPolicyModels}); - }); - } + PolicyToscaService.getToscaPolicyModels().then(allToscaModels => { + this.setState({ + toscaPolicyModelsData: allToscaModels, + selectedPolicyModelsData: selectedPolicyModels + }); + }); + } - getToscaPolicyModelYaml(policyModelType, policyModelVersion) { - if (typeof policyModelType !== "undefined") { - PolicyToscaService.getToscaPolicyModelYaml(policyModelType, policyModelVersion).then(toscaYaml => { - if (toscaYaml.length !== 0) { - this.setState({content: toscaYaml}) - } else { - this.setState({ content: 'No Tosca model Yaml available' }) - } - }); - } else { - this.setState({ content: 'Please select Tosca model to view the details' }) - } - } + getToscaPolicyModelYaml(policyModelType, policyModelVersion) { + if (typeof policyModelType !== "undefined") { + PolicyToscaService.getToscaPolicyModelYaml(policyModelType, policyModelVersion).then(toscaYaml => { + if (toscaYaml.length !== 0) { + this.setState({ content: toscaYaml }) + } else { + this.setState({ content: 'No Tosca model Yaml available' }) + } + }); + } else { + this.setState({ content: 'Please select Tosca model to view the details' }) + } + } - handleYamlContent(event) { - this.setState({ content: event.target.value }); - } + handleYamlContent(event) { + this.setState({ content: event.target.value }); + } - handleClose() { - this.setState({ show: false }); - this.props.history.push('/'); - } + handleClose() { + this.setState({ show: false }); + this.props.history.push('/'); + } - renderAlert() { - return ( - <div> - <Alert variant="danger" show={this.state.showFailAlert} onClose={this.disableAlert} dismissible> - {this.state.showMessage} - </Alert> - </div> - ); - } + renderAlert() { + return ( + <div> + <Alert variant="danger" show={ this.state.showFailAlert } onClose={ this.disableAlert } dismissible> + { this.state.showMessage } + </Alert> + </div> + ); + } - handleAdd() { - LoopService.addOperationalPolicyType(this.state.loopCache.getLoopName(),this.state.selectedRowData.policyModelType,this.state.selectedRowData.version) - .then(pars => { - this.props.loadLoopFunction(this.state.loopCache.getLoopName()); - this.handleClose(); - }) - .catch(error => { - this.setState({ showFailAlert: true, showMessage: "Adding failed with error: " + error.message}); - }); - } + handleAdd() { + LoopService.addOperationalPolicyType(this.state.loopCache.getLoopName(), this.state.selectedRowData.policyModelType, this.state.selectedRowData.version) + .then(pars => { + this.props.loadLoopFunction(this.state.loopCache.getLoopName()); + this.handleClose(); + }) + .catch(error => { + this.setState({ showFailAlert: true, showMessage: "Adding failed with error: " + error.message }); + }); + } - handleRemove() { - LoopService.removeOperationalPolicyType(this.state.loopCache.getLoopName(),this.state.selectedRowData.policyModelType,this.state.selectedRowData.version,this.state.selectedRowData.policyName); - this.props.loadLoopFunction(this.state.loopCache.getLoopName()); - this.handleClose(); - } + handleRemove() { + LoopService.removeOperationalPolicyType(this.state.loopCache.getLoopName(), this.state.selectedRowData.policyModelType, this.state.selectedRowData.version, this.state.selectedRowData.policyName); + this.props.loadLoopFunction(this.state.loopCache.getLoopName()); + this.handleClose(); + } - render() { - return ( - <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} > - <Modal.Header closeButton> - <Modal.Title>Modify Loop Operational Policies</Modal.Title> - </Modal.Header> - <Tabs id="controlled-tab-example" activeKey={this.state.key} onSelect={key => this.setState({ key, selectedRowData: {} })}> - <Tab eventKey="add" title="Add Operational Policies"> - <Modal.Body> - <MaterialTable - title={"View Tosca Policy Models"} - data={this.state.toscaPolicyModelsData} - columns={this.state.toscaColumns} - icons={this.state.tableIcons} - onRowClick={(event, rowData) => {this.getToscaPolicyModelYaml(rowData.policyModelType, rowData.version);this.setState({selectedRowData: rowData})}} - options={{ - headerStyle: rowHeaderStyle, - rowStyle: rowData => ({ - backgroundColor: (this.state.selectedRowData !== {} && this.state.selectedRowData.tableData !== undefined - && this.state.selectedRowData.tableData.id === rowData.tableData.id) ? '#EEE' : '#FFF' - }) - }} - /> - <div> - <TextModal value={this.state.content} onChange={this.handleYamlContent}/> - </div> - </Modal.Body> - {this.renderAlert()} - </Tab> - <Tab eventKey="remove" title="Remove Operational Policies"> - <Modal.Body> - <MaterialTable - title={"Tosca Policy Models already added"} - data={this.state.selectedPolicyModelsData} - columns={this.state.toscaColumns} - icons={this.state.tableIcons} - onRowClick={(event, rowData) => {this.setState({selectedRowData: rowData})}} - options={{ - headerStyle: rowHeaderStyle, - rowStyle: rowData => ({ - backgroundColor: (this.state.selectedRowData !== {} && this.state.selectedRowData.tableData !== undefined - && this.state.selectedRowData.tableData.id === rowData.tableData.id) ? '#EEE' : '#FFF' - }) - }} - /> - </Modal.Body> - </Tab> - </Tabs> - <Modal.Footer> - <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button> - <Button variant="primary" disabled={(this.state.key === "remove")} type="submit" onClick={this.handleAdd}>Add</Button> - <Button variant="primary" disabled={(this.state.key === "add")} type="submit" onClick={this.handleRemove}>Remove</Button> - </Modal.Footer> + render() { + return ( + <ModalStyled size="xl" show={ this.state.show } onHide={ this.handleClose } backdrop="static" keyboard={ false }> + <Modal.Header closeButton> + <Modal.Title>Modify Loop Operational Policies</Modal.Title> + </Modal.Header> + <Tabs id="controlled-tab-example" activeKey={ this.state.key } onSelect={ key => this.setState({ key, selectedRowData: {} }) }> + <Tab eventKey="add" title="Add Operational Policies"> + <Modal.Body> + <MaterialTable + title={ "View Tosca Policy Models" } + data={ this.state.toscaPolicyModelsData } + columns={ this.state.toscaColumns } + icons={ this.state.tableIcons } + onRowClick={ (event, rowData) => { + this.getToscaPolicyModelYaml(rowData.policyModelType, rowData.version); + this.setState({ selectedRowData: rowData }) + } } + options={ { + headerStyle: rowHeaderStyle, + rowStyle: rowData => ({ + backgroundColor: (this.state.selectedRowData !== {} && this.state.selectedRowData.tableData !== undefined + && this.state.selectedRowData.tableData.id === rowData.tableData.id) ? '#EEE' : '#FFF' + }) + } } + /> + <div> + <TextModal value={ this.state.content } onChange={ this.handleYamlContent }/> + </div> + </Modal.Body> + { this.renderAlert() } + </Tab> + <Tab eventKey="remove" title="Remove Operational Policies"> + <Modal.Body> + <MaterialTable + title={ "Tosca Policy Models already added" } + data={ this.state.selectedPolicyModelsData } + columns={ this.state.toscaColumns } + icons={ this.state.tableIcons } + onRowClick={ (event, rowData) => { + this.setState({ selectedRowData: rowData }) + } } + options={ { + headerStyle: rowHeaderStyle, + rowStyle: rowData => ({ + backgroundColor: (this.state.selectedRowData !== {} && this.state.selectedRowData.tableData !== undefined + && this.state.selectedRowData.tableData.id === rowData.tableData.id) ? '#EEE' : '#FFF' + }) + } } + /> + </Modal.Body> + </Tab> + </Tabs> + <Modal.Footer> + <Button variant="secondary" type="null" onClick={ this.handleClose }>Cancel</Button> + <Button variant="primary" disabled={ (this.state.key === "remove") } type="submit" onClick={ this.handleAdd }>Add</Button> + <Button variant="primary" disabled={ (this.state.key === "add") } type="submit" onClick={ this.handleRemove }>Remove</Button> + </Modal.Footer> - </ModalStyled> - ); - } + </ModalStyled> + ); + } } diff --git a/runtime/ui-react/src/components/dialogs/Loop/ModifyLoopModal.test.js b/runtime/ui-react/src/components/dialogs/Loop/ModifyLoopModal.test.js index 055ad0e68..79267af4f 100644 --- a/runtime/ui-react/src/components/dialogs/Loop/ModifyLoopModal.test.js +++ b/runtime/ui-react/src/components/dialogs/Loop/ModifyLoopModal.test.js @@ -28,82 +28,82 @@ import LoopService from '../../../api/LoopService'; import PolicyToscaService from '../../../api/PolicyToscaService'; describe('Verify ModifyLoopModal', () => { - beforeEach(() => { - PolicyToscaService.getToscaPolicyModels = jest.fn().mockImplementation(() => { - return Promise.resolve([{ - "policyModelType":"test", - "policyAcronym":"test", - "version":"1.0.0", - "updatedBy":"", - "updatedDate":"" - }]); - }); - PolicyToscaService.getToscaPolicyModelYaml = jest.fn().mockImplementation(() => { - return Promise.resolve("OK"); - }); - }) + beforeEach(() => { + PolicyToscaService.getToscaPolicyModels = jest.fn().mockImplementation(() => { + return Promise.resolve([{ + "policyModelType": "test", + "policyAcronym": "test", + "version": "1.0.0", + "updatedBy": "", + "updatedDate": "" + }]); + }); + PolicyToscaService.getToscaPolicyModelYaml = jest.fn().mockImplementation(() => { + return Promise.resolve("OK"); + }); + }) - const loopCache = new LoopCache({ - "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", - "microServicePolicies": [{ - "name": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca", - "modelType": "onap.policies.monitoring.cdap.tca.hi.lo.app", - "properties": {"domain": "measurementsForVfScaling"}, - "shared": false, - "jsonRepresentation": {"schema": {}} - }], - "globalPropertiesJson": { - "dcaeDeployParameters": { - "testMs": { - "location_id": "", - "policy_id": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca" - } - } + const loopCache = new LoopCache({ + "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", + "microServicePolicies": [{ + "name": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca", + "modelType": "onap.policies.monitoring.cdap.tca.hi.lo.app", + "properties": { "domain": "measurementsForVfScaling" }, + "shared": false, + "jsonRepresentation": { "schema": {} } + }], + "globalPropertiesJson": { + "dcaeDeployParameters": { + "testMs": { + "location_id": "", + "policy_id": "TCA_h2NMX_v1_0_ResourceInstanceName1_tca" } - }); - const historyMock = { push: jest.fn() }; - const flushPromises = () => new Promise(setImmediate); + } + } + }); + const historyMock = { push: jest.fn() }; + const flushPromises = () => new Promise(setImmediate); - it('Test handleClose', () => { - const handleClose = jest.spyOn(ModifyLoopModal.prototype,'handleClose'); - const component = mount(<ModifyLoopModal history={historyMock} loopCache={loopCache}/>) + it('Test handleClose', () => { + const handleClose = jest.spyOn(ModifyLoopModal.prototype, 'handleClose'); + const component = mount(<ModifyLoopModal history={ historyMock } loopCache={ loopCache }/>) - component.find('[variant="secondary"]').get(0).props.onClick(); + component.find('[variant="secondary"]').get(0).props.onClick(); - expect(handleClose).toHaveBeenCalledTimes(1); - expect(component.state('show')).toEqual(false); - expect(historyMock.push.mock.calls[0]).toEqual([ '/']); - }); + expect(handleClose).toHaveBeenCalledTimes(1); + expect(component.state('show')).toEqual(false); + expect(historyMock.push.mock.calls[0]).toEqual(['/']); + }); - it('Test getToscaPolicyModelYaml', async () => { - const flushPromises = () => new Promise(setImmediate); - const component = mount(<ModifyLoopModal history={historyMock} loopCache={loopCache}/>) - component.setState({ - "selectedRowData": {"tableData":{"id":0}} - }); - const instance = component.instance(); + it('Test getToscaPolicyModelYaml', async () => { + const flushPromises = () => new Promise(setImmediate); + const component = mount(<ModifyLoopModal history={ historyMock } loopCache={ loopCache }/>) + component.setState({ + "selectedRowData": { "tableData": { "id": 0 } } + }); + const instance = component.instance(); - instance.getToscaPolicyModelYaml("","1.0.0"); - expect(component.state('content')).toEqual("Please select Tosca model to view the details"); + instance.getToscaPolicyModelYaml("", "1.0.0"); + expect(component.state('content')).toEqual("Please select Tosca model to view the details"); - instance.getToscaPolicyModelYaml("test","1.0.0"); - await flushPromises(); - expect(component.state('content')).toEqual("OK"); + instance.getToscaPolicyModelYaml("test", "1.0.0"); + await flushPromises(); + expect(component.state('content')).toEqual("OK"); - PolicyToscaService.getToscaPolicyModelYaml = jest.fn().mockImplementation(() => { - return Promise.resolve(""); - }); - instance.getToscaPolicyModelYaml("test","1.0.0"); - await flushPromises(); - expect(component.state('content')).toEqual("No Tosca model Yaml available"); + PolicyToscaService.getToscaPolicyModelYaml = jest.fn().mockImplementation(() => { + return Promise.resolve(""); }); + instance.getToscaPolicyModelYaml("test", "1.0.0"); + await flushPromises(); + expect(component.state('content')).toEqual("No Tosca model Yaml available"); + }); - it('Test handleYamlContent', async () => { - const component = mount(<ModifyLoopModal loopCache={loopCache}/>) - const instance = component.instance(); + it('Test handleYamlContent', async () => { + const component = mount(<ModifyLoopModal loopCache={ loopCache }/>) + const instance = component.instance(); - const event = {"target":{"value":"testValue"}} - instance.handleYamlContent(event); - expect(component.state('content')).toEqual("testValue"); - }); -});
\ No newline at end of file + const event = { "target": { "value": "testValue" } } + instance.handleYamlContent(event); + expect(component.state('content')).toEqual("testValue"); + }); +}); diff --git a/runtime/ui-react/src/components/dialogs/Loop/OpenLoopModal.js b/runtime/ui-react/src/components/dialogs/Loop/OpenLoopModal.js index b45df6502..b6407fbbb 100644 --- a/runtime/ui-react/src/components/dialogs/Loop/OpenLoopModal.js +++ b/runtime/ui-react/src/components/dialogs/Loop/OpenLoopModal.js @@ -34,104 +34,106 @@ import SvgGenerator from '../../loop_viewer/svg/SvgGenerator'; import LoopCache from '../../../api/LoopCache'; const ModalStyled = styled(Modal)` - background-color: transparent; + background-color: transparent; ` const CheckBoxStyled = styled(FormCheck.Input)` - margin-left:3rem; + margin-left: 3rem; ` export default class OpenLoopModal extends React.Component { - constructor(props, context) { - super(props, context); + constructor(props, context) { + super(props, context); - this.getLoopNames = this.getLoopNames.bind(this); - this.handleOpen = this.handleOpen.bind(this); - this.handleClose = this.handleClose.bind(this); - this.handleDropDownListChange = this.handleDropDownListChange.bind(this); - this.renderSvg = this.renderSvg.bind(this); - this.showReadOnly = props.showReadOnly !== undefined ? props.showReadOnly : true; - this.state = { - show: true, - chosenLoopName: '', - loopNames: [], - loopCacheOpened: new LoopCache({}) - }; - } + this.getLoopNames = this.getLoopNames.bind(this); + this.handleOpen = this.handleOpen.bind(this); + this.handleClose = this.handleClose.bind(this); + this.handleDropDownListChange = this.handleDropDownListChange.bind(this); + this.renderSvg = this.renderSvg.bind(this); + this.showReadOnly = props.showReadOnly !== undefined ? props.showReadOnly : true; + this.state = { + show: true, + chosenLoopName: '', + loopNames: [], + loopCacheOpened: new LoopCache({}) + }; + } - componentWillMount() { - this.getLoopNames(); - } + componentWillMount() { + this.getLoopNames(); + } - handleClose() { - this.setState({ show: false }); - this.props.history.push('/'); - } + handleClose() { + this.setState({ show: false }); + this.props.history.push('/'); + } - handleDropDownListChange(e) { - LoopService.getLoop(e.value).then(loop => { - this.setState({ - chosenLoopName: e.value, - loopCacheOpened: new LoopCache(loop) - }); - }); - } + handleDropDownListChange(e) { + LoopService.getLoop(e.value).then(loop => { + this.setState({ + chosenLoopName: e.value, + loopCacheOpened: new LoopCache(loop) + }); + }); + } - getLoopNames() { - LoopService.getLoopNames().then(loopNames => { - if (Object.entries(loopNames).length !== 0) { - const loopOptions = loopNames.filter(loopName => loopName!=='undefined').map((loopName) => { return { label: loopName, value: loopName } }); - this.setState({ loopNames: loopOptions }) - } - }); - } + getLoopNames() { + LoopService.getLoopNames().then(loopNames => { + if (Object.entries(loopNames).length !== 0) { + const loopOptions = loopNames.filter(loopName => loopName !== 'undefined').map((loopName) => { + return { label: loopName, value: loopName } + }); + this.setState({ loopNames: loopOptions }) + } + }); + } - handleOpen() { - console.info("Loop " + this.state.chosenLoopName + " is chosen"); - this.handleClose(); - this.props.loadLoopFunction(this.state.chosenLoopName); - } + handleOpen() { + console.info("Loop " + this.state.chosenLoopName + " is chosen"); + this.handleClose(); + this.props.loadLoopFunction(this.state.chosenLoopName); + } - renderSvg() { - return( - <SvgGenerator loopCache={this.state.loopCacheOpened} clickable={false} generatedFrom={SvgGenerator.GENERATED_FROM_INSTANCE}/> - ); - } + renderSvg() { + return ( + <SvgGenerator loopCache={ this.state.loopCacheOpened } clickable={ false } generatedFrom={ SvgGenerator.GENERATED_FROM_INSTANCE }/> + ); + } - render() { - return ( - <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} > - <Modal.Header closeButton> - <Modal.Title>Open Model</Modal.Title> - </Modal.Header> - <Modal.Body> - <Form.Group as={Row} controlId="formPlaintextEmail"> - <Form.Label column sm="2">Model Name:</Form.Label> - <Col sm="10"> - <Select onChange={this.handleDropDownListChange} - options={this.state.loopNames} /> - </Col> - </Form.Group> - <Form.Group as={Row} style={{alignItems: 'center'}} controlId="formSvgPreview"> - <Form.Label column sm="2">Model Preview:</Form.Label> - <Col sm="10"> - {this.renderSvg()} - </Col> - </Form.Group> - {this.showReadOnly === true ? - <Form.Group as={Row} controlId="formBasicCheckbox"> - <Form.Check> - <FormCheck.Label>Read Only Mode:</FormCheck.Label> - <CheckBoxStyled style={{marginLeft: '3.5em'}} type="checkbox" /> - </Form.Check> - </Form.Group> - : null} - </Modal.Body> - <Modal.Footer> - <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button> - <Button variant="primary" type="submit" onClick={this.handleOpen}>Open</Button> - </Modal.Footer> - </ModalStyled> + render() { + return ( + <ModalStyled size="xl" show={ this.state.show } onHide={ this.handleClose } backdrop="static" keyboard={ false }> + <Modal.Header closeButton> + <Modal.Title>Open Model</Modal.Title> + </Modal.Header> + <Modal.Body> + <Form.Group as={ Row } controlId="formPlaintextEmail"> + <Form.Label column sm="2">Model Name:</Form.Label> + <Col sm="10"> + <Select onChange={ this.handleDropDownListChange } + options={ this.state.loopNames }/> + </Col> + </Form.Group> + <Form.Group as={ Row } style={ { alignItems: 'center' } } controlId="formSvgPreview"> + <Form.Label column sm="2">Model Preview:</Form.Label> + <Col sm="10"> + { this.renderSvg() } + </Col> + </Form.Group> + { this.showReadOnly === true ? + <Form.Group as={ Row } controlId="formBasicCheckbox"> + <Form.Check> + <FormCheck.Label>Read Only Mode:</FormCheck.Label> + <CheckBoxStyled style={ { marginLeft: '3.5em' } } type="checkbox"/> + </Form.Check> + </Form.Group> + : null } + </Modal.Body> + <Modal.Footer> + <Button variant="secondary" type="null" onClick={ this.handleClose }>Cancel</Button> + <Button variant="primary" type="submit" onClick={ this.handleOpen }>Open</Button> + </Modal.Footer> + </ModalStyled> - ); - } + ); + } } diff --git a/runtime/ui-react/src/components/dialogs/Loop/OpenLoopModal.test.js b/runtime/ui-react/src/components/dialogs/Loop/OpenLoopModal.test.js index 1865869df..6b2dc4afb 100644 --- a/runtime/ui-react/src/components/dialogs/Loop/OpenLoopModal.test.js +++ b/runtime/ui-react/src/components/dialogs/Loop/OpenLoopModal.test.js @@ -28,15 +28,15 @@ import LoopService from '../../../api/LoopService'; describe('Verify OpenLoopModal', () => { beforeEach(() => { - fetch.resetMocks(); - fetch.mockResponse(JSON.stringify([ - "LOOP_gmtAS_v1_0_ResourceInstanceName1_tca", - "LOOP_gmtAS_v1_0_ResourceInstanceName1_tca_3", - "LOOP_gmtAS_v1_0_ResourceInstanceName2_tca_2" - ])); + fetch.resetMocks(); + fetch.mockResponse(JSON.stringify([ + "LOOP_gmtAS_v1_0_ResourceInstanceName1_tca", + "LOOP_gmtAS_v1_0_ResourceInstanceName1_tca_3", + "LOOP_gmtAS_v1_0_ResourceInstanceName2_tca_2" + ])); }); - it('Test the render method', () => { + it('Test the render method', () => { const component = shallow(<OpenLoopModal/>); expect(component).toMatchSnapshot(); @@ -45,13 +45,14 @@ describe('Verify OpenLoopModal', () => { it('Onchange event', async () => { const flushPromises = () => new Promise(setImmediate); LoopService.getLoop = jest.fn().mockImplementation(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => {} - }); - }); - const event = {value: 'LOOP_gmtAS_v1_0_ResourceInstanceName1_tca_3'}; + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + } + }); + }); + const event = { value: 'LOOP_gmtAS_v1_0_ResourceInstanceName1_tca_3' }; const component = shallow(<OpenLoopModal/>); component.find('StateManager').simulate('change', event); await flushPromises(); @@ -61,30 +62,30 @@ describe('Verify OpenLoopModal', () => { it('Test handleClose', () => { - const historyMock = { push: jest.fn() }; - const handleClose = jest.spyOn(OpenLoopModal.prototype,'handleClose'); - const component = shallow(<OpenLoopModal history={historyMock} />) + const historyMock = { push: jest.fn() }; + const handleClose = jest.spyOn(OpenLoopModal.prototype, 'handleClose'); + const component = shallow(<OpenLoopModal history={ historyMock }/>) component.find('[variant="secondary"]').prop('onClick')(); expect(handleClose).toHaveBeenCalledTimes(1); expect(component.state('show')).toEqual(false); - expect(historyMock.push.mock.calls[0]).toEqual([ '/']); - + expect(historyMock.push.mock.calls[0]).toEqual(['/']); + handleClose.mockClear(); }); - it('Test handleSubmit', () => { + it('Test handleSubmit', () => { const historyMock = { push: jest.fn() }; - const loadLoopFunction = jest.fn(); - const handleOpen = jest.spyOn(OpenLoopModal.prototype,'handleOpen'); - const component = shallow(<OpenLoopModal history={historyMock} loadLoopFunction={loadLoopFunction}/>) + const loadLoopFunction = jest.fn(); + const handleOpen = jest.spyOn(OpenLoopModal.prototype, 'handleOpen'); + const component = shallow(<OpenLoopModal history={ historyMock } loadLoopFunction={ loadLoopFunction }/>) component.find('[variant="primary"]').prop('onClick')(); expect(handleOpen).toHaveBeenCalledTimes(1); expect(component.state('show')).toEqual(false); - expect(historyMock.push.mock.calls[0]).toEqual([ '/']); + expect(historyMock.push.mock.calls[0]).toEqual(['/']); handleOpen.mockClear(); }); diff --git a/runtime/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js b/runtime/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js index 90bbc887c..d7ba8d1c6 100644 --- a/runtime/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js +++ b/runtime/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.js @@ -29,7 +29,7 @@ import Col from 'react-bootstrap/Col'; import styled from 'styled-components'; import TemplateMenuService from '../../../api/TemplateService'; import CsvToJson from '../../../utils/CsvToJson'; -import MaterialTable, {MTableToolbar} from "material-table"; +import MaterialTable, { MTableToolbar } from "material-table"; import IconButton from '@material-ui/core/IconButton'; import Tooltip from '@material-ui/core/Tooltip'; import AddBox from '@material-ui/icons/AddBox'; @@ -51,581 +51,587 @@ import ViewColumn from '@material-ui/icons/ViewColumn'; const ModalStyled = styled(Modal)` - @media (min-width: 1200px) { - .modal-xl { - max-width: 96%; - } - } - background-color: transparent; + @media (min-width: 1200px) { + .modal-xl { + max-width: 96%; + } + } + background-color: transparent; ` const MTableToolbarStyled = styled(MTableToolbar)` - display: flex; - flex-direction: row; - align-items: center; + display: flex; + flex-direction: row; + align-items: center; ` const ColPullLeftStyled = styled(Col)` - display: flex; - flex-direction: row; - align-items: center; - margin-left: -40px; + display: flex; + flex-direction: row; + align-items: center; + margin-left: -40px; ` const cellStyle = { border: '1px solid black' }; -const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' }; -const rowHeaderStyle = {backgroundColor:'#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black'}; +const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' }; +const rowHeaderStyle = { backgroundColor: '#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black' }; let dictList = []; let subDictFlag = false; function SelectSubDictType(props) { - const {onChange} = props; - const selectedValues = (e) => { - let options = e.target.options; - let SelectedDictTypes = ''; - for (let dictType = 0, values = options.length; dictType < values; dictType++) { - if (options[dictType].selected) { - SelectedDictTypes = SelectedDictTypes.concat(options[dictType].value); - SelectedDictTypes = SelectedDictTypes.concat('|'); - } - } - SelectedDictTypes = SelectedDictTypes.slice(0,-1); - onChange(SelectedDictTypes); - } - // When the subDictFlag is true, we need to disable selection of element "type" - return( - <div> - <select disabled={subDictFlag} multiple={true} onChange={selectedValues}> - <option value="string">string</option> - <option value="number">number</option> - <option value="datetime">datetime</option> - <option value="map">map</option> - <option value="json">json</option> - </select> - </div> - ); + const { onChange } = props; + const selectedValues = (e) => { + let options = e.target.options; + let SelectedDictTypes = ''; + for (let dictType = 0, values = options.length; dictType < values; dictType++) { + if (options[dictType].selected) { + SelectedDictTypes = SelectedDictTypes.concat(options[dictType].value); + SelectedDictTypes = SelectedDictTypes.concat('|'); + } + } + SelectedDictTypes = SelectedDictTypes.slice(0, -1); + onChange(SelectedDictTypes); + } + // When the subDictFlag is true, we need to disable selection of element "type" + return ( + <div> + <select disabled={ subDictFlag } multiple={ true } onChange={ selectedValues }> + <option value="string">string</option> + <option value="number">number</option> + <option value="datetime">datetime</option> + <option value="map">map</option> + <option value="json">json</option> + </select> + </div> + ); } function SubDict(props) { - const {onChange} = props; - const subDicts = []; - subDicts.push('none'); - if (dictList !== undefined && dictList.length > 0) { - let item; - for(item in dictList) { - if(dictList[item].secondLevelDictionary === 1) { - subDicts.push(dictList[item].name); - } - } - } - let optionItems = []; - for (let i=0; i<subDicts.length; ++i) { - if (i === 0) { - optionItems.push(<option selected key={subDicts[i]}>{subDicts[i]}</option>); - } else { - optionItems.push(<option key={subDicts[i]}>{subDicts[i]}</option>); - } - } - - function selectedValue (e) { - onChange(e.target.value); - } - // When the subDictFlag is true, we need to disable selection of - // the sub-dictionary flag - return( - <select disabled={subDictFlag} onChange={selectedValue} > - {optionItems} - </select> - ); + const { onChange } = props; + const subDicts = []; + subDicts.push('none'); + if (dictList !== undefined && dictList.length > 0) { + let item; + for (item in dictList) { + if (dictList[item].secondLevelDictionary === 1) { + subDicts.push(dictList[item].name); + } + } + } + let optionItems = []; + for (let i = 0; i < subDicts.length; ++i) { + if (i === 0) { + optionItems.push(<option selected key={ subDicts[i] }>{ subDicts[i] }</option>); + } else { + optionItems.push(<option key={ subDicts[i] }>{ subDicts[i] }</option>); + } + } + + function selectedValue(e) { + onChange(e.target.value); + } + + // When the subDictFlag is true, we need to disable selection of + // the sub-dictionary flag + return ( + <select disabled={ subDictFlag } onChange={ selectedValue }> + { optionItems } + </select> + ); } export default class ManageDictionaries extends React.Component { - constructor(props, context) { - super(props, context); - this.addDictionaryElementRow = this.addDictionaryElementRow.bind(this); - this.addDictionaryRow = this.addDictionaryRow.bind(this); - this.addReplaceDictionaryRequest = this.addReplaceDictionaryRequest.bind(this); - this.clickHandler = this.clickHandler.bind(this); - this.deleteDictionaryElementRow = this.deleteDictionaryElementRow.bind(this); - this.deleteDictionaryRequest = this.deleteDictionaryRequest.bind(this); - this.deleteDictionaryRow = this.deleteDictionaryRow.bind(this); - this.fileSelectedHandler = this.fileSelectedHandler.bind(this); - this.getDictionaries = this.getDictionaries.bind(this); - this.getDictionaryElements = this.getDictionaryElements.bind(this); - this.handleClose = this.handleClose.bind(this); - this.handleDictionaryRowClick = this.handleDictionaryRowClick.bind(this); - this.importCsvData = this.importCsvData.bind(this); - this.updateDictionaryElementRow = this.updateDictionaryElementRow.bind(this); - this.updateDictionaryElementsRequest = this.updateDictionaryElementsRequest.bind(this); - this.updateDictionaryRow = this.updateDictionaryRow.bind(this); - this.readOnly = props.readOnly !== undefined ? props.readOnly : false; - this.state = { - show: true, - currentSelectedDictionary: null, - exportFilename: '', - content: null, - dictionaryElements: [], - tableIcons: { - Add: forwardRef((props, ref) => <AddBox {...props} ref={ref} />), - Delete: forwardRef((props, ref) => <DeleteOutline {...props} ref={ref} />), - DetailPanel: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />), - Edit: forwardRef((props, ref) => <Edit {...props} ref={ref} />), - Check: forwardRef((props, ref) => <Check {...props} ref={ref} />), - Clear: forwardRef((props, ref) => <Clear {...props} ref={ref} />), - Export: forwardRef((props, ref) => <VerticalAlignBottomIcon {...props} ref={ref} />), - Filter: forwardRef((props, ref) => <FilterList {...props} ref={ref} />), - FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />), - LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />), - NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />), - PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />), - ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />), - Search: forwardRef((props, ref) => <Search {...props} ref={ref} />), - SortArrow: forwardRef((props, ref) => <ArrowUpward {...props} ref={ref} />), - ThirdStateCheck: forwardRef((props, ref) => <Remove {...props} ref={ref} />), - ViewColumn: forwardRef((props, ref) => <ViewColumn {...props} ref={ref} />) - }, - dictColumns: [ - { - title: "Dictionary Name", field: "name",editable: 'onAdd', - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { - title: "Sub Dictionary ?", field: "secondLevelDictionary", lookup: {0: 'No', 1: 'Yes'}, - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { - title: "Dictionary Type", field: "subDictionaryType",lookup: {string: 'string', number: 'number'}, - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { - title: "Updated By", field: "updatedBy", editable: 'never', - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { - title: "Last Updated Date", field: "updatedDate", editable: 'never', - cellStyle: cellStyle, - headerStyle: headerStyle - } - ], - dictElementColumns: [ - { - title: "Element Short Name", field: "shortName",editable: 'onAdd', - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { - title: "Element Name", field: "name", - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { - title: "Element Description", field: "description", - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { - title: "Element Type", field: "type", - editComponent: props => ( - <div> - <SelectSubDictType value={props.value} onChange={props.onChange} /> - </div> - ), - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { - title: "Sub-Dictionary", field: "subDictionary", - editComponent: props => ( - <div> - <SubDict value={props.value} onChange={props.onChange} /> - </div> - ), - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { - title: "Updated By", field: "updatedBy", editable: 'never', - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { - title: "Updated Date", field: "updatedDate", editable: 'never', - cellStyle: cellStyle, - headerStyle: headerStyle - } - ] - } - } - - componentDidMount() { - this.getDictionaries(); - } - - getDictionaries() { - TemplateMenuService.getDictionary().then(arrayOfdictionaries => { - this.setState({ dictionaries: arrayOfdictionaries, currentSelectedDictionary: null }) - // global variable setting used functional components in this file - dictList = arrayOfdictionaries; - }).catch(() => { - console.error('Failed to retrieve dictionaries'); - this.setState({ dictionaries: [], currentSelectedDictionary: null }) - }); - } - - getDictionaryElements(dictionaryName) { - TemplateMenuService.getDictionaryElements(dictionaryName).then(dictionaryElements => { - this.setState({ dictionaryElements: dictionaryElements.dictionaryElements} ); - this.setState({ currentSelectDictionary: dictionaryName }); - }).catch(() => console.error('Failed to retrieve dictionary elements')) - } - - clickHandler(rowData) { - this.getDictionaries(); - } - - handleClose() { - this.setState({ show: false }); - this.props.history.push('/'); - } - - addReplaceDictionaryRequest(dictionaryEntry) { - TemplateMenuService.insDictionary(dictionaryEntry) - .then(resp => { - this.getDictionaries(); - }) - .catch(() => console.error('Failed to insert new dictionary elements')); - } - - updateDictionaryElementsRequest(dictElements) { - let reqData = { "name": this.state.currentSelectedDictionary, 'dictionaryElements': dictElements }; - TemplateMenuService.insDictionaryElements(reqData) - .then(resp => { this.getDictionaryElements(this.state.currentSelectedDictionary) }) - .catch(() => console.error('Failed to update dictionary elements')); - } - - deleteDictionaryRequest(dictionaryName) { - TemplateMenuService.deleteDictionary(dictionaryName) - .then(resp => { - this.getDictionaries(); - }) - .catch(() => console.error('Failed to delete dictionary')); - } - - deleteDictionaryElementRequest(dictionaryName, elemenetShortName) { - TemplateMenuService.deleteDictionaryElements({ 'name': dictionaryName, 'shortName': elemenetShortName }) - .then(resp => { - this.getDictionaryElements(dictionaryName); - }) - .catch(() => console.error('Failed to delete dictionary elements')); - } - - fileSelectedHandler = (event) => { - - if (event.target.files[0].type === 'text/csv' || event.target.files[0].type === 'application/vnd.ms-excel') { - if (event.target.files && event.target.files[0]) { - const reader = new FileReader(); - reader.onload = (e) => { - let errorMessages = this.importCsvData(reader.result); - if (errorMessages !== '') { - alert(errorMessages); - } - } - reader.readAsText(event.target.files[0]); - } - } else { - alert('Please upload .csv extention files only.'); - } - } - - importCsvData(rawCsvData) { - - const jsonKeyNames = [ 'shortName', 'name', 'description', 'type', 'subDictionary' ]; - const userHeaderNames = [ 'Element Short Name', 'Element Name', 'Element Description', 'Element Type', 'Sub-Dictionary' ]; - const validTypes = ['string','number','datetime','json','map']; - - let mandatory; - - if (subDictFlag) { - mandatory = [ true, true, true, false, false ]; - } else { - mandatory = [ true, true, true, true, false ]; - } - - let result = CsvToJson(rawCsvData, ',', '||||', userHeaderNames, jsonKeyNames, mandatory); - - let errorMessages = result.errorMessages; - let jsonObjArray = result.jsonObjArray; - - let validTypesErrorMesg = ''; - - for (let i=0; i < validTypes.length; ++i) { - if (i === 0) { - validTypesErrorMesg = validTypes[i]; - } else { - validTypesErrorMesg += ',' + validTypes[i]; - } - } - - if (errorMessages !== '') { - return errorMessages; - } - - // Perform further checks on data that is now in JSON form - let subDictionaries = []; - - // NOTE: dictList is a global variable maintained faithfully - // by the getDictionaries() method outside this import - // functionality. - let item; - for (item in dictList) { - if (dictList[item].secondLevelDictionary === 1) { - subDictionaries.push(dictList[item].name); - } - }; - - // Check for valid Sub-Dictionary and Element Type values - subDictionaries = subDictionaries.toString(); - let row = 2; - let dictElem; - for (dictElem of jsonObjArray) { - let itemKey; - for (itemKey in dictElem){ - let value = dictElem[itemKey].trim(); - let keyIndex = jsonKeyNames.indexOf(itemKey); - if (itemKey === 'shortName' && /[^a-zA-Z0-9-_.]/.test(value)) { - errorMessages += '\n' + userHeaderNames[keyIndex] + - ' at row #' + row + - ' can only contain alphanumeric characters and periods, hyphens or underscores'; - } - if (itemKey === 'type' && validTypes.indexOf(value) < 0) { - errorMessages += '\nInvalid value of "' + value + '" for "' + userHeaderNames[keyIndex] + '" at row #' + row; - errorMessages += '\nValid types are: ' + validTypesErrorMesg; - } - if (value !== "" && itemKey === 'subDictionary' && subDictionaries.indexOf(value) < 0) { - errorMessages += '\nInvalid Sub-Dictionary value of "' + value + '" at row #' + row; - } - } - ++row; - } - if (errorMessages === '') { - // We made it through all the checks. Send it to back end - this.updateDictionaryElementsRequest(jsonObjArray); - } - - return errorMessages; - } - - addDictionaryRow(newData) { - let validData = true; - return new Promise((resolve, reject) => { - setTimeout(() => { - if (/[^a-zA-Z0-9-_.]/.test(newData.name)) { - validData = false; - alert('Please enter alphanumeric input. Only allowed special characters are:(period, hyphen, underscore)'); - reject(); - } - for (let i = 0; i < this.state.dictionaries.length; i++) { - if (this.state.dictionaries[i].name === newData.name) { - validData = false; - alert(newData.name + ' dictionary name already exists') - reject(); - } - } - if (validData) { - this.addReplaceDictionaryRequest(newData); - } - resolve(); - }, 1000); - }); - } - - - updateDictionaryRow(newData, oldData) { - let validData = true; - return new Promise((resolve, reject) => { - setTimeout(() => { - if (/[^a-zA-Z0-9-_.]/.test(newData.name)) { - validData = false; - alert('Please enter alphanumberic input. Only allowed special characters are:(period, hyphen, underscore)'); - reject(); - } - if (validData) { - this.addReplaceDictionaryRequest(newData); - } - resolve(); - }, 1000); - }); - } - - deleteDictionaryRow(oldData) { - return new Promise((resolve, reject) => { - setTimeout(() => { - this.deleteDictionaryRequest(oldData.name); - resolve(); - }, 1000); - }); - } - - addDictionaryElementRow(newData) { - return new Promise((resolve, reject) => { - setTimeout(() => { - let dictionaryElements = this.state.dictionaryElements; - let errorMessages = ''; - for (let i = 0; i < this.state.dictionaryElements.length; i++) { - if (this.state.dictionaryElements[i].shortName === newData.shortName) { - alert('Short Name "' + newData.shortName + '" already exists'); - reject(""); - } - } - // MaterialTable returns no property at all if the user has not touched a - // new column, so we want to add the property with an emptry string - // for several cases if that is the case to simplify other checks. - if (newData.description === undefined) { - newData.description = ""; - } - if (newData.subDictionary === undefined) { - newData.subDictionary = null; - } - if (newData.type === undefined) { - newData.type = ""; - } - if (!newData.shortName && /[^a-zA-Z0-9-_.]/.test(newData.shortName)) { - errorMessages += '\nShort Name is limited to alphanumeric characters and also period, hyphen, and underscore'; - } - if (!newData.shortName){ - errorMessages += '\nShort Name must be specified'; - } - if (!newData.name){ - errorMessages += '\nElement Name must be specified'; - } - if (!newData.type && !subDictFlag){ - errorMessages += '\nElement Type must be specified'; - } - if (errorMessages === '') { - dictionaryElements.push(newData); - this.updateDictionaryElementsRequest([newData]); - resolve(); - } else { - alert(errorMessages); - reject(""); - } - }, 1000); - }); - } - - updateDictionaryElementRow(newData, oldData) { - return new Promise((resolve, reject) => { - setTimeout(() => { - let dictionaryElements = this.state.dictionaryElements; - let validData = true; - if (!newData.type) { - validData = false; - alert('Element Type cannot be null'); - reject(); - } - if (validData) { - const index = dictionaryElements.indexOf(oldData); - dictionaryElements[index] = newData; - this.updateDictionaryElementsRequest([newData]); - } - resolve(); - }, 1000); - }); - } - - - deleteDictionaryElementRow(oldData) { - return new Promise((resolve) => { - setTimeout(() => { - this.deleteDictionaryElementRequest(this.state.currentSelectedDictionary, oldData.shortName); - resolve(); - }, 1000); - }); - } - - handleDictionaryRowClick(event, rowData) { - subDictFlag = rowData.secondLevelDictionary === 1 ? true : false; - this.setState({ - currentSelectedDictionary : rowData.name, - exportFilename: rowData.name - }) - this.getDictionaryElements(rowData.name); - } - - render() { - return ( - <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} > - <Modal.Header closeButton> - <Modal.Title>Manage Dictionaries</Modal.Title> - </Modal.Header> - <Modal.Body> - {this.state.currentSelectedDictionary === null ? - <MaterialTable - title={"Dictionary List"} - data={this.state.dictionaries} - columns={this.state.dictColumns} - icons={this.state.tableIcons} - onRowClick={this.handleDictionaryRowClick} - options={{ - headerStyle: rowHeaderStyle, - }} - editable={!this.readOnly ? - { - onRowAdd: this.addDictionaryRow, - onRowUpdate: this.updateDictionaryRow, - onRowDelete: this.deleteDictionaryRow - } : undefined } - /> : null - } - {this.state.currentSelectedDictionary !== null ? - <MaterialTable - title={'Dictionary Elements List for ' + (subDictFlag ? 'Sub-Dictionary "' : '"') + this.state.currentSelectedDictionary + '"'} - data={this.state.dictionaryElements} - columns={this.state.dictElementColumns} - icons={this.state.tableIcons} - options={{ - exportAllData: true, - exportButton: true, - exportFileName: this.state.exportFilename, - headerStyle:{backgroundColor:'white', fontSize: '15pt', text: 'bold', border: '1px solid black'} - }} - components={{ - Toolbar: props => ( - <Row> - <Col sm="11"> - <MTableToolbarStyled {...props} /> - </Col> - <ColPullLeftStyled sm="1"> - <Tooltip title="Import" placement = "bottom"> - <IconButton aria-label="import" disabled={this.readOnly} onClick={() => this.fileUpload.click()}> - <VerticalAlignTopIcon /> - </IconButton> - </Tooltip> - <input type="file" ref={(fileUpload) => {this.fileUpload = fileUpload;}} - style={{ visibility: 'hidden', width: '1px' }} onChange={this.fileSelectedHandler} /> - </ColPullLeftStyled> - </Row> - ) - }} - editable={!this.readOnly ? - { - onRowAdd: this.addDictionaryElementRow, - onRowUpdate: this.updateDictionaryElementRow, - onRowDelete: this.deleteDictionaryElementRow - } : undefined - } - /> : null - } - {this.state.currentSelectedDictionary !== null ? <button onClick={this.clickHandler} style={{marginTop: '25px'}}>Go Back to Dictionaries List</button>:""} - </Modal.Body> - <Modal.Footer> - <Button variant="secondary" type="null" onClick={this.handleClose}>Close</Button> - </Modal.Footer> - </ModalStyled> - ); - } + constructor(props, context) { + super(props, context); + this.addDictionaryElementRow = this.addDictionaryElementRow.bind(this); + this.addDictionaryRow = this.addDictionaryRow.bind(this); + this.addReplaceDictionaryRequest = this.addReplaceDictionaryRequest.bind(this); + this.clickHandler = this.clickHandler.bind(this); + this.deleteDictionaryElementRow = this.deleteDictionaryElementRow.bind(this); + this.deleteDictionaryRequest = this.deleteDictionaryRequest.bind(this); + this.deleteDictionaryRow = this.deleteDictionaryRow.bind(this); + this.fileSelectedHandler = this.fileSelectedHandler.bind(this); + this.getDictionaries = this.getDictionaries.bind(this); + this.getDictionaryElements = this.getDictionaryElements.bind(this); + this.handleClose = this.handleClose.bind(this); + this.handleDictionaryRowClick = this.handleDictionaryRowClick.bind(this); + this.importCsvData = this.importCsvData.bind(this); + this.updateDictionaryElementRow = this.updateDictionaryElementRow.bind(this); + this.updateDictionaryElementsRequest = this.updateDictionaryElementsRequest.bind(this); + this.updateDictionaryRow = this.updateDictionaryRow.bind(this); + this.readOnly = props.readOnly !== undefined ? props.readOnly : false; + this.state = { + show: true, + currentSelectedDictionary: null, + exportFilename: '', + content: null, + dictionaryElements: [], + tableIcons: { + Add: forwardRef((props, ref) => <AddBox { ...props } ref={ ref }/>), + Delete: forwardRef((props, ref) => <DeleteOutline { ...props } ref={ ref }/>), + DetailPanel: forwardRef((props, ref) => <ChevronRight { ...props } ref={ ref }/>), + Edit: forwardRef((props, ref) => <Edit { ...props } ref={ ref }/>), + Check: forwardRef((props, ref) => <Check { ...props } ref={ ref }/>), + Clear: forwardRef((props, ref) => <Clear { ...props } ref={ ref }/>), + Export: forwardRef((props, ref) => <VerticalAlignBottomIcon { ...props } ref={ ref }/>), + Filter: forwardRef((props, ref) => <FilterList { ...props } ref={ ref }/>), + FirstPage: forwardRef((props, ref) => <FirstPage { ...props } ref={ ref }/>), + LastPage: forwardRef((props, ref) => <LastPage { ...props } ref={ ref }/>), + NextPage: forwardRef((props, ref) => <ChevronRight { ...props } ref={ ref }/>), + PreviousPage: forwardRef((props, ref) => <ChevronLeft { ...props } ref={ ref }/>), + ResetSearch: forwardRef((props, ref) => <Clear { ...props } ref={ ref }/>), + Search: forwardRef((props, ref) => <Search { ...props } ref={ ref }/>), + SortArrow: forwardRef((props, ref) => <ArrowUpward { ...props } ref={ ref }/>), + ThirdStateCheck: forwardRef((props, ref) => <Remove { ...props } ref={ ref }/>), + ViewColumn: forwardRef((props, ref) => <ViewColumn { ...props } ref={ ref }/>) + }, + dictColumns: [ + { + title: "Dictionary Name", field: "name", editable: 'onAdd', + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Sub Dictionary ?", field: "secondLevelDictionary", lookup: { 0: 'No', 1: 'Yes' }, + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Dictionary Type", field: "subDictionaryType", lookup: { string: 'string', number: 'number' }, + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Updated By", field: "updatedBy", editable: 'never', + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Last Updated Date", field: "updatedDate", editable: 'never', + cellStyle: cellStyle, + headerStyle: headerStyle + } + ], + dictElementColumns: [ + { + title: "Element Short Name", field: "shortName", editable: 'onAdd', + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Element Name", field: "name", + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Element Description", field: "description", + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Element Type", field: "type", + editComponent: props => ( + <div> + <SelectSubDictType value={ props.value } onChange={ props.onChange }/> + </div> + ), + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Sub-Dictionary", field: "subDictionary", + editComponent: props => ( + <div> + <SubDict value={ props.value } onChange={ props.onChange }/> + </div> + ), + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Updated By", field: "updatedBy", editable: 'never', + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Updated Date", field: "updatedDate", editable: 'never', + cellStyle: cellStyle, + headerStyle: headerStyle + } + ] + } + } + + componentDidMount() { + this.getDictionaries(); + } + + getDictionaries() { + TemplateMenuService.getDictionary().then(arrayOfdictionaries => { + this.setState({ dictionaries: arrayOfdictionaries, currentSelectedDictionary: null }) + // global variable setting used functional components in this file + dictList = arrayOfdictionaries; + }).catch(() => { + console.error('Failed to retrieve dictionaries'); + this.setState({ dictionaries: [], currentSelectedDictionary: null }) + }); + } + + getDictionaryElements(dictionaryName) { + TemplateMenuService.getDictionaryElements(dictionaryName).then(dictionaryElements => { + this.setState({ dictionaryElements: dictionaryElements.dictionaryElements }); + this.setState({ currentSelectDictionary: dictionaryName }); + }).catch(() => console.error('Failed to retrieve dictionary elements')) + } + + clickHandler(rowData) { + this.getDictionaries(); + } + + handleClose() { + this.setState({ show: false }); + this.props.history.push('/'); + } + + addReplaceDictionaryRequest(dictionaryEntry) { + TemplateMenuService.insDictionary(dictionaryEntry) + .then(resp => { + this.getDictionaries(); + }) + .catch(() => console.error('Failed to insert new dictionary elements')); + } + + updateDictionaryElementsRequest(dictElements) { + let reqData = { "name": this.state.currentSelectedDictionary, 'dictionaryElements': dictElements }; + TemplateMenuService.insDictionaryElements(reqData) + .then(resp => { + this.getDictionaryElements(this.state.currentSelectedDictionary) + }) + .catch(() => console.error('Failed to update dictionary elements')); + } + + deleteDictionaryRequest(dictionaryName) { + TemplateMenuService.deleteDictionary(dictionaryName) + .then(resp => { + this.getDictionaries(); + }) + .catch(() => console.error('Failed to delete dictionary')); + } + + deleteDictionaryElementRequest(dictionaryName, elemenetShortName) { + TemplateMenuService.deleteDictionaryElements({ 'name': dictionaryName, 'shortName': elemenetShortName }) + .then(resp => { + this.getDictionaryElements(dictionaryName); + }) + .catch(() => console.error('Failed to delete dictionary elements')); + } + + fileSelectedHandler = (event) => { + + if (event.target.files[0].type === 'text/csv' || event.target.files[0].type === 'application/vnd.ms-excel') { + if (event.target.files && event.target.files[0]) { + const reader = new FileReader(); + reader.onload = (e) => { + let errorMessages = this.importCsvData(reader.result); + if (errorMessages !== '') { + alert(errorMessages); + } + } + reader.readAsText(event.target.files[0]); + } + } else { + alert('Please upload .csv extention files only.'); + } + } + + importCsvData(rawCsvData) { + + const jsonKeyNames = ['shortName', 'name', 'description', 'type', 'subDictionary']; + const userHeaderNames = ['Element Short Name', 'Element Name', 'Element Description', 'Element Type', 'Sub-Dictionary']; + const validTypes = ['string', 'number', 'datetime', 'json', 'map']; + + let mandatory; + + if (subDictFlag) { + mandatory = [true, true, true, false, false]; + } else { + mandatory = [true, true, true, true, false]; + } + + let result = CsvToJson(rawCsvData, ',', '||||', userHeaderNames, jsonKeyNames, mandatory); + + let errorMessages = result.errorMessages; + let jsonObjArray = result.jsonObjArray; + + let validTypesErrorMesg = ''; + + for (let i = 0; i < validTypes.length; ++i) { + if (i === 0) { + validTypesErrorMesg = validTypes[i]; + } else { + validTypesErrorMesg += ',' + validTypes[i]; + } + } + + if (errorMessages !== '') { + return errorMessages; + } + + // Perform further checks on data that is now in JSON form + let subDictionaries = []; + + // NOTE: dictList is a global variable maintained faithfully + // by the getDictionaries() method outside this import + // functionality. + let item; + for (item in dictList) { + if (dictList[item].secondLevelDictionary === 1) { + subDictionaries.push(dictList[item].name); + } + } + ; + + // Check for valid Sub-Dictionary and Element Type values + subDictionaries = subDictionaries.toString(); + let row = 2; + let dictElem; + for (dictElem of jsonObjArray) { + let itemKey; + for (itemKey in dictElem) { + let value = dictElem[itemKey].trim(); + let keyIndex = jsonKeyNames.indexOf(itemKey); + if (itemKey === 'shortName' && /[^a-zA-Z0-9-_.]/.test(value)) { + errorMessages += '\n' + userHeaderNames[keyIndex] + + ' at row #' + row + + ' can only contain alphanumeric characters and periods, hyphens or underscores'; + } + if (itemKey === 'type' && validTypes.indexOf(value) < 0) { + errorMessages += '\nInvalid value of "' + value + '" for "' + userHeaderNames[keyIndex] + '" at row #' + row; + errorMessages += '\nValid types are: ' + validTypesErrorMesg; + } + if (value !== "" && itemKey === 'subDictionary' && subDictionaries.indexOf(value) < 0) { + errorMessages += '\nInvalid Sub-Dictionary value of "' + value + '" at row #' + row; + } + } + ++row; + } + if (errorMessages === '') { + // We made it through all the checks. Send it to back end + this.updateDictionaryElementsRequest(jsonObjArray); + } + + return errorMessages; + } + + addDictionaryRow(newData) { + let validData = true; + return new Promise((resolve, reject) => { + setTimeout(() => { + if (/[^a-zA-Z0-9-_.]/.test(newData.name)) { + validData = false; + alert('Please enter alphanumeric input. Only allowed special characters are:(period, hyphen, underscore)'); + reject(); + } + for (let i = 0; i < this.state.dictionaries.length; i++) { + if (this.state.dictionaries[i].name === newData.name) { + validData = false; + alert(newData.name + ' dictionary name already exists') + reject(); + } + } + if (validData) { + this.addReplaceDictionaryRequest(newData); + } + resolve(); + }, 1000); + }); + } + + + updateDictionaryRow(newData, oldData) { + let validData = true; + return new Promise((resolve, reject) => { + setTimeout(() => { + if (/[^a-zA-Z0-9-_.]/.test(newData.name)) { + validData = false; + alert('Please enter alphanumberic input. Only allowed special characters are:(period, hyphen, underscore)'); + reject(); + } + if (validData) { + this.addReplaceDictionaryRequest(newData); + } + resolve(); + }, 1000); + }); + } + + deleteDictionaryRow(oldData) { + return new Promise((resolve, reject) => { + setTimeout(() => { + this.deleteDictionaryRequest(oldData.name); + resolve(); + }, 1000); + }); + } + + addDictionaryElementRow(newData) { + return new Promise((resolve, reject) => { + setTimeout(() => { + let dictionaryElements = this.state.dictionaryElements; + let errorMessages = ''; + for (let i = 0; i < this.state.dictionaryElements.length; i++) { + if (this.state.dictionaryElements[i].shortName === newData.shortName) { + alert('Short Name "' + newData.shortName + '" already exists'); + reject(""); + } + } + // MaterialTable returns no property at all if the user has not touched a + // new column, so we want to add the property with an emptry string + // for several cases if that is the case to simplify other checks. + if (newData.description === undefined) { + newData.description = ""; + } + if (newData.subDictionary === undefined) { + newData.subDictionary = null; + } + if (newData.type === undefined) { + newData.type = ""; + } + if (!newData.shortName && /[^a-zA-Z0-9-_.]/.test(newData.shortName)) { + errorMessages += '\nShort Name is limited to alphanumeric characters and also period, hyphen, and underscore'; + } + if (!newData.shortName) { + errorMessages += '\nShort Name must be specified'; + } + if (!newData.name) { + errorMessages += '\nElement Name must be specified'; + } + if (!newData.type && !subDictFlag) { + errorMessages += '\nElement Type must be specified'; + } + if (errorMessages === '') { + dictionaryElements.push(newData); + this.updateDictionaryElementsRequest([newData]); + resolve(); + } else { + alert(errorMessages); + reject(""); + } + }, 1000); + }); + } + + updateDictionaryElementRow(newData, oldData) { + return new Promise((resolve, reject) => { + setTimeout(() => { + let dictionaryElements = this.state.dictionaryElements; + let validData = true; + if (!newData.type) { + validData = false; + alert('Element Type cannot be null'); + reject(); + } + if (validData) { + const index = dictionaryElements.indexOf(oldData); + dictionaryElements[index] = newData; + this.updateDictionaryElementsRequest([newData]); + } + resolve(); + }, 1000); + }); + } + + + deleteDictionaryElementRow(oldData) { + return new Promise((resolve) => { + setTimeout(() => { + this.deleteDictionaryElementRequest(this.state.currentSelectedDictionary, oldData.shortName); + resolve(); + }, 1000); + }); + } + + handleDictionaryRowClick(event, rowData) { + subDictFlag = rowData.secondLevelDictionary === 1 ? true : false; + this.setState({ + currentSelectedDictionary: rowData.name, + exportFilename: rowData.name + }) + this.getDictionaryElements(rowData.name); + } + + render() { + return ( + <ModalStyled size="xl" show={ this.state.show } onHide={ this.handleClose } backdrop="static" keyboard={ false }> + <Modal.Header closeButton> + <Modal.Title>Manage Dictionaries</Modal.Title> + </Modal.Header> + <Modal.Body> + { this.state.currentSelectedDictionary === null ? + <MaterialTable + title={ "Dictionary List" } + data={ this.state.dictionaries } + columns={ this.state.dictColumns } + icons={ this.state.tableIcons } + onRowClick={ this.handleDictionaryRowClick } + options={ { + headerStyle: rowHeaderStyle, + } } + editable={ !this.readOnly ? + { + onRowAdd: this.addDictionaryRow, + onRowUpdate: this.updateDictionaryRow, + onRowDelete: this.deleteDictionaryRow + } : undefined } + /> : null + } + { this.state.currentSelectedDictionary !== null ? + <MaterialTable + title={ 'Dictionary Elements List for ' + (subDictFlag ? 'Sub-Dictionary "' : '"') + this.state.currentSelectedDictionary + '"' } + data={ this.state.dictionaryElements } + columns={ this.state.dictElementColumns } + icons={ this.state.tableIcons } + options={ { + exportAllData: true, + exportButton: true, + exportFileName: this.state.exportFilename, + headerStyle: { backgroundColor: 'white', fontSize: '15pt', text: 'bold', border: '1px solid black' } + } } + components={ { + Toolbar: props => ( + <Row> + <Col sm="11"> + <MTableToolbarStyled { ...props } /> + </Col> + <ColPullLeftStyled sm="1"> + <Tooltip title="Import" placement="bottom"> + <IconButton aria-label="import" disabled={ this.readOnly } onClick={ () => this.fileUpload.click() }> + <VerticalAlignTopIcon/> + </IconButton> + </Tooltip> + <input type="file" ref={ (fileUpload) => { + this.fileUpload = fileUpload; + } } + style={ { visibility: 'hidden', width: '1px' } } onChange={ this.fileSelectedHandler }/> + </ColPullLeftStyled> + </Row> + ) + } } + editable={ !this.readOnly ? + { + onRowAdd: this.addDictionaryElementRow, + onRowUpdate: this.updateDictionaryElementRow, + onRowDelete: this.deleteDictionaryElementRow + } : undefined + } + /> : null + } + { this.state.currentSelectedDictionary !== null ? <button onClick={ this.clickHandler } style={ { marginTop: '25px' } }>Go Back to Dictionaries List</button> : "" } + </Modal.Body> + <Modal.Footer> + <Button variant="secondary" type="null" onClick={ this.handleClose }>Close</Button> + </Modal.Footer> + </ModalStyled> + ); + } } diff --git a/runtime/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js b/runtime/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js index a4c1335d8..2552d7a3d 100644 --- a/runtime/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js +++ b/runtime/ui-react/src/components/dialogs/ManageDictionaries/ManageDictionaries.test.js @@ -28,435 +28,438 @@ import ManageDictionaries from './ManageDictionaries'; import TemplateMenuService from '../../../api/TemplateService' const TestDictionaryElements = { - name: "test", - secondLevelDictionary: 0, - subDictionaryType: "", - dictionaryElements: [ - { - shortName: "alertType", - name: "Alert Type", - description: "Type of Alert", - type: "string", - subDictionary: "", - createdDate: "2020-06-12T13:58:51.443931Z", - updatedDate: "2020-06-13T16:27:57.084870Z", - updatedBy: "admin", - createdBy: "admin" - } - ] + name: "test", + secondLevelDictionary: 0, + subDictionaryType: "", + dictionaryElements: [ + { + shortName: "alertType", + name: "Alert Type", + description: "Type of Alert", + type: "string", + subDictionary: "", + createdDate: "2020-06-12T13:58:51.443931Z", + updatedDate: "2020-06-13T16:27:57.084870Z", + updatedBy: "admin", + createdBy: "admin" + } + ] }; const TestDictionaries = -[ - { - name: "test", - secondLevelDictionary: 0, - subDictionaryType: "string", - dictionaryElements: [ TestDictionaryElements ], - createdDate: "2020-06-14T21:00:33.231166Z", - updatedDate: "2020-06-14T21:00:33.231166Z", - updatedBy: "admin", - createdBy: "admin" - }, - { - name: "testSub1", - secondLevelDictionary: 1, - subDictionaryType: "string", - dictionaryElements: [ - { - shortName: "subElem", - name: "Sub Element", - description: "Sub Element Description", - type: "string", - createdDate: "2020-06-14T21:04:44.402287Z", - updatedDate: "2020-06-14T21:04:44.402287Z", - updatedBy: "admin", - createdBy: "admin" - } - ], - createdDate: "2020-06-14T21:01:16.390250Z", - updatedDate: "2020-06-14T21:01:16.390250Z", - updatedBy: "admin", - createdBy: "admin" - } -]; + [ + { + name: "test", + secondLevelDictionary: 0, + subDictionaryType: "string", + dictionaryElements: [TestDictionaryElements], + createdDate: "2020-06-14T21:00:33.231166Z", + updatedDate: "2020-06-14T21:00:33.231166Z", + updatedBy: "admin", + createdBy: "admin" + }, + { + name: "testSub1", + secondLevelDictionary: 1, + subDictionaryType: "string", + dictionaryElements: [ + { + shortName: "subElem", + name: "Sub Element", + description: "Sub Element Description", + type: "string", + createdDate: "2020-06-14T21:04:44.402287Z", + updatedDate: "2020-06-14T21:04:44.402287Z", + updatedBy: "admin", + createdBy: "admin" + } + ], + createdDate: "2020-06-14T21:01:16.390250Z", + updatedDate: "2020-06-14T21:01:16.390250Z", + updatedBy: "admin", + createdBy: "admin" + } + ]; const historyMock = { push: jest.fn() }; let errorMessage = ''; -window.alert = jest.fn().mockImplementation((mesg) => { errorMessage = mesg ; return }); +window.alert = jest.fn().mockImplementation((mesg) => { + errorMessage = mesg; + return +}); TemplateMenuService.getDictionary = jest.fn().mockImplementation(() => { - return Promise.resolve(TestDictionaries); + return Promise.resolve(TestDictionaries); }); TemplateMenuService.insDictionary = jest.fn().mockImplementation(() => { - return Promise.resolve({ ok: true, status: 200 }); + return Promise.resolve({ ok: true, status: 200 }); }); TemplateMenuService.deleteDictionary = jest.fn().mockImplementation(() => { - return Promise.resolve("200"); + return Promise.resolve("200"); }); TemplateMenuService.getDictionaryElements = jest.fn().mockImplementation(() => { - return Promise.resolve(TestDictionaryElements); + return Promise.resolve(TestDictionaryElements); }); TemplateMenuService.deleteDictionaryElements = jest.fn().mockImplementation(() => { - return Promise.resolve("200"); + return Promise.resolve("200"); }); TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => { - return Promise.resolve("200"); + return Promise.resolve("200"); }); describe('Verify ManageDictionaries', () => { - beforeEach(() => { - fetch.resetMocks(); - }); + beforeEach(() => { + fetch.resetMocks(); + }); + + it('Test API Successful', () => { + fetch.mockImplementationOnce(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + return Promise.resolve({ + "name": "vtest", + "secondLevelDictionary": 1, + "subDictionaryType": "string", + "updatedBy": "test", + "updatedDate": "05-07-2019 19:09:42" + }); + } + }); + }); + const component = shallow(<ManageDictionaries/>); + expect(component).toMatchSnapshot(); + }); + + it('Test API Exception', () => { + fetch.mockImplementationOnce(() => { + return Promise.resolve({ + ok: false, + status: 500, + json: () => { + return Promise.resolve({ + "name": "vtest", + "secondLevelDictionary": 1, + "subDictionaryType": "string", + "updatedBy": "test", + "updatedDate": "05-07-2019 19:09:42" + }); + } + }); + }); + const component = shallow(<ManageDictionaries/>); + }); + + it('Test Table icons', () => { + + const component = mount(<ManageDictionaries/>); + expect(component.find('[className="MuiSelect-icon MuiTablePagination-selectIcon"]')).toBeTruthy(); + }); + + test('Test add/replace and delete dictionary requests', async () => { + + const component = shallow(<ManageDictionaries history={ historyMock }/>) + const instance = component.instance(); + + const flushPromises = () => new Promise(setImmediate); + + instance.addReplaceDictionaryRequest({ name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" }); + instance.deleteDictionaryRequest("test"); + + await flushPromises(); + + expect(component.state('currentSelectedDictionary')).toEqual(null); + expect(component.state('dictionaries')).toEqual(TestDictionaries); + }); - it('Test API Successful', () => { - fetch.mockImplementationOnce(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => { - return Promise.resolve({ - "name": "vtest", - "secondLevelDictionary": 1, - "subDictionaryType": "string", - "updatedBy": "test", - "updatedDate": "05-07-2019 19:09:42" - }); - } - }); - }); - const component = shallow(<ManageDictionaries />); - expect(component).toMatchSnapshot(); - }); + test('Test update dictionary row', async () => { - it('Test API Exception', () => { - fetch.mockImplementationOnce(() => { - return Promise.resolve({ - ok: false, - status: 500, - json: () => { - return Promise.resolve({ - "name": "vtest", - "secondLevelDictionary": 1, - "subDictionaryType": "string", - "updatedBy": "test", - "updatedDate": "05-07-2019 19:09:42" - }); - } - }); - }); - const component = shallow(<ManageDictionaries />); - }); + const component = shallow(<ManageDictionaries history={ historyMock }/>) + const instance = component.instance(); + const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" }; - it('Test Table icons', () => { + await expect(instance.updateDictionaryRow(rowData, rowData)).resolves.toEqual(undefined); - const component = mount(<ManageDictionaries />); - expect(component.find('[className="MuiSelect-icon MuiTablePagination-selectIcon"]')).toBeTruthy(); - }); + }, 2000); - test('Test add/replace and delete dictionary requests', async () => { + test('Test add dictionary row', async () => { - const component = shallow(<ManageDictionaries history={historyMock}/>) - const instance = component.instance(); + const addReplaceRequest = jest.spyOn(ManageDictionaries.prototype, 'addReplaceDictionaryRequest'); + const component = shallow(<ManageDictionaries/>) + const instance = component.instance(); + const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" }; - const flushPromises = () => new Promise(setImmediate); + await instance.addDictionaryRow(rowData); + expect(addReplaceRequest).toHaveBeenCalledWith(rowData); - instance.addReplaceDictionaryRequest({name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string"}); - instance.deleteDictionaryRequest("test"); + }, 2000); - await flushPromises(); + test('Test add dictionary row with errors name already exists', async () => { - expect(component.state('currentSelectedDictionary')).toEqual(null); - expect(component.state('dictionaries')).toEqual(TestDictionaries); - }); + const component = shallow(<ManageDictionaries/>) + const instance = component.instance(); + let rowData = { name: "test", secondLevelDictionary: 0, subDictionaryType: "" }; - test('Test update dictionary row', async () => { + await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined); - const component = shallow(<ManageDictionaries history={historyMock}/>) - const instance = component.instance(); - const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" }; + }, 2000); - await expect(instance.updateDictionaryRow(rowData, rowData)).resolves.toEqual(undefined); + test('Test add dictionary row with errors illegal chars in name', async () => { - }, 2000); + const component = shallow(<ManageDictionaries/>) + const instance = component.instance(); + let rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" }; - test('Test add dictionary row', async () => { + await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined); - const addReplaceRequest = jest.spyOn(ManageDictionaries.prototype,'addReplaceDictionaryRequest'); - const component = shallow(<ManageDictionaries />) - const instance = component.instance(); - const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" }; + }, 2000); - await instance.addDictionaryRow(rowData); - expect(addReplaceRequest).toHaveBeenCalledWith(rowData); + test('Test update dictionary row with errors illegal chars in name', async () => { - }, 2000); + const component = shallow(<ManageDictionaries/>) + const instance = component.instance(); + let rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" }; - test('Test add dictionary row with errors name already exists', async () => { + await expect(instance.updateDictionaryRow(rowData)).rejects.toEqual(undefined); + }); - const component = shallow(<ManageDictionaries />) - const instance = component.instance(); - let rowData = { name: "test", secondLevelDictionary: 0, subDictionaryType: "" }; - await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined); + test('Test add dictionary row with errors (illegal chars)', async () => { - }, 2000); + const addReplaceRequest = jest.spyOn(ManageDictionaries.prototype, 'addReplaceDictionaryRequest'); + const component = shallow(<ManageDictionaries/>) + const instance = component.instance(); + let rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" }; - test('Test add dictionary row with errors illegal chars in name', async () => { + await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined); - const component = shallow(<ManageDictionaries />) - const instance = component.instance(); - let rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" }; + }, 2000); - await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined); - }, 2000); + test('Test delete dictionary row', async () => { - test('Test update dictionary row with errors illegal chars in name', async () => { + const deleteRequest = jest.spyOn(ManageDictionaries.prototype, 'deleteDictionaryRequest'); + const component = shallow(<ManageDictionaries/>) + const instance = component.instance(); + const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" }; - const component = shallow(<ManageDictionaries />) - const instance = component.instance(); - let rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" }; + await instance.deleteDictionaryRow(rowData); + expect(deleteRequest).toHaveBeenCalledWith("newdict"); - await expect(instance.updateDictionaryRow(rowData)).rejects.toEqual(undefined); - }); + }, 2000); + test('Test handle select dictionary row click', async () => { - test('Test add dictionary row with errors (illegal chars)', async () => { + const component = shallow(<ManageDictionaries/>) + const instance = component.instance(); + const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" }; - const addReplaceRequest = jest.spyOn(ManageDictionaries.prototype,'addReplaceDictionaryRequest'); - const component = shallow(<ManageDictionaries />) - const instance = component.instance(); - let rowData = { name: "test@@", secondLevelDictionary: 0, subDictionaryType: "" }; + instance.handleDictionaryRowClick("event", rowData); + expect(component.state('currentSelectedDictionary')).toEqual("newdict"); + }, 2000); - await expect(instance.addDictionaryRow(rowData)).rejects.toEqual(undefined); + test('Test dictionary element row add, update, delete', async () => { - }, 2000); + const rowData = { + createdBy: "admin", + createdDate: "2020-06-15T13:59:20.467381Z", + description: "Description", + name: "Some Elem", + shortName: "someElem", + type: "string", + updatedBy: "admin", + updatedDate: "2020-06-15T13:59:20.467381Z" + }; + const component = shallow(<ManageDictionaries/>) + const instance = component.instance(); - test('Test delete dictionary row', async () => { + const badRowData = { + description: "Description", + name: "Some Elem", + shortName: "someElem", + type: "string" + }; - const deleteRequest = jest.spyOn(ManageDictionaries.prototype,'deleteDictionaryRequest'); - const component = shallow(<ManageDictionaries />) - const instance = component.instance(); - const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" }; + await instance.clickHandler(); + await instance.getDictionaryElements("test"); - await instance.deleteDictionaryRow(rowData); - expect(deleteRequest).toHaveBeenCalledWith("newdict"); + await expect(instance.addDictionaryElementRow(rowData)).resolves.toEqual(undefined); + await expect(instance.updateDictionaryElementRow(rowData, rowData)).resolves.toEqual(undefined); + await expect(instance.deleteDictionaryElementRow(rowData)).resolves.toEqual(undefined); + }); - }, 2000); + test('Test dictionary element row add with errors', async () => { - test('Test handle select dictionary row click', async () => { + const badRowData = { + description: "", + name: "", + shortName: "some#Elem", + type: "" + }; - const component = shallow(<ManageDictionaries />) - const instance = component.instance(); - const rowData = { name: "newdict", secondLevelDictionary: 0, subDictionaryType: "string" }; + const component = shallow(<ManageDictionaries/>) + const instance = component.instance(); - instance.handleDictionaryRowClick("event", rowData); - expect(component.state('currentSelectedDictionary')).toEqual("newdict"); - }, 2000); + await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual(""); + }); - test('Test dictionary element row add, update, delete', async () => { + test('Test dictionary element update with error illegal name', async () => { - const rowData = { - createdBy: "admin", - createdDate: "2020-06-15T13:59:20.467381Z", - description: "Description", - name: "Some Elem", - shortName: "someElem", - type: "string", - updatedBy: "admin", - updatedDate: "2020-06-15T13:59:20.467381Z" - }; + const badRowData = { + description: "", + name: "test@@", + shortName: "some#Elem", + type: "" + }; - const component = shallow(<ManageDictionaries/>) - const instance = component.instance(); + const component = shallow(<ManageDictionaries/>) + const instance = component.instance(); - const badRowData = { - description: "Description", - name: "Some Elem", - shortName: "someElem", - type: "string" - }; + await expect(instance.updateDictionaryElementRow(badRowData)).rejects.toEqual(undefined); + }); - await instance.clickHandler(); - await instance.getDictionaryElements("test"); + test('Test dictionary element addition with duplicate name error', async () => { - await expect(instance.addDictionaryElementRow(rowData)).resolves.toEqual(undefined); - await expect(instance.updateDictionaryElementRow(rowData, rowData)).resolves.toEqual(undefined); - await expect(instance.deleteDictionaryElementRow(rowData)).resolves.toEqual(undefined); - }); + const badRowData = { + description: "description", + name: "Alert Type", + shortName: "alertType", + type: "string" + }; - test('Test dictionary element row add with errors', async () => { + const component = shallow(<ManageDictionaries/>) + const instance = component.instance(); - const badRowData = { - description: "", - name: "", - shortName: "some#Elem", - type: "" - }; + component.setState({ currentSelectedDictionary: 'test' }); - const component = shallow(<ManageDictionaries/>) - const instance = component.instance(); + await instance.getDictionaryElements(); + await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual(""); + }); - await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual(""); - }); + test('Test dictionary element addition with empty name error', async () => { - test('Test dictionary element update with error illegal name', async () => { + const badRowData = { + description: "description", + name: "Alert Type", + shortName: "", + type: "string" + }; - const badRowData = { - description: "", - name: "test@@", - shortName: "some#Elem", - type: "" - }; + const component = shallow(<ManageDictionaries/>) + const instance = component.instance(); - const component = shallow(<ManageDictionaries/>) - const instance = component.instance(); + component.setState({ currentSelectedDictionary: 'test' }); - await expect(instance.updateDictionaryElementRow(badRowData)).rejects.toEqual(undefined); - }); + await instance.getDictionaryElements(); + await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual(""); + }); - test('Test dictionary element addition with duplicate name error', async () => { - const badRowData = { - description: "description", - name: "Alert Type", - shortName: "alertType", - type: "string" - }; + it('Test Import CSV Sunny Day', async () => { - const component = shallow(<ManageDictionaries/>) - const instance = component.instance(); + TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => { + return Promise.resolve({ ok: true, status: 200 }); + }); - component.setState({ currentSelectedDictionary: 'test' }); + let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsvData += '"alertType","Alert Type","Alert Type Description","string","","admin","2020-06-11T13:56:14.927437Z"'; - await instance.getDictionaryElements(); - await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual(""); - }); + let expectedResult = [ + { + description: "Alert Type Description", + name: "Alert Type", + shortName: "alertType", + subDictionary: "", + type: "string" + } + ]; + + const updateDictionaryElementsRequest = jest.spyOn(ManageDictionaries.prototype, 'updateDictionaryElementsRequest'); + + const component = shallow(<ManageDictionaries/>) + const instance = component.instance(); + + await expect(instance.importCsvData(rawCsvData)).toEqual(''); + expect(updateDictionaryElementsRequest).toHaveBeenCalledWith(expectedResult); + }); + + it('Test Import CSV Mandatory Field Check Errors', () => { + + let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsvData += '"","","","","","",""'; - test('Test dictionary element addition with empty name error', async () => { + // The empty values for all the fields in row 1 of the rawCsvData will trigger a bunch of errors. + // Getting Enzyme to properly match them with embedded newlines turned out to be impossible + // and maybe not desirable anyway; so our test for "success" here is simply that the + // routine returns a non-empty error string. - const badRowData = { - description: "description", - name: "Alert Type", - shortName: "", - type: "string" - }; - - const component = shallow(<ManageDictionaries/>) - const instance = component.instance(); - - component.setState({ currentSelectedDictionary: 'test' }); - - await instance.getDictionaryElements(); - await expect(instance.addDictionaryElementRow(badRowData)).rejects.toEqual(""); - }); - - - it('Test Import CSV Sunny Day', async () => { - - TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => { - return Promise.resolve({ ok: true, status: 200 }); - }); - - let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; - rawCsvData += '"alertType","Alert Type","Alert Type Description","string","","admin","2020-06-11T13:56:14.927437Z"'; - - let expectedResult = [ - { - description: "Alert Type Description", - name: "Alert Type", - shortName: "alertType", - subDictionary: "", - type: "string" - } - ]; - - const updateDictionaryElementsRequest = jest.spyOn(ManageDictionaries.prototype,'updateDictionaryElementsRequest'); - - const component = shallow(<ManageDictionaries />) - const instance = component.instance(); - - await expect(instance.importCsvData(rawCsvData)).toEqual(''); - expect(updateDictionaryElementsRequest).toHaveBeenCalledWith(expectedResult); - }); - - it('Test Import CSV Mandatory Field Check Errors', () => { - - let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; - rawCsvData += '"","","","","","",""'; - - // The empty values for all the fields in row 1 of the rawCsvData will trigger a bunch of errors. - // Getting Enzyme to properly match them with embedded newlines turned out to be impossible - // and maybe not desirable anyway; so our test for "success" here is simply that the - // routine returns a non-empty error string. - - const component = shallow(<ManageDictionaries />) - const instance = component.instance(); - expect(instance.importCsvData(rawCsvData)).not.toEqual(''); - }); - - it('Test Import CSV Errors in Row Data', async () => { - - TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => { - return Promise.resolve({ ok: true, status: 200 }); - }); - - let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; - rawCsvData += '"alert@Type","Alert Type","Alert Type Description","strin","subby","admin","2020-06-11T13:56:14.927437Z"'; - - let expectedResult = [ - { - description: "Alert Type Description", - name: "Alert Type", - shortName: "alertType", - subDictionary: "", - type: "string" - } - ]; - - const updateDictionaryElementsRequest = jest.spyOn(ManageDictionaries.prototype,'updateDictionaryElementsRequest'); - - const component = shallow(<ManageDictionaries />) - const instance = component.instance(); - - await expect(instance.importCsvData(rawCsvData)).not.toEqual(''); - }); - - - it('Test handleClose', () => { - fetch.mockImplementationOnce(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => { - return Promise.resolve({ - "name": "vtest", - "secondLevelDictionary": 1, - "subDictionaryType": "string", - "updatedBy": "test", - "updatedDate": "05-07-2019 19:09:42" - }); - } - }); - }); - const handleClose = jest.spyOn(ManageDictionaries.prototype,'handleClose'); - const component = shallow(<ManageDictionaries history={historyMock} />) - component.find('[variant="secondary"]').prop('onClick')(); - expect(handleClose).toHaveBeenCalledTimes(1); - expect(component.state('show')).toEqual(false); - expect(historyMock.push.mock.calls[0]).toEqual([ '/']); - handleClose.mockClear(); - }); + const component = shallow(<ManageDictionaries/>) + const instance = component.instance(); + expect(instance.importCsvData(rawCsvData)).not.toEqual(''); + }); + + it('Test Import CSV Errors in Row Data', async () => { + + TemplateMenuService.insDictionaryElements = jest.fn().mockImplementation(() => { + return Promise.resolve({ ok: true, status: 200 }); + }); + + let rawCsvData = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsvData += '"alert@Type","Alert Type","Alert Type Description","strin","subby","admin","2020-06-11T13:56:14.927437Z"'; + + let expectedResult = [ + { + description: "Alert Type Description", + name: "Alert Type", + shortName: "alertType", + subDictionary: "", + type: "string" + } + ]; + + const updateDictionaryElementsRequest = jest.spyOn(ManageDictionaries.prototype, 'updateDictionaryElementsRequest'); + + const component = shallow(<ManageDictionaries/>) + const instance = component.instance(); + + await expect(instance.importCsvData(rawCsvData)).not.toEqual(''); + }); + + + it('Test handleClose', () => { + fetch.mockImplementationOnce(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + return Promise.resolve({ + "name": "vtest", + "secondLevelDictionary": 1, + "subDictionaryType": "string", + "updatedBy": "test", + "updatedDate": "05-07-2019 19:09:42" + }); + } + }); + }); + const handleClose = jest.spyOn(ManageDictionaries.prototype, 'handleClose'); + const component = shallow(<ManageDictionaries history={ historyMock }/>) + component.find('[variant="secondary"]').prop('onClick')(); + expect(handleClose).toHaveBeenCalledTimes(1); + expect(component.state('show')).toEqual(false); + expect(historyMock.push.mock.calls[0]).toEqual(['/']); + handleClose.mockClear(); + }); }); diff --git a/runtime/ui-react/src/components/dialogs/PerformActions.js b/runtime/ui-react/src/components/dialogs/PerformActions.js index f6001e21f..ba7c99d7f 100644 --- a/runtime/ui-react/src/components/dialogs/PerformActions.js +++ b/runtime/ui-react/src/components/dialogs/PerformActions.js @@ -25,71 +25,71 @@ import LoopActionService from '../../api/LoopActionService'; export default class PerformActions extends React.Component { - state = { - loopName: this.props.loopCache.getLoopName(), - loopAction: this.props.loopAction - }; + state = { + loopName: this.props.loopCache.getLoopName(), + loopAction: this.props.loopAction + }; - constructor(props, context) { - super(props, context); - this.refreshStatus = this.refreshStatus.bind(this); - } + constructor(props, context) { + super(props, context); + this.refreshStatus = this.refreshStatus.bind(this); + } - componentWillReceiveProps(newProps) { - this.setState({ - loopName: newProps.loopCache.getLoopName(), - loopAction: newProps.loopAction - }); - } + componentWillReceiveProps(newProps) { + this.setState({ + loopName: newProps.loopCache.getLoopName(), + loopAction: newProps.loopAction + }); + } - componentDidMount() { - const action = this.state.loopAction; - const loopName = this.state.loopName; + componentDidMount() { + const action = this.state.loopAction; + const loopName = this.state.loopName; - if (action === 'delete') { - if (window.confirm('You are about to remove Control Loop Model "' + loopName + - '". Select OK to continue with deletion or Cancel to keep the model.') === false) { - return; - } - } + if (action === 'delete') { + if (window.confirm('You are about to remove Control Loop Model "' + loopName + + '". Select OK to continue with deletion or Cancel to keep the model.') === false) { + return; + } + } - this.props.setBusyLoading(); // Alert top level to start block user clicks + this.props.setBusyLoading(); // Alert top level to start block user clicks - LoopActionService.performAction(loopName, action) - .then(pars => { - this.props.showSucAlert("Action " + action + " successfully performed"); - if (action === 'delete') { - this.props.updateLoopFunction(null); - this.props.history.push('/'); - } else { - // refresh status and update loop logs - this.refreshStatus(loopName); - } - }) - .catch(error => { - this.props.showFailAlert("Action " + action + " failed"); - // refresh status and update loop logs - this.refreshStatus(loopName); - }) - .finally(() => this.props.clearBusyLoading()); - } + LoopActionService.performAction(loopName, action) + .then(pars => { + this.props.showSucAlert("Action " + action + " successfully performed"); + if (action === 'delete') { + this.props.updateLoopFunction(null); + this.props.history.push('/'); + } else { + // refresh status and update loop logs + this.refreshStatus(loopName); + } + }) + .catch(error => { + this.props.showFailAlert("Action " + action + " failed"); + // refresh status and update loop logs + this.refreshStatus(loopName); + }) + .finally(() => this.props.clearBusyLoading()); + } - refreshStatus(loopName) { + refreshStatus(loopName) { - this.props.setBusyLoading(); + this.props.setBusyLoading(); - LoopActionService.refreshStatus(loopName) - .then(data => { - this.props.updateLoopFunction(data); - this.props.history.push('/'); - }) - .catch(error => { - this.props.history.push('/'); - }) - .finally(() => this.props.clearBusyLoading()); - } + LoopActionService.refreshStatus(loopName) + .then(data => { + this.props.updateLoopFunction(data); + this.props.history.push('/'); + }) + .catch(error => { + this.props.history.push('/'); + }) + .finally(() => this.props.clearBusyLoading()); + } - render() { - return null; - } + render() { + return null; + } } diff --git a/runtime/ui-react/src/components/dialogs/PerformActions.test.js b/runtime/ui-react/src/components/dialogs/PerformActions.test.js index c91c2f675..74c914584 100644 --- a/runtime/ui-react/src/components/dialogs/PerformActions.test.js +++ b/runtime/ui-react/src/components/dialogs/PerformActions.test.js @@ -28,63 +28,68 @@ import LoopActionService from '../../api/LoopActionService'; describe('Verify PerformActions', () => { - const loopCache = new LoopCache({ - "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca" - }); + const loopCache = new LoopCache({ + "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca" + }); - it('Test the render method action failed', async () => { - const flushPromises = () => new Promise(setImmediate); - const historyMock = { push: jest.fn() }; - const updateLoopFunction = jest.fn(); - const showSucAlert = jest.fn(); - const showFailAlert = jest.fn(); - const setBusyLoading = jest.fn(); - const clearBusyLoading = jest.fn(); - - LoopActionService.refreshStatus = jest.fn().mockImplementation(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => {} - }); - }); - const component = shallow(<PerformActions loopCache={loopCache} - loopAction="submit" history={historyMock} updateLoopFunction={updateLoopFunction} showSucAlert={showSucAlert} showFailAlert={showFailAlert} setBusyLoading={setBusyLoading} clearBusyLoading={clearBusyLoading}/>) - await flushPromises(); - component.update(); + it('Test the render method action failed', async () => { + const flushPromises = () => new Promise(setImmediate); + const historyMock = { push: jest.fn() }; + const updateLoopFunction = jest.fn(); + const showSucAlert = jest.fn(); + const showFailAlert = jest.fn(); + const setBusyLoading = jest.fn(); + const clearBusyLoading = jest.fn(); - expect(historyMock.push.mock.calls[0]).toEqual([ '/']); - }); + LoopActionService.refreshStatus = jest.fn().mockImplementation(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + } + }); + }); + const component = shallow(<PerformActions loopCache={ loopCache } + loopAction="submit" history={ historyMock } updateLoopFunction={ updateLoopFunction } showSucAlert={ showSucAlert } showFailAlert={ showFailAlert } + setBusyLoading={ setBusyLoading } clearBusyLoading={ clearBusyLoading }/>) + await flushPromises(); + component.update(); - it('Test the render method action successful', async () => { - const flushPromises = () => new Promise(setImmediate); - const historyMock = { push: jest.fn() }; - const updateLoopFunction = jest.fn(); - const showSucAlert = jest.fn(); - const showFailAlert = jest.fn(); - const setBusyLoading = jest.fn(); - const clearBusyLoading = jest.fn(); + expect(historyMock.push.mock.calls[0]).toEqual(['/']); + }); - LoopActionService.performAction = jest.fn().mockImplementation(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => {} - }); - }); - LoopActionService.refreshStatus = jest.fn().mockImplementation(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => {} - }); - }); - const component = shallow(<PerformActions loopCache={loopCache} - loopAction="submit" history={historyMock} updateLoopFunction={updateLoopFunction} showSucAlert={showSucAlert} showFailAlert={showFailAlert} setBusyLoading={setBusyLoading} clearBusyLoading={clearBusyLoading}/>) - await flushPromises(); - component.update(); + it('Test the render method action successful', async () => { + const flushPromises = () => new Promise(setImmediate); + const historyMock = { push: jest.fn() }; + const updateLoopFunction = jest.fn(); + const showSucAlert = jest.fn(); + const showFailAlert = jest.fn(); + const setBusyLoading = jest.fn(); + const clearBusyLoading = jest.fn(); - expect(historyMock.push.mock.calls[0]).toEqual([ '/']); - }); + LoopActionService.performAction = jest.fn().mockImplementation(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + } + }); + }); + LoopActionService.refreshStatus = jest.fn().mockImplementation(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + } + }); + }); + const component = shallow(<PerformActions loopCache={ loopCache } + loopAction="submit" history={ historyMock } updateLoopFunction={ updateLoopFunction } showSucAlert={ showSucAlert } showFailAlert={ showFailAlert } + setBusyLoading={ setBusyLoading } clearBusyLoading={ clearBusyLoading }/>) + await flushPromises(); + component.update(); + + expect(historyMock.push.mock.calls[0]).toEqual(['/']); + }); }); diff --git a/runtime/ui-react/src/components/dialogs/Policy/PoliciesTreeViewer.js b/runtime/ui-react/src/components/dialogs/Policy/PoliciesTreeViewer.js index 9c2f102b4..5bca4e6e1 100644 --- a/runtime/ui-react/src/components/dialogs/Policy/PoliciesTreeViewer.js +++ b/runtime/ui-react/src/components/dialogs/Policy/PoliciesTreeViewer.js @@ -31,79 +31,79 @@ import DescriptionIcon from '@material-ui/icons/Description'; export default class PoliciesTreeViewer extends React.Component { - separator = "."; + separator = "."; - nodesList = new Map(); + nodesList = new Map(); - constructor(props, context) { - super(props, context); - this.createPoliciesTree = this.createPoliciesTree.bind(this); - this.handleTreeItemClick = this.handleTreeItemClick.bind(this); - this.buildNameWithParent = this.buildNameWithParent.bind(this); + constructor(props, context) { + super(props, context); + this.createPoliciesTree = this.createPoliciesTree.bind(this); + this.handleTreeItemClick = this.handleTreeItemClick.bind(this); + this.buildNameWithParent = this.buildNameWithParent.bind(this); - } + } - state = { - policiesTreeData: this.createPoliciesTree(this.props.policiesData), - } + state = { + policiesTreeData: this.createPoliciesTree(this.props.policiesData), + } - componentDidUpdate(prevProps) { - if (prevProps.policiesData !== this.props.policiesData) { - this.setState({policiesTreeData: this.createPoliciesTree(this.props.policiesData)}) - } + componentDidUpdate(prevProps) { + if (prevProps.policiesData !== this.props.policiesData) { + this.setState({ policiesTreeData: this.createPoliciesTree(this.props.policiesData) }) } + } - createPoliciesTree(policiesArray) { - // put my policies array in a Json - let nodeId = 1; - let root = {id:nodeId, policyCount:0, name:"ROOT", children:[], parent: undefined}; - this.nodesList.set(nodeId++, root); - - policiesArray.forEach(policy => { - let currentTreeNode = root; - policy[this.props.valueForTreeCreation].split(this.separator).forEach((policyNamePart, index, policyNamePartsArray) => { - let node = currentTreeNode["children"].find(element => element.name === policyNamePart); - if (typeof(node) === "undefined") { - node = {id:nodeId, policyCount:0, children:[], name:policyNamePart, parent:currentTreeNode}; - this.nodesList.set(nodeId++, node); - currentTreeNode["children"].push(node); - } - if ((index+1) === policyNamePartsArray.length) { - ++currentTreeNode["policyCount"]; - } - currentTreeNode = node; - }) - }) - return root; - } + createPoliciesTree(policiesArray) { + // put my policies array in a Json + let nodeId = 1; + let root = { id: nodeId, policyCount: 0, name: "ROOT", children: [], parent: undefined }; + this.nodesList.set(nodeId++, root); - buildNameWithParent(node) { - let nameToBuild = node.name; - if (node.parent !== undefined) { - nameToBuild = this.buildNameWithParent(node.parent) + this.separator + node.name; + policiesArray.forEach(policy => { + let currentTreeNode = root; + policy[this.props.valueForTreeCreation].split(this.separator).forEach((policyNamePart, index, policyNamePartsArray) => { + let node = currentTreeNode["children"].find(element => element.name === policyNamePart); + if (typeof (node) === "undefined") { + node = { id: nodeId, policyCount: 0, children: [], name: policyNamePart, parent: currentTreeNode }; + this.nodesList.set(nodeId++, node); + currentTreeNode["children"].push(node); } - return nameToBuild; - } + if ((index + 1) === policyNamePartsArray.length) { + ++currentTreeNode["policyCount"]; + } + currentTreeNode = node; + }) + }) + return root; + } - handleTreeItemClick(event, value) { - let fullName = this.buildNameWithParent(this.nodesList.get(value[0])).substring(5); - this.props.policiesFilterFunction(fullName); + buildNameWithParent(node) { + let nameToBuild = node.name; + if (node.parent !== undefined) { + nameToBuild = this.buildNameWithParent(node.parent) + this.separator + node.name; } + return nameToBuild; + } - renderTreeItems(nodes) { - return (<TreeItem key={nodes.id} nodeId={nodes.id} label={nodes.name + "("+ nodes.policyCount + ")"} onNodeSelect={this.handleTreeItemClick}> - { - Array.isArray(nodes.children) ? nodes.children.map((node) => this.renderTreeItems(node)) : null - } - </TreeItem>); - }; + handleTreeItemClick(event, value) { + let fullName = this.buildNameWithParent(this.nodesList.get(value[0])).substring(5); + this.props.policiesFilterFunction(fullName); + } - render() { - return ( - <TreeView defaultExpanded={['root']} defaultCollapseIcon={<FolderOpenIcon />} - defaultExpandIcon={<FolderIcon />} defaultEndIcon={<DescriptionIcon />} onNodeSelect={this.handleTreeItemClick} multiSelect> - {this.renderTreeItems(this.state.policiesTreeData)} - </TreeView> - ); - } -}
\ No newline at end of file + renderTreeItems(nodes) { + return (<TreeItem key={ nodes.id } nodeId={ nodes.id } label={ nodes.name + "(" + nodes.policyCount + ")" } onNodeSelect={ this.handleTreeItemClick }> + { + Array.isArray(nodes.children) ? nodes.children.map((node) => this.renderTreeItems(node)) : null + } + </TreeItem>); + }; + + render() { + return ( + <TreeView defaultExpanded={ ['root'] } defaultCollapseIcon={ <FolderOpenIcon/> } + defaultExpandIcon={ <FolderIcon/> } defaultEndIcon={ <DescriptionIcon/> } onNodeSelect={ this.handleTreeItemClick } multiSelect> + { this.renderTreeItems(this.state.policiesTreeData) } + </TreeView> + ); + } +} diff --git a/runtime/ui-react/src/components/dialogs/Policy/PolicyEditor.test.js b/runtime/ui-react/src/components/dialogs/Policy/PolicyEditor.test.js index 0b734430a..111f2c6bf 100644 --- a/runtime/ui-react/src/components/dialogs/Policy/PolicyEditor.test.js +++ b/runtime/ui-react/src/components/dialogs/Policy/PolicyEditor.test.js @@ -26,46 +26,47 @@ import { shallow, mount } from 'enzyme'; import PolicyToscaService from '../../../api/PolicyToscaService'; describe('Verify PolicyEditor', () => { - const fs = require('fs'); + const fs = require('fs'); - let toscaJson = fs.readFileSync('src/components/dialogs/Policy/toscaData.test.json', {encoding:'utf8', flag:'r'}) + let toscaJson = fs.readFileSync('src/components/dialogs/Policy/toscaData.test.json', { encoding: 'utf8', flag: 'r' }) - const policyProperties = { - "tca.policy": { - "domain": "measurementsForVfScaling", - "metricsPerEventName": [ - { - "policyScope": "DCAE", - "thresholds": [ - { - "version": "1.0.2", - "severity": "MAJOR", - "thresholdValue": 200, - "closedLoopEventStatus": "ONSET", - "closedLoopControlName": "LOOP_test", - "direction": "LESS_OR_EQUAL", - "fieldPath": "$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedTotalPacketsDelta" - } - ], - "eventName": "vLoadBalancer", - "policyVersion": "v0.0.1", - "controlLoopSchemaType": "VM", - "policyName": "DCAE.Config_tca-hi-lo" - } - ] - } - }; + const policyProperties = { + "tca.policy": { + "domain": "measurementsForVfScaling", + "metricsPerEventName": [ + { + "policyScope": "DCAE", + "thresholds": [ + { + "version": "1.0.2", + "severity": "MAJOR", + "thresholdValue": 200, + "closedLoopEventStatus": "ONSET", + "closedLoopControlName": "LOOP_test", + "direction": "LESS_OR_EQUAL", + "fieldPath": "$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedTotalPacketsDelta" + } + ], + "eventName": "vLoadBalancer", + "policyVersion": "v0.0.1", + "controlLoopSchemaType": "VM", + "policyName": "DCAE.Config_tca-hi-lo" + } + ] + } + }; - it('Test the render method',async () => { - PolicyToscaService.getToscaPolicyModel = jest.fn().mockImplementation(() => { - return Promise.resolve(toscaJson); - }); - - const component = mount(<PolicyEditor policyModelType="onap.policies.monitoring.tcagen2" policyModelTypeVersion="1.0.0" - policyName="org.onap.new" policyVersion="1.0.0" policyProperties={policyProperties} - policiesTableUpdateFunction={() => {}} />); - await PolicyToscaService.getToscaPolicyModel(); - expect(component).toMatchSnapshot(); + it('Test the render method', async () => { + PolicyToscaService.getToscaPolicyModel = jest.fn().mockImplementation(() => { + return Promise.resolve(toscaJson); }); -});
\ No newline at end of file + + const component = mount(<PolicyEditor policyModelType="onap.policies.monitoring.tcagen2" policyModelTypeVersion="1.0.0" + policyName="org.onap.new" policyVersion="1.0.0" policyProperties={ policyProperties } + policiesTableUpdateFunction={ () => { + } }/>); + await PolicyToscaService.getToscaPolicyModel(); + expect(component).toMatchSnapshot(); + }); +}); diff --git a/runtime/ui-react/src/components/dialogs/Policy/PolicyToscaFileSelector.js b/runtime/ui-react/src/components/dialogs/Policy/PolicyToscaFileSelector.js index 9cd3d4172..8093b7e4e 100644 --- a/runtime/ui-react/src/components/dialogs/Policy/PolicyToscaFileSelector.js +++ b/runtime/ui-react/src/components/dialogs/Policy/PolicyToscaFileSelector.js @@ -27,102 +27,107 @@ import Row from 'react-bootstrap/Row'; import Col from 'react-bootstrap/Col'; import styled from 'styled-components'; import Alert from 'react-bootstrap/Alert'; -import { Input, InputLabel, Button , SvgIcon} from "@material-ui/core"; +import { Input, InputLabel, Button, SvgIcon } from "@material-ui/core"; import PublishIcon from '@material-ui/icons/Publish'; import PolicyService from '../../../api/PolicyService'; const ModalStyled = styled(Modal)` - background-color: transparent; + background-color: transparent; ` const StyledMessagesDiv = styled.div` - overflow: auto; - max-height: 300px; + overflow: auto; + max-height: 300px; ` export default class PolicyToscaFileSelector extends React.Component { - state = { - show: this.props.show, - alertMessages: [], - } - constructor(props, context) { - super(props, context); - this.handleClose = this.handleClose.bind(this); - this.onFileChange = this.onFileChange.bind(this); - } + state = { + show: this.props.show, + alertMessages: [], + } - componentDidUpdate(prevProps) { - if (this.props.show !== this.state.show) { - this.setState({show: this.props.show}); - } - } + constructor(props, context) { + super(props, context); + this.handleClose = this.handleClose.bind(this); + this.onFileChange = this.onFileChange.bind(this); + } - handleClose() { - this.props.disableFunction(); - this.setState({alertMessages:[]}); + componentDidUpdate(prevProps) { + if (this.props.show !== this.state.show) { + this.setState({ show: this.props.show }); } + } - onFileChange(target) { - this.setState({alertMessages:[]}); - target.currentTarget.files.forEach(file => { - const fileReader = new FileReader(); - fileReader.readAsDataURL(file); - fileReader.onload = (content) => { - PolicyService.sendNewPolicyModel(atob(content.target.result.split(",")[1])).then(respModelCreate => { - if (typeof(respModelCreate) === "undefined") { - //it indicates a failure - this.setState(state => { - return { - alertMessages: [...state.alertMessages,(<Alert variant="danger"><Alert.Heading>{file.name}</Alert.Heading><p>Policy Tosca Model Creation Failure</p><hr/><p>Type: {file.type}</p><p>Size: {file.size}</p></Alert>)] - }; - }); - } else { - this.props.toscaTableUpdateFunction(); - this.setState(state => { - return { - alertMessages: [...state.alertMessages,(<Alert variant="success"><Alert.Heading>{file.name}</Alert.Heading><p>Policy Tosca Model Created Successfully</p><hr/><p>Type: {file.type}</p><p>Size: {file.size}</p></Alert>)] - }; - }); - } - }); - }; + handleClose() { + this.props.disableFunction(); + this.setState({ alertMessages: [] }); + } + + onFileChange(target) { + this.setState({ alertMessages: [] }); + target.currentTarget.files.forEach(file => { + const fileReader = new FileReader(); + fileReader.readAsDataURL(file); + fileReader.onload = (content) => { + PolicyService.sendNewPolicyModel(atob(content.target.result.split(",")[1])).then(respModelCreate => { + if (typeof (respModelCreate) === "undefined") { + //it indicates a failure + this.setState(state => { + return { + alertMessages: [...state.alertMessages, (<Alert variant="danger"><Alert.Heading>{ file.name }</Alert.Heading><p>Policy Tosca Model Creation Failure</p> + <hr/> + <p>Type: { file.type }</p><p>Size: { file.size }</p></Alert>)] + }; + }); + } else { + this.props.toscaTableUpdateFunction(); + this.setState(state => { + return { + alertMessages: [...state.alertMessages, (<Alert variant="success"><Alert.Heading>{ file.name }</Alert.Heading><p>Policy Tosca Model Created Successfully</p> + <hr/> + <p>Type: { file.type }</p><p>Size: { file.size }</p></Alert>)] + }; + }); + } }); + }; + }); - } + } - render() { - return ( - <ModalStyled size="lg" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false} > - <Modal.Header closeButton> - <Modal.Title>Create New Policy Tosca Model</Modal.Title> - </Modal.Header> - <Modal.Body> - <Form.Group as={Row} controlId="formPlaintextEmail"> - <Col sm="10"> - <input type="file" multiple accept=".yaml,.yml" id="fileUploadButton" style={{ display: 'none' }} onChange={this.onFileChange} /> - <label htmlFor={'fileUploadButton'}> - <Button color="primary" variant="contained" component="span" - startIcon={ - <SvgIcon fontSize="small"> - <PublishIcon /> - </SvgIcon> - }> - Upload Files - </Button> - <p>(Only YAML files are supported)</p> - </label> - <StyledMessagesDiv> - {this.state.alertMessages} - </StyledMessagesDiv> - </Col> - </Form.Group> - </Modal.Body> - <Modal.Footer> - <Button variant="secondary" onClick={this.handleClose}>Close</Button> - </Modal.Footer> - </ModalStyled> - ); - } -}
\ No newline at end of file + render() { + return ( + <ModalStyled size="lg" show={ this.state.show } onHide={ this.handleClose } backdrop="static" keyboard={ false }> + <Modal.Header closeButton> + <Modal.Title>Create New Policy Tosca Model</Modal.Title> + </Modal.Header> + <Modal.Body> + <Form.Group as={ Row } controlId="formPlaintextEmail"> + <Col sm="10"> + <input type="file" multiple accept=".yaml,.yml" id="fileUploadButton" style={ { display: 'none' } } onChange={ this.onFileChange }/> + <label htmlFor={ 'fileUploadButton' }> + <Button color="primary" variant="contained" component="span" + startIcon={ + <SvgIcon fontSize="small"> + <PublishIcon/> + </SvgIcon> + }> + Upload Files + </Button> + <p>(Only YAML files are supported)</p> + </label> + <StyledMessagesDiv> + { this.state.alertMessages } + </StyledMessagesDiv> + </Col> + </Form.Group> + </Modal.Body> + <Modal.Footer> + <Button variant="secondary" onClick={ this.handleClose }>Close</Button> + </Modal.Footer> + </ModalStyled> + ); + } +} diff --git a/runtime/ui-react/src/components/dialogs/Policy/ToscaViewer.js b/runtime/ui-react/src/components/dialogs/Policy/ToscaViewer.js index fa83aa245..fc69a637a 100644 --- a/runtime/ui-react/src/components/dialogs/Policy/ToscaViewer.js +++ b/runtime/ui-react/src/components/dialogs/Policy/ToscaViewer.js @@ -27,40 +27,40 @@ import styled from 'styled-components'; import Button from 'react-bootstrap/Button'; const ToscaDiv = styled.div` - background-color: ${props => props.theme.toscaTextareaBackgroundColor}; - text-align: justify; - font-size: ${props => props.theme.toscaTextareaFontSize}; - width: 100%; - height: 30%; + background-color: ${ props => props.theme.toscaTextareaBackgroundColor }; + text-align: justify; + font-size: ${ props => props.theme.toscaTextareaFontSize }; + width: 100%; + height: 30%; ` export default class ToscaViewer extends React.Component { - state = { - toscaData: this.props.toscaData, - yamlPolicyTosca: this.getToscaModelYamlFor(this.props.toscaData), - } + state = { + toscaData: this.props.toscaData, + yamlPolicyTosca: this.getToscaModelYamlFor(this.props.toscaData), + } - constructor(props, context) { - super(props, context); - this.getToscaModelYamlFor = this.getToscaModelYamlFor.bind(this); - } + constructor(props, context) { + super(props, context); + this.getToscaModelYamlFor = this.getToscaModelYamlFor.bind(this); + } - getToscaModelYamlFor(toscaData) { - PolicyToscaService.getToscaPolicyModelYaml(toscaData["policyModelType"], toscaData["version"]).then(respYamlPolicyTosca => { - this.setState({ - yamlPolicyTosca: respYamlPolicyTosca, - }) - }); - } + getToscaModelYamlFor(toscaData) { + PolicyToscaService.getToscaPolicyModelYaml(toscaData["policyModelType"], toscaData["version"]).then(respYamlPolicyTosca => { + this.setState({ + yamlPolicyTosca: respYamlPolicyTosca, + }) + }); + } - render() { - return ( - <ToscaDiv> - <pre>{this.state.yamlPolicyTosca}</pre> - <Button variant="secondary" title="Create a new policy version from the defined parameters" - onClick={this.handleCreateNewVersion}>Create New Version</Button> - </ToscaDiv> - ); - } + render() { + return ( + <ToscaDiv> + <pre>{ this.state.yamlPolicyTosca }</pre> + <Button variant="secondary" title="Create a new policy version from the defined parameters" + onClick={ this.handleCreateNewVersion }>Create New Version</Button> + </ToscaDiv> + ); + } } diff --git a/runtime/ui-react/src/components/dialogs/Policy/ToscaViewer.test.js b/runtime/ui-react/src/components/dialogs/Policy/ToscaViewer.test.js index cc8c59a03..5b5976046 100644 --- a/runtime/ui-react/src/components/dialogs/Policy/ToscaViewer.test.js +++ b/runtime/ui-react/src/components/dialogs/Policy/ToscaViewer.test.js @@ -26,29 +26,29 @@ import { shallow, mount } from 'enzyme'; import PolicyToscaService from '../../../api/PolicyToscaService'; describe('Verify ToscaViewer', () => { - const fs = require('fs'); + const fs = require('fs'); - let toscaYaml = fs.readFileSync('src/components/dialogs/Policy/toscaData.test.yaml', {encoding:'utf8', flag:'r'}) + let toscaYaml = fs.readFileSync('src/components/dialogs/Policy/toscaData.test.yaml', { encoding: 'utf8', flag: 'r' }) - const toscaData = { - "policyModelType": "onap.policies.controlloop.Guard", - "version": "1.0.0", - "policyAcronym": "Guard", - "createdDate": "2021-04-09T02:29:31.407356Z", - "updatedDate": "2021-04-09T02:29:31.407356Z", - "updatedBy": "Not found", - "createdBy": "Not found", - "tableData": { - "id": 0 - } - }; + const toscaData = { + "policyModelType": "onap.policies.controlloop.Guard", + "version": "1.0.0", + "policyAcronym": "Guard", + "createdDate": "2021-04-09T02:29:31.407356Z", + "updatedDate": "2021-04-09T02:29:31.407356Z", + "updatedBy": "Not found", + "createdBy": "Not found", + "tableData": { + "id": 0 + } + }; - it('Test the render method',async () => { - PolicyToscaService.getToscaPolicyModelYaml = jest.fn().mockImplementation(() => { - return Promise.resolve(toscaYaml); - }); - const component = shallow(<ToscaViewer toscaData={toscaData}/>); - await PolicyToscaService.getToscaPolicyModelYaml(); - expect(component).toMatchSnapshot(); - }); + it('Test the render method', async () => { + PolicyToscaService.getToscaPolicyModelYaml = jest.fn().mockImplementation(() => { + return Promise.resolve(toscaYaml); + }); + const component = shallow(<ToscaViewer toscaData={ toscaData }/>); + await PolicyToscaService.getToscaPolicyModelYaml(); + expect(component).toMatchSnapshot(); + }); }); diff --git a/runtime/ui-react/src/components/dialogs/Policy/ViewAllPolicies.js b/runtime/ui-react/src/components/dialogs/Policy/ViewAllPolicies.js index 04965352b..f571bc16d 100644 --- a/runtime/ui-react/src/components/dialogs/Policy/ViewAllPolicies.js +++ b/runtime/ui-react/src/components/dialogs/Policy/ViewAllPolicies.js @@ -59,415 +59,417 @@ import PoliciesTreeViewer from './PoliciesTreeViewer'; import PolicyToscaFileSelector from './PolicyToscaFileSelector'; const DivWhiteSpaceStyled = styled.div` - white-space: pre; + white-space: pre; ` const ModalStyled = styled(Modal)` - @media (min-width: 800px) { - .modal-xl { - max-width: 96%; - } + @media (min-width: 800px) { + .modal-xl { + max-width: 96%; } - background-color: transparent; + } + background-color: transparent; ` const DetailedRow = styled.div` - margin: 0 auto; - background-color: ${props => props.theme.policyEditorBackgroundColor}; - font-size: ${props => props.theme.policyEditorFontSize}; - width: 97%; - margin-left: auto; - margin-right: auto; - margin-top: 20px; + margin: 0 auto; + background-color: ${ props => props.theme.policyEditorBackgroundColor }; + font-size: ${ props => props.theme.policyEditorFontSize }; + width: 97%; + margin-left: auto; + margin-right: auto; + margin-top: 20px; ` const PoliciesTreeViewerDiv = styled.div` - width: 20%; - float: left; - left: 0; - overflow: auto; + width: 20%; + float: left; + left: 0; + overflow: auto; ` const MaterialTableDiv = styled.div` - float: right; - width: 80%; - left: 20%; + float: right; + width: 80%; + left: 20%; ` const standardCellStyle = { backgroundColor: '#039be5', color: '#FFF', border: '1px solid black' }; const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' }; -const rowHeaderStyle = {backgroundColor:'#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black'}; +const rowHeaderStyle = { backgroundColor: '#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black' }; export default class ViewAllPolicies extends React.Component { state = { - show: true, - policiesListData: [], - policiesListDataFiltered: [], - toscaModelsListData: [], - toscaModelsListDataFiltered: [], - jsonEditorForPolicy: new Map(), - showSuccessAlert: false, - showFailAlert: false, - showFileSelector: false, - policyColumnsDefinition: [ - { - title: "Policy Name", field: "name", - cellStyle: standardCellStyle, - headerStyle: headerStyle - }, - { - title: "Policy Version", field: "version", - cellStyle: standardCellStyle, - headerStyle: headerStyle, - }, - { - title: "Policy Type", field: "type", - cellStyle: standardCellStyle, - headerStyle: headerStyle - }, - { - title: "Policy Type Version", field: "type_version", - cellStyle: standardCellStyle, - headerStyle: headerStyle - }, - { - title: "Deployable in PDP Group", field: "supportedPdpGroupsString", - cellStyle: standardCellStyle, - headerStyle: headerStyle - }, - { - title: "Deployed in PDP Group", field: "pdpGroupInfoString", - cellStyle: standardCellStyle, - headerStyle: headerStyle - } - ], - toscaColumnsDefinition: [ - { - title: "Policy Model Type", field: "policyModelType", - cellStyle: standardCellStyle, - headerStyle: headerStyle - }, - { - title: "Policy Acronym", field: "policyAcronym", - cellStyle: standardCellStyle, - headerStyle: headerStyle - }, - { - title: "Version", field: "version", - cellStyle: standardCellStyle, - headerStyle: headerStyle - }, - { - title: "Uploaded By", field: "updatedBy", - cellStyle: standardCellStyle, - headerStyle: headerStyle - }, - { - title: "Uploaded Date", field: "updatedDate", editable: 'never', - cellStyle: standardCellStyle, - headerStyle: headerStyle - } - ], - tableIcons: { - Add: forwardRef((props, ref) => <AddBox {...props} ref={ref} />), - Check: forwardRef((props, ref) => <Check {...props} ref={ref} />), - Clear: forwardRef((props, ref) => <Clear {...props} ref={ref} />), - Delete: forwardRef((props, ref) => <DeleteRoundedIcon {...props} ref={ref} />), - DetailPanel: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />), - Edit: forwardRef((props, ref) => <Edit {...props} ref={ref} />), - Export: forwardRef((props, ref) => <SaveAlt {...props} ref={ref} />), - Filter: forwardRef((props, ref) => <FilterList {...props} ref={ref} />), - FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />), - LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />), - NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />), - PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />), - ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />), - Search: forwardRef((props, ref) => <Search {...props} ref={ref} />), - SortArrow: forwardRef((props, ref) => <ArrowDownward {...props} ref={ref} />), - ThirdStateCheck: forwardRef((props, ref) => <Remove {...props} ref={ref} />), - ViewColumn: forwardRef((props, ref) => <ViewColumn {...props} ref={ref} />) - } - }; - - constructor(props, context) { - super(props, context); - this.handleClose = this.handleClose.bind(this); - this.handleDeletePolicy = this.handleDeletePolicy.bind(this); - this.disableAlert = this.disableAlert.bind(this); - this.getAllPolicies = this.getAllPolicies.bind(this); - this.getAllToscaModels = this.getAllToscaModels.bind(this); - this.generateAdditionalPolicyColumns = this.generateAdditionalPolicyColumns.bind(this); - this.filterPolicies = this.filterPolicies.bind(this); - this.filterTosca = this.filterTosca.bind(this); - this.showFileSelector = this.showFileSelector.bind(this); - this.disableFileSelector = this.disableFileSelector.bind(this); - this.getAllPolicies(); - this.getAllToscaModels(); + show: true, + policiesListData: [], + policiesListDataFiltered: [], + toscaModelsListData: [], + toscaModelsListDataFiltered: [], + jsonEditorForPolicy: new Map(), + showSuccessAlert: false, + showFailAlert: false, + showFileSelector: false, + policyColumnsDefinition: [ + { + title: "Policy Name", field: "name", + cellStyle: standardCellStyle, + headerStyle: headerStyle + }, + { + title: "Policy Version", field: "version", + cellStyle: standardCellStyle, + headerStyle: headerStyle, + }, + { + title: "Policy Type", field: "type", + cellStyle: standardCellStyle, + headerStyle: headerStyle + }, + { + title: "Policy Type Version", field: "type_version", + cellStyle: standardCellStyle, + headerStyle: headerStyle + }, + { + title: "Deployable in PDP Group", field: "supportedPdpGroupsString", + cellStyle: standardCellStyle, + headerStyle: headerStyle + }, + { + title: "Deployed in PDP Group", field: "pdpGroupInfoString", + cellStyle: standardCellStyle, + headerStyle: headerStyle + } + ], + toscaColumnsDefinition: [ + { + title: "Policy Model Type", field: "policyModelType", + cellStyle: standardCellStyle, + headerStyle: headerStyle + }, + { + title: "Policy Acronym", field: "policyAcronym", + cellStyle: standardCellStyle, + headerStyle: headerStyle + }, + { + title: "Version", field: "version", + cellStyle: standardCellStyle, + headerStyle: headerStyle + }, + { + title: "Uploaded By", field: "updatedBy", + cellStyle: standardCellStyle, + headerStyle: headerStyle + }, + { + title: "Uploaded Date", field: "updatedDate", editable: 'never', + cellStyle: standardCellStyle, + headerStyle: headerStyle + } + ], + tableIcons: { + Add: forwardRef((props, ref) => <AddBox { ...props } ref={ ref }/>), + Check: forwardRef((props, ref) => <Check { ...props } ref={ ref }/>), + Clear: forwardRef((props, ref) => <Clear { ...props } ref={ ref }/>), + Delete: forwardRef((props, ref) => <DeleteRoundedIcon { ...props } ref={ ref }/>), + DetailPanel: forwardRef((props, ref) => <ChevronRight { ...props } ref={ ref }/>), + Edit: forwardRef((props, ref) => <Edit { ...props } ref={ ref }/>), + Export: forwardRef((props, ref) => <SaveAlt { ...props } ref={ ref }/>), + Filter: forwardRef((props, ref) => <FilterList { ...props } ref={ ref }/>), + FirstPage: forwardRef((props, ref) => <FirstPage { ...props } ref={ ref }/>), + LastPage: forwardRef((props, ref) => <LastPage { ...props } ref={ ref }/>), + NextPage: forwardRef((props, ref) => <ChevronRight { ...props } ref={ ref }/>), + PreviousPage: forwardRef((props, ref) => <ChevronLeft { ...props } ref={ ref }/>), + ResetSearch: forwardRef((props, ref) => <Clear { ...props } ref={ ref }/>), + Search: forwardRef((props, ref) => <Search { ...props } ref={ ref }/>), + SortArrow: forwardRef((props, ref) => <ArrowDownward { ...props } ref={ ref }/>), + ThirdStateCheck: forwardRef((props, ref) => <Remove { ...props } ref={ ref }/>), + ViewColumn: forwardRef((props, ref) => <ViewColumn { ...props } ref={ ref }/>) } + }; - generateAdditionalPolicyColumns(policiesData) { - policiesData.forEach(policy => { - let supportedPdpGroupsString = ""; - if (typeof policy.supportedPdpGroups !== "undefined") { - for (const pdpGroup of policy["supportedPdpGroups"]) { - for (const pdpSubGroup of Object.values(pdpGroup)[0]) { - supportedPdpGroupsString += (Object.keys(pdpGroup)[0] + "/" + pdpSubGroup + "\r\n"); - } - } - policy["supportedPdpGroupsString"] = supportedPdpGroupsString; - } + constructor(props, context) { + super(props, context); + this.handleClose = this.handleClose.bind(this); + this.handleDeletePolicy = this.handleDeletePolicy.bind(this); + this.disableAlert = this.disableAlert.bind(this); + this.getAllPolicies = this.getAllPolicies.bind(this); + this.getAllToscaModels = this.getAllToscaModels.bind(this); + this.generateAdditionalPolicyColumns = this.generateAdditionalPolicyColumns.bind(this); + this.filterPolicies = this.filterPolicies.bind(this); + this.filterTosca = this.filterTosca.bind(this); + this.showFileSelector = this.showFileSelector.bind(this); + this.disableFileSelector = this.disableFileSelector.bind(this); + this.getAllPolicies(); + this.getAllToscaModels(); + } - let infoPdpGroup = ""; - if (typeof policy.pdpGroupInfo !== "undefined") { - policy["pdpGroupInfo"].forEach(pdpGroupElem => { - let groupName = Object.keys(pdpGroupElem)[0]; - pdpGroupElem[groupName]["pdpSubgroups"].forEach(pdpSubGroupElem => { - infoPdpGroup += (groupName + "/" + pdpSubGroupElem["pdpType"] + " (" - + pdpGroupElem[groupName]["pdpGroupState"] + ")" + "\r\n"); - }); - policy["pdpGroupInfoString"] = infoPdpGroup; - }); - } - }); - } + generateAdditionalPolicyColumns(policiesData) { + policiesData.forEach(policy => { + let supportedPdpGroupsString = ""; + if (typeof policy.supportedPdpGroups !== "undefined") { + for (const pdpGroup of policy["supportedPdpGroups"]) { + for (const pdpSubGroup of Object.values(pdpGroup)[0]) { + supportedPdpGroupsString += (Object.keys(pdpGroup)[0] + "/" + pdpSubGroup + "\r\n"); + } + } + policy["supportedPdpGroupsString"] = supportedPdpGroupsString; + } - getAllToscaModels() { - PolicyToscaService.getToscaPolicyModels().then(toscaModelsList => { - this.setState({ toscaModelsListData: toscaModelsList, - toscaModelsListDataFiltered: toscaModelsList - }); + let infoPdpGroup = ""; + if (typeof policy.pdpGroupInfo !== "undefined") { + policy["pdpGroupInfo"].forEach(pdpGroupElem => { + let groupName = Object.keys(pdpGroupElem)[0]; + pdpGroupElem[groupName]["pdpSubgroups"].forEach(pdpSubGroupElem => { + infoPdpGroup += (groupName + "/" + pdpSubGroupElem["pdpType"] + " (" + + pdpGroupElem[groupName]["pdpGroupState"] + ")" + "\r\n"); + }); + policy["pdpGroupInfoString"] = infoPdpGroup; }); - } + } + }); + } - getAllPolicies() { - PolicyService.getPoliciesList().then(allPolicies => { - this.generateAdditionalPolicyColumns(allPolicies["policies"]) - this.setState({ policiesListData: allPolicies["policies"], - policiesListDataFiltered: allPolicies["policies"], - }) - }); + getAllToscaModels() { + PolicyToscaService.getToscaPolicyModels().then(toscaModelsList => { + this.setState({ + toscaModelsListData: toscaModelsList, + toscaModelsListDataFiltered: toscaModelsList + }); + }); + } - } + getAllPolicies() { + PolicyService.getPoliciesList().then(allPolicies => { + this.generateAdditionalPolicyColumns(allPolicies["policies"]) + this.setState({ + policiesListData: allPolicies["policies"], + policiesListDataFiltered: allPolicies["policies"], + }) + }); - handleClose() { - this.setState({ show: false }); - this.props.history.push('/') - } + } - handleDeletePolicy(event, rowData) { - PolicyService.deletePolicy(rowData["type"], rowData["type_version"], rowData["name"],rowData["version"]).then( - respPolicyDeletion => { - if (typeof(respPolicyDeletion) === "undefined") { - //it indicates a failure - this.setState({ - showFailAlert: true, - showMessage: 'Policy Deletion Failure' - }); - } else { - this.setState({ - showSuccessAlert: true, - showMessage: 'Policy successfully Deleted' - }); - this.getAllPolicies(); - } - } - ) - } + handleClose() { + this.setState({ show: false }); + this.props.history.push('/') + } - disableAlert() { - this.setState ({ showSuccessAlert: false, showFailAlert: false }); - } + handleDeletePolicy(event, rowData) { + PolicyService.deletePolicy(rowData["type"], rowData["type_version"], rowData["name"], rowData["version"]).then( + respPolicyDeletion => { + if (typeof (respPolicyDeletion) === "undefined") { + //it indicates a failure + this.setState({ + showFailAlert: true, + showMessage: 'Policy Deletion Failure' + }); + } else { + this.setState({ + showSuccessAlert: true, + showMessage: 'Policy successfully Deleted' + }); + this.getAllPolicies(); + } + } + ) + } - filterPolicies(prefixForFiltering) { - this.setState({policiesListDataFiltered: this.state.policiesListData.filter(element => element.name.startsWith(prefixForFiltering))}); - } + disableAlert() { + this.setState({ showSuccessAlert: false, showFailAlert: false }); + } - filterTosca(prefixForFiltering) { - this.setState({toscaModelsListDataFiltered: this.state.toscaModelsListData.filter(element => element.policyModelType.startsWith(prefixForFiltering))}); - } + filterPolicies(prefixForFiltering) { + this.setState({ policiesListDataFiltered: this.state.policiesListData.filter(element => element.name.startsWith(prefixForFiltering)) }); + } - showFileSelector() { - this.setState({showFileSelector:true}); - } + filterTosca(prefixForFiltering) { + this.setState({ toscaModelsListDataFiltered: this.state.toscaModelsListData.filter(element => element.policyModelType.startsWith(prefixForFiltering)) }); + } - disableFileSelector() { - this.setState({showFileSelector:false}); - } + showFileSelector() { + this.setState({ showFileSelector: true }); + } - renderPoliciesTab() { - return ( - <Tab eventKey="policies" title="Policies in Policy Framework"> - <Modal.Body> - <div> - <PoliciesTreeViewerDiv> - <PoliciesTreeViewer policiesData={this.state.policiesListData} valueForTreeCreation="name" policiesFilterFunction={this.filterPolicies} /> - </PoliciesTreeViewerDiv> - <MaterialTableDiv> - <MaterialTable - title={"Policies"} - data={this.state.policiesListDataFiltered} - columns={this.state.policyColumnsDefinition} - icons={this.state.tableIcons} - onRowClick={(event, rowData, togglePanel) => togglePanel()} - options={{ - grouping: true, - exportButton: true, - headerStyle:rowHeaderStyle, - actionsColumnIndex: -1 - }} - detailPanel={[ - { - icon: ArrowForwardIosIcon, - tooltip: 'Show Configuration', - render: rowData => { - return ( - <DetailedRow> - <PolicyEditor policyModelType={rowData["type"]} policyModelTypeVersion={rowData["type_version"]} - policyName={rowData["name"]} policyVersion={rowData["version"]} policyProperties={rowData["properties"]} - policiesTableUpdateFunction={this.getAllPolicies} /> - </DetailedRow> - ) - }, - }, - { - icon: DehazeIcon, - openIcon: DehazeIcon, - tooltip: 'Show Raw Data', - render: rowData => { - return ( - <DetailedRow> - <pre>{JSON.stringify(rowData, null, 2)}</pre> - </DetailedRow> - ) - }, - }, - { - icon: PublishIcon, - openIcon: PublishIcon, - tooltip: 'PDP Group Deployment', - render: rowData => { - return ( - <DetailedRow> - <PolicyDeploymentEditor policyData={rowData} policiesTableUpdateFunction={this.getAllPolicies} /> - </DetailedRow> - ) - }, - } - ]} - actions={[ - { - icon: DeleteRoundedIcon, - tooltip: 'Delete Policy', - onClick: (event, rowData) => this.handleDeletePolicy(event, rowData) - } - ]} - /> - </MaterialTableDiv> - </div> - </Modal.Body> - </Tab> - ); - } + disableFileSelector() { + this.setState({ showFileSelector: false }); + } - renderToscaTab() { - return ( - <Tab eventKey="tosca models" title="Tosca Models in Policy Framework"> - <Modal.Body> - <div> - <PoliciesTreeViewerDiv> - <PoliciesTreeViewer policiesData={this.state.toscaModelsListData} valueForTreeCreation="policyModelType" policiesFilterFunction={this.filterTosca} /> - </PoliciesTreeViewerDiv> - <MaterialTableDiv> - <MaterialTable - title={"Tosca Models"} - data={this.state.toscaModelsListDataFiltered} - columns={this.state.toscaColumnsDefinition} - icons={this.state.tableIcons} - onRowClick={(event, rowData, togglePanel) => togglePanel()} - options={{ - grouping: true, - exportButton: true, - headerStyle:rowHeaderStyle, - actionsColumnIndex: -1 - }} - actions={[ - { - icon: AddIcon, - tooltip: 'Add New Tosca Model', - isFreeAction: true, - onClick: () => this.showFileSelector() - } - ]} - detailPanel={[ - { - icon: ArrowForwardIosIcon, - tooltip: 'Show Tosca', - render: rowData => { - return ( - <DetailedRow> - <ToscaViewer toscaData={rowData} /> - </DetailedRow> - ) - }, - }, - { - icon: DehazeIcon, - openIcon: DehazeIcon, - tooltip: 'Show Raw Data', - render: rowData => { - return ( - <DetailedRow> - <pre>{JSON.stringify(rowData, null, 2)}</pre> - </DetailedRow> - ) - }, - }, - { - icon: AddIcon, - openIcon: AddIcon, - tooltip: 'Create a policy from this model', - render: rowData => { - return ( - <DetailedRow> - <PolicyEditor policyModelType={rowData["policyModelType"]} policyModelTypeVersion={rowData["version"]} policyProperties={{}} policiesTableUpdateFunction={this.getAllPolicies} /> - </DetailedRow> - ) - }, - }, - ]} - /> - </MaterialTableDiv> - </div> - </Modal.Body> - </Tab> - ); - } + renderPoliciesTab() { + return ( + <Tab eventKey="policies" title="Policies in Policy Framework"> + <Modal.Body> + <div> + <PoliciesTreeViewerDiv> + <PoliciesTreeViewer policiesData={ this.state.policiesListData } valueForTreeCreation="name" policiesFilterFunction={ this.filterPolicies }/> + </PoliciesTreeViewerDiv> + <MaterialTableDiv> + <MaterialTable + title={ "Policies" } + data={ this.state.policiesListDataFiltered } + columns={ this.state.policyColumnsDefinition } + icons={ this.state.tableIcons } + onRowClick={ (event, rowData, togglePanel) => togglePanel() } + options={ { + grouping: true, + exportButton: true, + headerStyle: rowHeaderStyle, + actionsColumnIndex: -1 + } } + detailPanel={ [ + { + icon: ArrowForwardIosIcon, + tooltip: 'Show Configuration', + render: rowData => { + return ( + <DetailedRow> + <PolicyEditor policyModelType={ rowData["type"] } policyModelTypeVersion={ rowData["type_version"] } + policyName={ rowData["name"] } policyVersion={ rowData["version"] } policyProperties={ rowData["properties"] } + policiesTableUpdateFunction={ this.getAllPolicies }/> + </DetailedRow> + ) + }, + }, + { + icon: DehazeIcon, + openIcon: DehazeIcon, + tooltip: 'Show Raw Data', + render: rowData => { + return ( + <DetailedRow> + <pre>{ JSON.stringify(rowData, null, 2) }</pre> + </DetailedRow> + ) + }, + }, + { + icon: PublishIcon, + openIcon: PublishIcon, + tooltip: 'PDP Group Deployment', + render: rowData => { + return ( + <DetailedRow> + <PolicyDeploymentEditor policyData={ rowData } policiesTableUpdateFunction={ this.getAllPolicies }/> + </DetailedRow> + ) + }, + } + ] } + actions={ [ + { + icon: DeleteRoundedIcon, + tooltip: 'Delete Policy', + onClick: (event, rowData) => this.handleDeletePolicy(event, rowData) + } + ] } + /> + </MaterialTableDiv> + </div> + </Modal.Body> + </Tab> + ); + } - render() { - return ( - <React.Fragment> - <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false}> - <Modal.Header closeButton> - </Modal.Header> - <Tabs id="controlled-tab-example" activeKey={this.state.key} onSelect={key => this.setState({ key, selectedRowData: {} })}> - {this.renderPoliciesTab()} - {this.renderToscaTab()} - </Tabs> - <Alert variant="success" show={this.state.showSuccessAlert} onClose={this.disableAlert} dismissible> - <DivWhiteSpaceStyled> - {this.state.showMessage} - </DivWhiteSpaceStyled> - </Alert> - <Alert variant="danger" show={this.state.showFailAlert} onClose={this.disableAlert} dismissible> - <DivWhiteSpaceStyled> - {this.state.showMessage} - </DivWhiteSpaceStyled> - </Alert> - <Modal.Footer> - <Button variant="secondary" onClick={this.handleClose}>Close</Button> - </Modal.Footer> - </ModalStyled> - <PolicyToscaFileSelector show={this.state.showFileSelector} disableFunction={this.disableFileSelector} toscaTableUpdateFunction={this.getAllToscaModels}/> - </React.Fragment> - ); - } - }
\ No newline at end of file + renderToscaTab() { + return ( + <Tab eventKey="tosca models" title="Tosca Models in Policy Framework"> + <Modal.Body> + <div> + <PoliciesTreeViewerDiv> + <PoliciesTreeViewer policiesData={ this.state.toscaModelsListData } valueForTreeCreation="policyModelType" policiesFilterFunction={ this.filterTosca }/> + </PoliciesTreeViewerDiv> + <MaterialTableDiv> + <MaterialTable + title={ "Tosca Models" } + data={ this.state.toscaModelsListDataFiltered } + columns={ this.state.toscaColumnsDefinition } + icons={ this.state.tableIcons } + onRowClick={ (event, rowData, togglePanel) => togglePanel() } + options={ { + grouping: true, + exportButton: true, + headerStyle: rowHeaderStyle, + actionsColumnIndex: -1 + } } + actions={ [ + { + icon: AddIcon, + tooltip: 'Add New Tosca Model', + isFreeAction: true, + onClick: () => this.showFileSelector() + } + ] } + detailPanel={ [ + { + icon: ArrowForwardIosIcon, + tooltip: 'Show Tosca', + render: rowData => { + return ( + <DetailedRow> + <ToscaViewer toscaData={ rowData }/> + </DetailedRow> + ) + }, + }, + { + icon: DehazeIcon, + openIcon: DehazeIcon, + tooltip: 'Show Raw Data', + render: rowData => { + return ( + <DetailedRow> + <pre>{ JSON.stringify(rowData, null, 2) }</pre> + </DetailedRow> + ) + }, + }, + { + icon: AddIcon, + openIcon: AddIcon, + tooltip: 'Create a policy from this model', + render: rowData => { + return ( + <DetailedRow> + <PolicyEditor policyModelType={ rowData["policyModelType"] } policyModelTypeVersion={ rowData["version"] } policyProperties={ {} } policiesTableUpdateFunction={ this.getAllPolicies }/> + </DetailedRow> + ) + }, + }, + ] } + /> + </MaterialTableDiv> + </div> + </Modal.Body> + </Tab> + ); + } + + render() { + return ( + <React.Fragment> + <ModalStyled size="xl" show={ this.state.show } onHide={ this.handleClose } backdrop="static" keyboard={ false }> + <Modal.Header closeButton> + </Modal.Header> + <Tabs id="controlled-tab-example" activeKey={ this.state.key } onSelect={ key => this.setState({ key, selectedRowData: {} }) }> + { this.renderPoliciesTab() } + { this.renderToscaTab() } + </Tabs> + <Alert variant="success" show={ this.state.showSuccessAlert } onClose={ this.disableAlert } dismissible> + <DivWhiteSpaceStyled> + { this.state.showMessage } + </DivWhiteSpaceStyled> + </Alert> + <Alert variant="danger" show={ this.state.showFailAlert } onClose={ this.disableAlert } dismissible> + <DivWhiteSpaceStyled> + { this.state.showMessage } + </DivWhiteSpaceStyled> + </Alert> + <Modal.Footer> + <Button variant="secondary" onClick={ this.handleClose }>Close</Button> + </Modal.Footer> + </ModalStyled> + <PolicyToscaFileSelector show={ this.state.showFileSelector } disableFunction={ this.disableFileSelector } toscaTableUpdateFunction={ this.getAllToscaModels }/> + </React.Fragment> + ); + } +} diff --git a/runtime/ui-react/src/components/dialogs/RefreshStatus.js b/runtime/ui-react/src/components/dialogs/RefreshStatus.js index bb0939152..e23ab3f84 100644 --- a/runtime/ui-react/src/components/dialogs/RefreshStatus.js +++ b/runtime/ui-react/src/components/dialogs/RefreshStatus.js @@ -26,40 +26,40 @@ import Spinner from 'react-bootstrap/Spinner'; import styled from 'styled-components'; const StyledSpinnerDiv = styled.div` - justify-content: center !important; - display: flex !important; + justify-content: center !important; + display: flex !important; `; export default class RefreshStatus extends React.Component { - state = { - loopName: this.props.loopCache.getLoopName() - }; + state = { + loopName: this.props.loopCache.getLoopName() + }; - componentWillReceiveProps(newProps) { - this.setState({ - loopName: newProps.loopCache.getLoopName() - }); - } + componentWillReceiveProps(newProps) { + this.setState({ + loopName: newProps.loopCache.getLoopName() + }); + } - componentDidMount() { - // refresh status and update loop logs - LoopActionService.refreshStatus(this.state.loopName).then(data => { - this.props.showSucAlert("Status successfully refreshed"); - this.props.updateLoopFunction(data); - this.props.history.push('/'); - }) - .catch(error => { - this.props.showFailAlert("Status refreshing failed"); - this.props.history.push('/'); - }); - } + componentDidMount() { + // refresh status and update loop logs + LoopActionService.refreshStatus(this.state.loopName).then(data => { + this.props.showSucAlert("Status successfully refreshed"); + this.props.updateLoopFunction(data); + this.props.history.push('/'); + }) + .catch(error => { + this.props.showFailAlert("Status refreshing failed"); + this.props.history.push('/'); + }); + } - render() { - return ( - <StyledSpinnerDiv> - <Spinner animation="border" role="status"> - </Spinner> - </StyledSpinnerDiv> - ); - } + render() { + return ( + <StyledSpinnerDiv> + <Spinner animation="border" role="status"> + </Spinner> + </StyledSpinnerDiv> + ); + } } diff --git a/runtime/ui-react/src/components/dialogs/RefreshStatus.test.js b/runtime/ui-react/src/components/dialogs/RefreshStatus.test.js index e08c50d2e..7736ffd49 100644 --- a/runtime/ui-react/src/components/dialogs/RefreshStatus.test.js +++ b/runtime/ui-react/src/components/dialogs/RefreshStatus.test.js @@ -28,44 +28,45 @@ import LoopActionService from '../../api/LoopActionService'; describe('Verify RefreshStatus', () => { - const loopCache = new LoopCache({ - "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca" - }); + const loopCache = new LoopCache({ + "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca" + }); - it('Test refresh status failed', async () => { - const flushPromises = () => new Promise(setImmediate); - const historyMock = { push: jest.fn() }; - const showSucAlert = jest.fn(); - const showFailAlert = jest.fn(); + it('Test refresh status failed', async () => { + const flushPromises = () => new Promise(setImmediate); + const historyMock = { push: jest.fn() }; + const showSucAlert = jest.fn(); + const showFailAlert = jest.fn(); - const component = shallow(<RefreshStatus loopCache={loopCache} history={historyMock} showSucAlert={showSucAlert} showFailAlert={showFailAlert} />) - await flushPromises(); - component.update(); + const component = shallow(<RefreshStatus loopCache={ loopCache } history={ historyMock } showSucAlert={ showSucAlert } showFailAlert={ showFailAlert }/>) + await flushPromises(); + component.update(); - expect(historyMock.push.mock.calls[0]).toEqual([ '/']); - }); + expect(historyMock.push.mock.calls[0]).toEqual(['/']); + }); - it('Test refresh status successful', async () => { - const flushPromises = () => new Promise(setImmediate); - const historyMock = { push: jest.fn() }; - const updateLoopFunction = jest.fn(); - const showSucAlert = jest.fn(); - const showFailAlert = jest.fn(); + it('Test refresh status successful', async () => { + const flushPromises = () => new Promise(setImmediate); + const historyMock = { push: jest.fn() }; + const updateLoopFunction = jest.fn(); + const showSucAlert = jest.fn(); + const showFailAlert = jest.fn(); - LoopActionService.refreshStatus = jest.fn().mockImplementation(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => {} - }); - }); + LoopActionService.refreshStatus = jest.fn().mockImplementation(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + } + }); + }); - const component = shallow(<RefreshStatus loopCache={loopCache} - loopAction="submit" history={historyMock} updateLoopFunction={updateLoopFunction} showSucAlert={showSucAlert} showFailAlert={showFailAlert} />) - await flushPromises(); - component.update(); + const component = shallow(<RefreshStatus loopCache={ loopCache } + loopAction="submit" history={ historyMock } updateLoopFunction={ updateLoopFunction } showSucAlert={ showSucAlert } showFailAlert={ showFailAlert }/>) + await flushPromises(); + component.update(); - expect(historyMock.push.mock.calls[0]).toEqual([ '/']); - }); + expect(historyMock.push.mock.calls[0]).toEqual(['/']); + }); }); diff --git a/runtime/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.js b/runtime/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.js index 4796b8d73..e7be9841e 100644 --- a/runtime/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.js +++ b/runtime/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.js @@ -38,126 +38,136 @@ import LoopCache from '../../../api/LoopCache'; import SvgGenerator from '../../loop_viewer/svg/SvgGenerator'; const ModalStyled = styled(Modal)` - background-color: transparent; + background-color: transparent; ` const cellStyle = { border: '1px solid black' }; -const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' }; -const rowHeaderStyle = {backgroundColor:'#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black'}; +const headerStyle = { backgroundColor: '#ddd', border: '2px solid black' }; +const rowHeaderStyle = { backgroundColor: '#ddd', fontSize: '15pt', text: 'bold', border: '1px solid black' }; export default class ViewLoopTemplatesModal extends React.Component { state = { - show: true, - content: 'Please select a loop template to display it', - selectedRow: -1, - loopTemplatesData: [], - fakeLoopCacheWithTemplate: new LoopCache({}), - loopTemplateColumnsDefinition: [ - { title: "#", field: "index", render: rowData => rowData.tableData.id + 1, - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { title: "Template Name", field: "name", - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { title: "Service Model Name", field: "modelService.serviceDetails.name", - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { title: "Loop Type Allowed", field: "allowedLoopType", - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { title: "# Instances Allowed", field: "maximumInstancesAllowed", - cellStyle: cellStyle, - headerStyle: headerStyle - }, - { title: "Modified Date", field: "updatedDate", editable: 'never', - cellStyle: cellStyle, - headerStyle: headerStyle - } - ], - tableIcons: { - FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />), - LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />), - NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />), - PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />), - ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />), - Search: forwardRef((props, ref) => <Search {...props} ref={ref} />), - SortArrow: forwardRef((props, ref) => <ArrowUpward {...props} ref={ref} />) - } - }; - - constructor(props, context) { - super(props, context); - this.handleClose = this.handleClose.bind(this); - this.renderSvg = this.renderSvg.bind(this); - this.getLoopTemplate = this.getLoopTemplate.bind(this); - this.getAllLoopTemplates(); - } + show: true, + content: 'Please select a loop template to display it', + selectedRow: -1, + loopTemplatesData: [], + fakeLoopCacheWithTemplate: new LoopCache({}), + loopTemplateColumnsDefinition: [ + { + title: "#", field: "index", render: rowData => rowData.tableData.id + 1, + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Template Name", field: "name", + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Service Model Name", field: "modelService.serviceDetails.name", + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Loop Type Allowed", field: "allowedLoopType", + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "# Instances Allowed", field: "maximumInstancesAllowed", + cellStyle: cellStyle, + headerStyle: headerStyle + }, + { + title: "Modified Date", field: "updatedDate", editable: 'never', + cellStyle: cellStyle, + headerStyle: headerStyle + } + ], + tableIcons: { + FirstPage: forwardRef((props, ref) => <FirstPage { ...props } ref={ ref }/>), + LastPage: forwardRef((props, ref) => <LastPage { ...props } ref={ ref }/>), + NextPage: forwardRef((props, ref) => <ChevronRight { ...props } ref={ ref }/>), + PreviousPage: forwardRef((props, ref) => <ChevronLeft { ...props } ref={ ref }/>), + ResetSearch: forwardRef((props, ref) => <Clear { ...props } ref={ ref }/>), + Search: forwardRef((props, ref) => <Search { ...props } ref={ ref }/>), + SortArrow: forwardRef((props, ref) => <ArrowUpward { ...props } ref={ ref }/>) + } + }; - getAllLoopTemplates() { - TemplateService.getAllLoopTemplates().then(templatesData => { - // replace -1 in maximumInstancesAllowed with more meaningful 'No Limit' - for (let item in templatesData) { - if (templatesData[item].maximumInstancesAllowed === -1) { - templatesData[item].maximumInstancesAllowed = 'No Limit'; - } - } - this.setState({ loopTemplatesData: templatesData }) - }); - } + constructor(props, context) { + super(props, context); + this.handleClose = this.handleClose.bind(this); + this.renderSvg = this.renderSvg.bind(this); + this.getLoopTemplate = this.getLoopTemplate.bind(this); + this.getAllLoopTemplates(); + } - getLoopTemplate(templateIdInDataArray) { - if (typeof templateIdInDataArray !== "undefined") { - this.setState({ fakeLoopCacheWithTemplate: - new LoopCache({ - "loopTemplate":this.state.loopTemplatesData[templateIdInDataArray], - "name": "fakeLoop" - }) - }) - } else { - this.setState({ fakeLoopCacheWithTemplate: new LoopCache({}) }) + getAllLoopTemplates() { + TemplateService.getAllLoopTemplates().then(templatesData => { + // replace -1 in maximumInstancesAllowed with more meaningful 'No Limit' + for (let item in templatesData) { + if (templatesData[item].maximumInstancesAllowed === -1) { + templatesData[item].maximumInstancesAllowed = 'No Limit'; } + } + this.setState({ loopTemplatesData: templatesData }) + }); + } + + getLoopTemplate(templateIdInDataArray) { + if (typeof templateIdInDataArray !== "undefined") { + this.setState({ + fakeLoopCacheWithTemplate: + new LoopCache({ + "loopTemplate": this.state.loopTemplatesData[templateIdInDataArray], + "name": "fakeLoop" + }) + }) + } else { + this.setState({ fakeLoopCacheWithTemplate: new LoopCache({}) }) } + } - handleClose() { - this.setState({ show: false }); - this.props.history.push('/') - } + handleClose() { + this.setState({ show: false }); + this.props.history.push('/') + } - renderSvg() { - return( - <SvgGenerator loopCache={this.state.fakeLoopCacheWithTemplate} clickable={false} generatedFrom={SvgGenerator.GENERATED_FROM_TEMPLATE}/> - ) - } + renderSvg() { + return ( + <SvgGenerator loopCache={ this.state.fakeLoopCacheWithTemplate } clickable={ false } generatedFrom={ SvgGenerator.GENERATED_FROM_TEMPLATE }/> + ) + } - render() { + render() { return ( - <ModalStyled size="xl" show={this.state.show} onHide={this.handleClose} backdrop="static" keyboard={false}> - <Modal.Header closeButton> - </Modal.Header> - <Modal.Body> - <MaterialTable - title={"View Blueprint MicroService Templates"} - data={this.state.loopTemplatesData} - columns={this.state.loopTemplateColumnsDefinition} - icons={this.state.tableIcons} - onRowClick={(event, rowData) => {this.getLoopTemplate(rowData.tableData.id);this.setState({selectedRow: rowData.tableData.id})}} - options={{ - headerStyle:rowHeaderStyle, - rowStyle: rowData => ({ - backgroundColor: (this.state.selectedRow !== -1 && this.state.selectedRow === rowData.tableData.id) ? '#EEE' : '#FFF' - }) - }} - /> - {this.renderSvg()} - </Modal.Body> - <Modal.Footer> - <Button variant="secondary" onClick={this.handleClose}>Close</Button> - </Modal.Footer> + <ModalStyled size="xl" show={ this.state.show } onHide={ this.handleClose } backdrop="static" keyboard={ false }> + <Modal.Header closeButton> + </Modal.Header> + <Modal.Body> + <MaterialTable + title={ "View Blueprint MicroService Templates" } + data={ this.state.loopTemplatesData } + columns={ this.state.loopTemplateColumnsDefinition } + icons={ this.state.tableIcons } + onRowClick={ (event, rowData) => { + this.getLoopTemplate(rowData.tableData.id); + this.setState({ selectedRow: rowData.tableData.id }) + } } + options={ { + headerStyle: rowHeaderStyle, + rowStyle: rowData => ({ + backgroundColor: (this.state.selectedRow !== -1 && this.state.selectedRow === rowData.tableData.id) ? '#EEE' : '#FFF' + }) + } } + /> + { this.renderSvg() } + </Modal.Body> + <Modal.Footer> + <Button variant="secondary" onClick={ this.handleClose }>Close</Button> + </Modal.Footer> </ModalStyled> - ); - } + ); } +} diff --git a/runtime/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.test.js b/runtime/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.test.js index 7680ec4b9..d93f0b00c 100644 --- a/runtime/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.test.js +++ b/runtime/ui-react/src/components/dialogs/Tosca/ViewLoopTemplatesModal.test.js @@ -27,136 +27,137 @@ import { mount } from 'enzyme'; import { BrowserRouter as Router } from 'react-router-dom'; describe('Verify ViewLoopTemplatesModal', () => { - beforeEach(() => { - fetch.resetMocks(); - }); + beforeEach(() => { + fetch.resetMocks(); + }); - it('Test API Successful', () => { - fetch.mockImplementationOnce(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => { - return Promise.resolve({ - "index": "1", - "name": "MTCA version 1", - "modelService.serviceDetails.name": "MTCA", - "allowedLoopType" : "CLOSED", - "maximumInstancesAllowed":1, - "updatedDate":"2019-09-06 19:09:42" - }); - } - }); - }); - const component = shallow(<ViewLoopTemplatesModal/>); - }); - - it('Test API Exception', () => { - fetch.mockImplementationOnce(() => { - return Promise.resolve({ - ok: false, - status: 500, - json: () => { - return Promise.resolve({ - "index": "1", - "name": "MTCA version 1", - "modelService.serviceDetails.name": "MTCA", - "allowedLoopType" : "CLOSED", - "maximumInstancesAllowed":1, - "updatedDate":"2019-09-06 19:09:42" - }); - } - }); - }); - const component = shallow(<ViewLoopTemplatesModal/>); - }); + it('Test API Successful', () => { + fetch.mockImplementationOnce(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + return Promise.resolve({ + "index": "1", + "name": "MTCA version 1", + "modelService.serviceDetails.name": "MTCA", + "allowedLoopType": "CLOSED", + "maximumInstancesAllowed": 1, + "updatedDate": "2019-09-06 19:09:42" + }); + } + }); + }); + const component = shallow(<ViewLoopTemplatesModal/>); + }); - it('Test API Rejection', () => { - const myMockFunc = fetch.mockImplementationOnce(() => Promise.reject('error')); - setTimeout( () => myMockFunc().catch(e => { - console.info(e); - }), - 100 - ); - const component = shallow(<ViewLoopTemplatesModal/>); - expect(myMockFunc.mock.calls.length).toBe(1); - }); + it('Test API Exception', () => { + fetch.mockImplementationOnce(() => { + return Promise.resolve({ + ok: false, + status: 500, + json: () => { + return Promise.resolve({ + "index": "1", + "name": "MTCA version 1", + "modelService.serviceDetails.name": "MTCA", + "allowedLoopType": "CLOSED", + "maximumInstancesAllowed": 1, + "updatedDate": "2019-09-06 19:09:42" + }); + } + }); + }); + const component = shallow(<ViewLoopTemplatesModal/>); + }); - it('Test the tosca model view render method', () => { - fetch.mockImplementationOnce(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => { - return Promise.resolve({ - "index": "1", - "name": "MTCA version 1", - "modelService.serviceDetails.name": "MTCA", - "allowedLoopType" : "CLOSED", - "maximumInstancesAllowed":1, - "updatedDate":"2019-09-06 19:09:42" - }); - } - }); - }); - const component = shallow(<ViewLoopTemplatesModal/>); - component.setState({ loopTemplateData: { - "index": "1", - "name": "MTCA version 1", - "modelService.serviceDetails.name": "MTCA", - "allowedLoopType" : "CLOSED", - "maximumInstancesAllowed":1, - "updatedDate":"2019-09-06 19:09:42" - } - }); - expect(component).toMatchSnapshot(); - }); + it('Test API Rejection', () => { + const myMockFunc = fetch.mockImplementationOnce(() => Promise.reject('error')); + setTimeout(() => myMockFunc().catch(e => { + console.info(e); + }), + 100 + ); + const component = shallow(<ViewLoopTemplatesModal/>); + expect(myMockFunc.mock.calls.length).toBe(1); + }); - it('Test Table icons', () => { - fetch.mockImplementationOnce(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => { - return Promise.resolve({ - "index": "1", - "name": "MTCA version 1", - "modelService.serviceDetails.name": "MTCA", - "allowedLoopType" : "CLOSED", - "maximumInstancesAllowed":1, - "updatedDate":"2019-09-06 19:09:42" - }); - } - }); - }); - const component = mount(<Router><ViewLoopTemplatesModal/></Router>); - expect(component.find('[className="MuiSelect-icon MuiTablePagination-selectIcon"]')).toBeTruthy(); - }); - - it('Test handleClose', () => { - fetch.mockImplementationOnce(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => { - return Promise.resolve({ - "index": "1", - "name": "MTCA version 1", - "modelService.serviceDetails.name": "MTCA", - "allowedLoopType" : "CLOSED", - "maximumInstancesAllowed":1, - "updatedDate":"2019-09-06 19:09:42" - }); - } - }); - }); - const historyMock = { push: jest.fn() }; - const handleClose = jest.spyOn(ViewLoopTemplatesModal.prototype,'handleClose'); - const component = shallow(<ViewLoopTemplatesModal history={historyMock} />) - component.find('[variant="secondary"]').prop('onClick')(); - expect(handleClose).toHaveBeenCalledTimes(1); - expect(component.state('show')).toEqual(false); - expect(historyMock.push.mock.calls[0]).toEqual([ '/']); - handleClose.mockClear(); - }); - }); + it('Test the tosca model view render method', () => { + fetch.mockImplementationOnce(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + return Promise.resolve({ + "index": "1", + "name": "MTCA version 1", + "modelService.serviceDetails.name": "MTCA", + "allowedLoopType": "CLOSED", + "maximumInstancesAllowed": 1, + "updatedDate": "2019-09-06 19:09:42" + }); + } + }); + }); + const component = shallow(<ViewLoopTemplatesModal/>); + component.setState({ + loopTemplateData: { + "index": "1", + "name": "MTCA version 1", + "modelService.serviceDetails.name": "MTCA", + "allowedLoopType": "CLOSED", + "maximumInstancesAllowed": 1, + "updatedDate": "2019-09-06 19:09:42" + } + }); + expect(component).toMatchSnapshot(); + }); + + it('Test Table icons', () => { + fetch.mockImplementationOnce(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + return Promise.resolve({ + "index": "1", + "name": "MTCA version 1", + "modelService.serviceDetails.name": "MTCA", + "allowedLoopType": "CLOSED", + "maximumInstancesAllowed": 1, + "updatedDate": "2019-09-06 19:09:42" + }); + } + }); + }); + const component = mount(<Router><ViewLoopTemplatesModal/></Router>); + expect(component.find('[className="MuiSelect-icon MuiTablePagination-selectIcon"]')).toBeTruthy(); + }); + + it('Test handleClose', () => { + fetch.mockImplementationOnce(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + return Promise.resolve({ + "index": "1", + "name": "MTCA version 1", + "modelService.serviceDetails.name": "MTCA", + "allowedLoopType": "CLOSED", + "maximumInstancesAllowed": 1, + "updatedDate": "2019-09-06 19:09:42" + }); + } + }); + }); + const historyMock = { push: jest.fn() }; + const handleClose = jest.spyOn(ViewLoopTemplatesModal.prototype, 'handleClose'); + const component = shallow(<ViewLoopTemplatesModal history={ historyMock }/>) + component.find('[variant="secondary"]').prop('onClick')(); + expect(handleClose).toHaveBeenCalledTimes(1); + expect(component.state('show')).toEqual(false); + expect(historyMock.push.mock.calls[0]).toEqual(['/']); + handleClose.mockClear(); + }); +}); diff --git a/runtime/ui-react/src/components/dialogs/UserInfoModal.js b/runtime/ui-react/src/components/dialogs/UserInfoModal.js index 96eabb4c2..d452f6de0 100644 --- a/runtime/ui-react/src/components/dialogs/UserInfoModal.js +++ b/runtime/ui-react/src/components/dialogs/UserInfoModal.js @@ -30,81 +30,86 @@ import styled from 'styled-components'; import UserService from '../../api/UserService'; const ModalStyled = styled(Modal)` - background-color: transparent; + background-color: transparent; ` export default class UserInfoModal extends React.Component { - constructor(props, context) { - super(props, context); + constructor(props, context) { + super(props, context); - this.handleClose = this.handleClose.bind(this); - this.renderPermissions = this.renderPermissions.bind(this); - this.renderUserName = this.renderUserName.bind(this); - this.state = { - show: true, - userInfo: {} - }; - } - componentWillMount() { - UserService.getUserInfo().then(userInfo => { - this.setState({ userInfo: userInfo }) - }); - } + this.handleClose = this.handleClose.bind(this); + this.renderPermissions = this.renderPermissions.bind(this); + this.renderUserName = this.renderUserName.bind(this); + this.state = { + show: true, + userInfo: {} + }; + } - handleClose() { - this.props.history.push('/'); - } - renderPermissions() { - if (this.state.userInfo["allPermissions"]) { - var listOfPermissions = this.state.userInfo["allPermissions"].map(function(perm) { - return <Form.Control key={perm} plaintext readOnly defaultValue={perm} />; - }) - return listOfPermissions; - } else { - return; - } - } - renderUserName() { - if (this.state.userInfo["userName"]) { - return <Form.Control plaintext readOnly defaultValue={this.state.userInfo["userName"]} /> - } else { - return; - } - } - renderVersion() { - if (this.state.userInfo["cldsVersion"]) { - return <Form.Control plaintext readOnly defaultValue={this.state.userInfo["cldsVersion"]} /> - } else { - return; - } - } - render() { - return ( - <ModalStyled size="lg" show={this.state.show} onHide={this.handleClose}> - <Modal.Header closeButton> - <Modal.Title>User Info</Modal.Title> - </Modal.Header> - <Modal.Body> - <Form.Group as={Row} controlId="userName"> - <Form.Label column sm="3">Current User:</Form.Label> - <Col>{this.renderUserName()}</Col> - </Form.Group> - <Form.Group as={Row} controlId="cldsVersion"> - <Form.Label column sm="3">CLDS Version:</Form.Label> - <Col>{this.renderVersion()}</Col> - </Form.Group> - <Form.Group as={Row} controlId="userPermissions"> - <Form.Label column sm="3">User Permissions:</Form.Label> - <Col> - {this.renderPermissions()} - </Col> - </Form.Group> - </Modal.Body> - <Modal.Footer> - <Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button> - </Modal.Footer> - </ModalStyled> - ); - } + componentWillMount() { + UserService.getUserInfo().then(userInfo => { + this.setState({ userInfo: userInfo }) + }); + } + + handleClose() { + this.props.history.push('/'); + } + + renderPermissions() { + if (this.state.userInfo["allPermissions"]) { + var listOfPermissions = this.state.userInfo["allPermissions"].map(function (perm) { + return <Form.Control key={ perm } plaintext readOnly defaultValue={ perm }/>; + }) + return listOfPermissions; + } else { + return; + } + } + + renderUserName() { + if (this.state.userInfo["userName"]) { + return <Form.Control plaintext readOnly defaultValue={ this.state.userInfo["userName"] }/> + } else { + return; + } + } + + renderVersion() { + if (this.state.userInfo["cldsVersion"]) { + return <Form.Control plaintext readOnly defaultValue={ this.state.userInfo["cldsVersion"] }/> + } else { + return; + } + } + + render() { + return ( + <ModalStyled size="lg" show={ this.state.show } onHide={ this.handleClose }> + <Modal.Header closeButton> + <Modal.Title>User Info</Modal.Title> + </Modal.Header> + <Modal.Body> + <Form.Group as={ Row } controlId="userName"> + <Form.Label column sm="3">Current User:</Form.Label> + <Col>{ this.renderUserName() }</Col> + </Form.Group> + <Form.Group as={ Row } controlId="cldsVersion"> + <Form.Label column sm="3">CLDS Version:</Form.Label> + <Col>{ this.renderVersion() }</Col> + </Form.Group> + <Form.Group as={ Row } controlId="userPermissions"> + <Form.Label column sm="3">User Permissions:</Form.Label> + <Col> + { this.renderPermissions() } + </Col> + </Form.Group> + </Modal.Body> + <Modal.Footer> + <Button variant="secondary" type="null" onClick={ this.handleClose }>Cancel</Button> + </Modal.Footer> + </ModalStyled> + ); + } } diff --git a/runtime/ui-react/src/components/dialogs/UserInfoModal.test.js b/runtime/ui-react/src/components/dialogs/UserInfoModal.test.js index f5ed0ae10..716879232 100644 --- a/runtime/ui-react/src/components/dialogs/UserInfoModal.test.js +++ b/runtime/ui-react/src/components/dialogs/UserInfoModal.test.js @@ -26,53 +26,59 @@ import UserInfoModal from './UserInfoModal'; describe('Verify UserInfoModal', () => { - beforeEach(() => { - fetch.resetMocks(); - fetch.mockImplementation(() => { - return Promise.resolve({ - ok: true, - status: 200, - json: () => { - return Promise.resolve({ - "userName": "test", - "cldsVersion": "1.0.0" - }); - }}); - }); - }) + beforeEach(() => { + fetch.resetMocks(); + fetch.mockImplementation(() => { + return Promise.resolve({ + ok: true, + status: 200, + json: () => { + return Promise.resolve({ + "userName": "test", + "cldsVersion": "1.0.0" + }); + } + }); + }); + }) - it('Test the render method full permission', () => { - const component = shallow(<UserInfoModal />) - component.setState({ userInfo: { - "userName": "test", - "cldsVersion": "1.0.0", - "allPermissions": ["permission1","permission2"] - }}); - expect(component).toMatchSnapshot(); - }); + it('Test the render method full permission', () => { + const component = shallow(<UserInfoModal/>) + component.setState({ + userInfo: { + "userName": "test", + "cldsVersion": "1.0.0", + "allPermissions": ["permission1", "permission2"] + } + }); + expect(component).toMatchSnapshot(); + }); - it('Test the render method no permission', () => { - const component = shallow(<UserInfoModal />) - component.setState({ userInfo: {} - }); + it('Test the render method no permission', () => { + const component = shallow(<UserInfoModal/>) + component.setState({ + userInfo: {} + }); - expect(component.find('FormControl').length).toEqual(0); - }); + expect(component.find('FormControl').length).toEqual(0); + }); - it('Test the render method read permission', () => { - const component = shallow(<UserInfoModal />) - component.setState({ userInfo: { - "userName": "test", - "cldsVersion": "1.0.0", - "allPermissions": ["permission1","permission2"] - }}); + it('Test the render method read permission', () => { + const component = shallow(<UserInfoModal/>) + component.setState({ + userInfo: { + "userName": "test", + "cldsVersion": "1.0.0", + "allPermissions": ["permission1", "permission2"] + } + }); - expect(component.find('FormControl').length).toEqual(4); + expect(component.find('FormControl').length).toEqual(4); - const forms = component.find('FormControl'); - expect(forms.get(0).props.defaultValue).toEqual("test"); - expect(forms.get(1).props.defaultValue).toEqual("1.0.0"); - expect(forms.get(2).props.defaultValue).toEqual("permission1"); - expect(forms.get(3).props.defaultValue).toEqual("permission2"); - }); + const forms = component.find('FormControl'); + expect(forms.get(0).props.defaultValue).toEqual("test"); + expect(forms.get(1).props.defaultValue).toEqual("1.0.0"); + expect(forms.get(2).props.defaultValue).toEqual("permission1"); + expect(forms.get(3).props.defaultValue).toEqual("permission2"); + }); }); diff --git a/runtime/ui-react/src/components/loop_viewer/logs/LoopLogs.js b/runtime/ui-react/src/components/loop_viewer/logs/LoopLogs.js index e3d9f601f..3435ba3bc 100644 --- a/runtime/ui-react/src/components/loop_viewer/logs/LoopLogs.js +++ b/runtime/ui-react/src/components/loop_viewer/logs/LoopLogs.js @@ -26,71 +26,72 @@ import LoopCache from '../../../api/LoopCache'; import styled from 'styled-components'; const LoopLogsHeaderDivStyled = styled.div` - background-color: ${props => props.theme.loopLogsHeaderBackgroundColor}; - padding: 10px 10px; - color: ${props => props.theme.loopLogsHeaderFontColor}; + background-color: ${ props => props.theme.loopLogsHeaderBackgroundColor }; + padding: 10px 10px; + color: ${ props => props.theme.loopLogsHeaderFontColor }; ` const TableStyled = styled(Table)` - - overflow: auto; + + overflow: auto; ` const TableRow = ({ logRow }) => ( - <tr> - <td>{logRow.logInstant}</td> - <td>{logRow.logType}</td> - <td>{logRow.logComponent}</td> - <td>{logRow.message}</td> - </tr> + <tr> + <td>{ logRow.logInstant }</td> + <td>{ logRow.logType }</td> + <td>{ logRow.logComponent }</td> + <td>{ logRow.message }</td> + </tr> ) export default class LoopLogs extends React.Component { - state = { - loopCache: new LoopCache({}) - } - constructor(props) { - super(props); - this.renderLogs = this.renderLogs.bind(this); - this.state.loopCache = props.loopCache; - } + state = { + loopCache: new LoopCache({}) + } + + constructor(props) { + super(props); + this.renderLogs = this.renderLogs.bind(this); + this.state.loopCache = props.loopCache; + } - shouldComponentUpdate(nextProps, nextState) { - return this.state.loopCache !== nextState.loopCache; - } + shouldComponentUpdate(nextProps, nextState) { + return this.state.loopCache !== nextState.loopCache; + } - componentWillReceiveProps(newProps) { - this.setState({ - loopCache: newProps.loopCache - }); - } + componentWillReceiveProps(newProps) { + this.setState({ + loopCache: newProps.loopCache + }); + } - renderLogs() { - let logsArray = this.state.loopCache.getLoopLogsArray(); - if (logsArray != null) { - return (logsArray.map(row => <TableRow key={row.id} logRow={row} />)); - } - } + renderLogs() { + let logsArray = this.state.loopCache.getLoopLogsArray(); + if (logsArray != null) { + return (logsArray.map(row => <TableRow key={ row.id } logRow={ row }/>)); + } + } - render() { - return ( - <LoopLogsHeaderDivStyled> - <label>Loop Logs</label> - <TableStyled striped hover variant responsive> - <thead> - <tr> - <th><span align="left">Date</span></th> - <th><span align="left">Type</span></th> - <th><span align="left">Component</span></th> - <th><span align="right">Log</span></th> - </tr> - </thead> - <tbody> - {this.renderLogs()} - </tbody> - </TableStyled> - </LoopLogsHeaderDivStyled> + render() { + return ( + <LoopLogsHeaderDivStyled> + <label>Loop Logs</label> + <TableStyled striped hover variant responsive> + <thead> + <tr> + <th><span align="left">Date</span></th> + <th><span align="left">Type</span></th> + <th><span align="left">Component</span></th> + <th><span align="right">Log</span></th> + </tr> + </thead> + <tbody> + { this.renderLogs() } + </tbody> + </TableStyled> + </LoopLogsHeaderDivStyled> - ); - } + ); + } } diff --git a/runtime/ui-react/src/components/loop_viewer/logs/LoopLogs.test.js b/runtime/ui-react/src/components/loop_viewer/logs/LoopLogs.test.js index 3b7fd413a..d3a21d80d 100644 --- a/runtime/ui-react/src/components/loop_viewer/logs/LoopLogs.test.js +++ b/runtime/ui-react/src/components/loop_viewer/logs/LoopLogs.test.js @@ -27,44 +27,44 @@ import LoopCache from '../../../api/LoopCache'; describe('Verify LoopLogs', () => { - const loopCache = new LoopCache({ - "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", - "loopLogs": [ - { - "id": 1, - "logType": "INFO", - "logComponent": "CLAMP", - "message": "Operational policies UPDATED", - "logInstant": "2019-07-08T09:44:37Z" - } - ] - }); + const loopCache = new LoopCache({ + "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", + "loopLogs": [ + { + "id": 1, + "logType": "INFO", + "logComponent": "CLAMP", + "message": "Operational policies UPDATED", + "logInstant": "2019-07-08T09:44:37Z" + } + ] + }); - it('Test the render method', () => { - const component = shallow(<LoopLogs loopCache={loopCache}/>) - expect(component).toMatchSnapshot(); + it('Test the render method', () => { + const component = shallow(<LoopLogs loopCache={ loopCache }/>) + expect(component).toMatchSnapshot(); - const loopCacheUpdated = new LoopCache({ - "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", - "loopLogs": [ - { - "id": 1, - "logType": "INFO", - "logComponent": "CLAMP", - "message": "Operational policies UPDATED", - "logInstant": "2019-07-08T09:44:37Z" - }, - { - "id": 2, - "logType": "INFO", - "logComponent": "CLAMP", - "message": "Operational policies UPDATED", - "logInstant": "2019-07-08T09:44:50Z" - } - ] - }); + const loopCacheUpdated = new LoopCache({ + "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", + "loopLogs": [ + { + "id": 1, + "logType": "INFO", + "logComponent": "CLAMP", + "message": "Operational policies UPDATED", + "logInstant": "2019-07-08T09:44:37Z" + }, + { + "id": 2, + "logType": "INFO", + "logComponent": "CLAMP", + "message": "Operational policies UPDATED", + "logInstant": "2019-07-08T09:44:50Z" + } + ] + }); - component.setProps({ loopCache: loopCacheUpdated }); - expect(component.find('TableRow').length).toEqual(2); - }); -});
\ No newline at end of file + component.setProps({ loopCache: loopCacheUpdated }); + expect(component.find('TableRow').length).toEqual(2); + }); +}); diff --git a/runtime/ui-react/src/components/loop_viewer/status/LoopStatus.js b/runtime/ui-react/src/components/loop_viewer/status/LoopStatus.js index 4b35b48e1..f539ad436 100644 --- a/runtime/ui-react/src/components/loop_viewer/status/LoopStatus.js +++ b/runtime/ui-react/src/components/loop_viewer/status/LoopStatus.js @@ -26,81 +26,85 @@ import styled from 'styled-components'; import LoopCache from '../../../api/LoopCache'; const LoopStatusViewDivStyled = styled.div` - background-color: ${props => props.theme.loopViewerHeaderBackgroundColor}; - padding: 10px 10px; - color: ${props => props.theme.loopViewerHeaderFontColor}; + background-color: ${ props => props.theme.loopViewerHeaderBackgroundColor }; + padding: 10px 10px; + color: ${ props => props.theme.loopViewerHeaderFontColor }; ` const TableStyled = styled(Table)` - overflow: auto; + overflow: auto; ` const TableRow = ({ statusRow }) => ( - <tr> - <td>{statusRow.componentName}</td> - <td>{statusRow.stateName}</td> - <td>{statusRow.description}</td> - </tr> + <tr> + <td>{ statusRow.componentName }</td> + <td>{ statusRow.stateName }</td> + <td>{ statusRow.description }</td> + </tr> ) export default class LoopStatus extends React.Component { - state = { - loopCache: new LoopCache({}) - } + state = { + loopCache: new LoopCache({}) + } - constructor(props) { - super(props); - this.renderStatus = this.renderStatus.bind(this); - this.state.loopCache = props.loopCache; - } + constructor(props) { + super(props); + this.renderStatus = this.renderStatus.bind(this); + this.state.loopCache = props.loopCache; + } - renderStatus() { - let componentStates = this.state.loopCache.getComponentStates(); - if (componentStates != null) { - return Object.keys(componentStates).map((key) => { - console.debug("Adding status for: ",key); - var res={} - res[key]=this.state.loopCache.getComponentStates()[key]; - return (<TableRow key={key} statusRow={{'componentName':key,'stateName':this.state.loopCache.getComponentStates()[key].componentState.stateName,'description':this.state.loopCache.getComponentStates()[key].componentState.description}} />) - }) + renderStatus() { + let componentStates = this.state.loopCache.getComponentStates(); + if (componentStates != null) { + return Object.keys(componentStates).map((key) => { + console.debug("Adding status for: ", key); + var res = {} + res[key] = this.state.loopCache.getComponentStates()[key]; + return (<TableRow key={ key } statusRow={ { + 'componentName': key, + 'stateName': this.state.loopCache.getComponentStates()[key].componentState.stateName, + 'description': this.state.loopCache.getComponentStates()[key].componentState.description + } }/>) + }) - } - } + } + } - shouldComponentUpdate(nextProps, nextState) { - return this.state.loopCache !== nextState.loopCache; - } + shouldComponentUpdate(nextProps, nextState) { + return this.state.loopCache !== nextState.loopCache; + } - componentWillReceiveProps(newProps) { - this.setState({ - loopCache: newProps.loopCache - }); - } + componentWillReceiveProps(newProps) { + this.setState({ + loopCache: newProps.loopCache + }); + } - render() { - return ( - <LoopStatusViewDivStyled> - <label>Loop Status: {this.state.loopCache.getComputedState()} - </label> + render() { + return ( + <LoopStatusViewDivStyled> + <label>Loop Status: { this.state.loopCache.getComputedState() } + </label> - <div > - <TableStyled striped hover variant responsive> - <thead> - <tr> - <th><span align="left">Component Name</span></th> - <th><span align="left">Component State</span></th> - <th><span align="right">Description</span></th> - </tr> - </thead> - <tbody> - {this.renderStatus()} - </tbody> - </TableStyled> - </div> - </LoopStatusViewDivStyled> - ); - } + <div> + <TableStyled striped hover variant responsive> + <thead> + <tr> + <th><span align="left">Component Name</span></th> + <th><span align="left">Component State</span></th> + <th><span align="right">Description</span></th> + </tr> + </thead> + <tbody> + { this.renderStatus() } + </tbody> + </TableStyled> + </div> + </LoopStatusViewDivStyled> + ); + } } diff --git a/runtime/ui-react/src/components/loop_viewer/status/LoopStatus.test.js b/runtime/ui-react/src/components/loop_viewer/status/LoopStatus.test.js index 8d0448796..b84067e18 100644 --- a/runtime/ui-react/src/components/loop_viewer/status/LoopStatus.test.js +++ b/runtime/ui-react/src/components/loop_viewer/status/LoopStatus.test.js @@ -27,52 +27,52 @@ import LoopCache from '../../../api/LoopCache'; describe('Verify LoopStatus', () => { - const loopCache = new LoopCache({ - "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", - "lastComputedState": "DESIGN", - "components": { - "POLICY": { - "componentState": { - "stateName": "NOT_SENT", - "description": "The policies defined have NOT yet been created on the policy engine" - } - }, - "DCAE": { - "componentState": { - "stateName": "BLUEPRINT_DEPLOYED", - "description": "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop" - } - } - } - }); + const loopCache = new LoopCache({ + "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", + "lastComputedState": "DESIGN", + "components": { + "POLICY": { + "componentState": { + "stateName": "NOT_SENT", + "description": "The policies defined have NOT yet been created on the policy engine" + } + }, + "DCAE": { + "componentState": { + "stateName": "BLUEPRINT_DEPLOYED", + "description": "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop" + } + } + } + }); - it('Test the render method', () => { - const component = shallow(<LoopStatus loopCache={loopCache}/>) + it('Test the render method', () => { + const component = shallow(<LoopStatus loopCache={ loopCache }/>) - expect(component).toMatchSnapshot(); - - const loopCacheUpdated = new LoopCache({ - "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", - "lastComputedState": "SUBMIT", - "components": { - "POLICY": { - "componentState": { - "stateName": "SENT", - "description": "The policies defined have NOT yet been created on the policy engine" - } - }, - "DCAE": { - "componentState": { - "stateName": "BLUEPRINT_DEPLOYED", - "description": "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop" - } - } - } - }); - component.setProps({ loopCache: loopCacheUpdated }); + expect(component).toMatchSnapshot(); - const forms = component.find('TableRow'); - expect(forms.get(0).props.statusRow.stateName).toEqual("SENT"); - expect(component.find('label').text()).toContain('SUBMIT'); - }); -});
\ No newline at end of file + const loopCacheUpdated = new LoopCache({ + "name": "LOOP_Jbv1z_v1_0_ResourceInstanceName1_tca", + "lastComputedState": "SUBMIT", + "components": { + "POLICY": { + "componentState": { + "stateName": "SENT", + "description": "The policies defined have NOT yet been created on the policy engine" + } + }, + "DCAE": { + "componentState": { + "stateName": "BLUEPRINT_DEPLOYED", + "description": "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop" + } + } + } + }); + component.setProps({ loopCache: loopCacheUpdated }); + + const forms = component.find('TableRow'); + expect(forms.get(0).props.statusRow.stateName).toEqual("SENT"); + expect(component.find('label').text()).toContain('SUBMIT'); + }); +}); diff --git a/runtime/ui-react/src/components/menu/MenuBar.test.js b/runtime/ui-react/src/components/menu/MenuBar.test.js index 3e96dbf38..81117bb90 100644 --- a/runtime/ui-react/src/components/menu/MenuBar.test.js +++ b/runtime/ui-react/src/components/menu/MenuBar.test.js @@ -26,21 +26,21 @@ import MenuBar from './MenuBar'; describe('Verify MenuBar', () => { - it('Test the render method', () => { - const component = shallow(<MenuBar />) + it('Test the render method', () => { + const component = shallow(<MenuBar/>) - expect(component).toMatchSnapshot(); - }); + expect(component).toMatchSnapshot(); + }); - it('Update loopName', () => { - const component = shallow(<MenuBar />) - component.setProps({ loopName: "newLoop" }); - expect(component.state('disabled')).toBe(false); - }); + it('Update loopName', () => { + const component = shallow(<MenuBar/>) + component.setProps({ loopName: "newLoop" }); + expect(component.state('disabled')).toBe(false); + }); - it('Default loopName', () => { - const component = shallow(<MenuBar />) - component.setProps({ loopName: "Empty (NO loop loaded yet)" }); - expect(component.state('disabled')).toBe(true); - }); + it('Default loopName', () => { + const component = shallow(<MenuBar/>) + component.setProps({ loopName: "Empty (NO loop loaded yet)" }); + expect(component.state('disabled')).toBe(true); + }); }); diff --git a/runtime/ui-react/src/index.js b/runtime/ui-react/src/index.js index dd83096ea..9baeaeec6 100644 --- a/runtime/ui-react/src/index.js +++ b/runtime/ui-react/src/index.js @@ -27,12 +27,12 @@ import { Route, MemoryRouter } from 'react-router-dom' const routing = ( - <MemoryRouter forceRefresh={false}> - <Route path="/" component={OnapClamp}/> - </MemoryRouter> + <MemoryRouter forceRefresh={ false }> + <Route path="/" component={ OnapClamp }/> + </MemoryRouter> ); export var mainClamp = ReactDOM.render( - routing, - document.getElementById('root') + routing, + document.getElementById('root') ) diff --git a/runtime/ui-react/src/theme/globalStyle.js b/runtime/ui-react/src/theme/globalStyle.js index 1e316d20b..5245aae19 100644 --- a/runtime/ui-react/src/theme/globalStyle.js +++ b/runtime/ui-react/src/theme/globalStyle.js @@ -24,75 +24,75 @@ import { createGlobalStyle } from 'styled-components'; export const GlobalClampStyle = createGlobalStyle` - body { - padding: 0; - margin: 0; - font-family: ${props => props.theme.fontFamily}; - font-size: ${props => props.theme.fontSize}; - font-weight: normal; - } + body { + padding: 0; + margin: 0; + font-family: ${ props => props.theme.fontFamily }; + font-size: ${ props => props.theme.fontSize }; + font-weight: normal; + } - span { - font-family: ${props => props.theme.fontFamily}; - font-size: ${props => props.theme.fontSize}; - font-weight: bold; - } + span { + font-family: ${ props => props.theme.fontFamily }; + font-size: ${ props => props.theme.fontSize }; + font-weight: bold; + } - a { - font-family: ${props => props.theme.fontFamily}; - font-size: ${props => props.theme.fontSize}; - font-weight: bold; - } + a { + font-family: ${ props => props.theme.fontFamily }; + font-size: ${ props => props.theme.fontSize }; + font-weight: bold; + } - div { - font-family: ${props => props.theme.fontFamily}; - font-size: ${props => props.theme.fontSize}; - border-radius: 4px; - margin-top: 1px; - } + div { + font-family: ${ props => props.theme.fontFamily }; + font-size: ${ props => props.theme.fontSize }; + border-radius: 4px; + margin-top: 1px; + } - label { - font-family: ${props => props.theme.fontFamily}; - font-size: ${props => props.theme.fontSize}; - font-weight: bold; - } + label { + font-family: ${ props => props.theme.fontFamily }; + font-size: ${ props => props.theme.fontSize }; + font-weight: bold; + } - button { - font-family: ${props => props.theme.fontFamily}; - font-size: ${props => props.theme.fontSize}; - font-weight: bold; - } + button { + font-family: ${ props => props.theme.fontFamily }; + font-size: ${ props => props.theme.fontSize }; + font-weight: bold; + } ` export const DefaultClampTheme = { - fontDanger: '#eb238e', - fontWarning: '#eb238e', - fontLight: '#ffffff', - fontDark: '#888888', - fontHighlight: '#ffff00', - fontNormal: 'black', + fontDanger: '#eb238e', + fontWarning: '#eb238e', + fontLight: '#ffffff', + fontDark: '#888888', + fontHighlight: '#ffff00', + fontNormal: 'black', - backgroundColor: '#eeeeee', - fontFamily: 'Arial, Sans-serif', - fontSize: '16px', + backgroundColor: '#eeeeee', + fontFamily: 'Arial, Sans-serif', + fontSize: '16px', - loopViewerBackgroundColor: 'white', - loopViewerFontColor: 'yellow', - loopViewerHeaderBackgroundColor: '#337ab7', - loopViewerHeaderFontColor: 'white', + loopViewerBackgroundColor: 'white', + loopViewerFontColor: 'yellow', + loopViewerHeaderBackgroundColor: '#337ab7', + loopViewerHeaderFontColor: 'white', - loopLogsHeaderBackgroundColor: 'white', - loopLogsHeaderFontColor: 'black', + loopLogsHeaderBackgroundColor: 'white', + loopLogsHeaderFontColor: 'black', - menuBackgroundColor: 'white', - menuFontColor: 'black', - menuHighlightedBackgroundColor: '#337ab7', - menuHighlightedFontColor: 'white', + menuBackgroundColor: 'white', + menuFontColor: 'black', + menuHighlightedBackgroundColor: '#337ab7', + menuHighlightedFontColor: 'white', - toscaTextareaBackgroundColor: 'white', - toscaTextareaFontSize: '13px', + toscaTextareaBackgroundColor: 'white', + toscaTextareaFontSize: '13px', - policyEditorBackgroundColor: 'white', - policyEditorFontSize: '13px' + policyEditorBackgroundColor: 'white', + policyEditorFontSize: '13px' }; diff --git a/runtime/ui-react/src/utils/CsvToJson.js b/runtime/ui-react/src/utils/CsvToJson.js index 5ec19c9e2..6d95b3119 100644 --- a/runtime/ui-react/src/utils/CsvToJson.js +++ b/runtime/ui-react/src/utils/CsvToJson.js @@ -22,183 +22,183 @@ export default function CsvToJson(rawCsvData, delimiter, internalDelimiter, csvHeaderNames, jsonKeyNames, mandatory) { - let printDictKeys = ''; - let result = { jsonObjArray: [], errorMessages: '' }; - - // Validate that all parallel arrays passed in have same number of elements; - // this would be a developer error. - - let checkLength = csvHeaderNames.length; - - if (checkLength !== jsonKeyNames.length || checkLength !== mandatory.length) { - result.errorMessages = 'interanl error: csvHeaderNames, jsonKeyNames, and mandatory arrays parameters are not the same length'; - return result; - } - - if (checkLength < 1) { - result.errorMessages = 'interanl error: csvHeaderNames, jsonKeyNames, and mandatory arrays have no entries'; - return result; - } - - // Make a nice string to print in the error case to tell user what is the - // required heaer row format - - for (let i=0; i < csvHeaderNames.length; ++i) { - if (i === 0) { - printDictKeys = csvHeaderNames[i]; - } else { - printDictKeys += ',' + csvHeaderNames[i]; - } - } - - let dictElems = rawCsvData.split('\n'); - let numColumns = 0; - let filteredDictElems = []; - - // The task of the following loop is to convert raw CSV rows into easily parseable - // and streamlined versions of the rows with an internalDelimiter replacing the standard - // comma; it is presumed (and checked) that the internalDelimiter cannot exist as a valid - // sequence of characters in the user's data. - - // This conversion process also strips leading and trailing whitespace from each row, - // discards empty rows, correctly interprets and removes all double quotes that programs like - // Excel use to support user columns that contain special characters, most notably, the comma - // delimiter. A double-quote that is contained within a double-quoted column value - // must appear in this raw data as a sequence of two double quotes. Furthermore, any column - // value in the raw CSV data that does not contain a delimiter may or may not be enclosed in - // double quotes. It is the Excel convention to not use double qoutes unless necessary, and - // there is no reasonable way to tell Excel to surround every column value with double quotes. - // Any files that were directly "exported" by CLAMP itself from the Managing Dictionaries - // capability, surround all columns with double quotes. - - for (let i = 0; i < dictElems.length; i++) { - - let oneRow = dictElems[i].trim(); - let j = 0; - let inQuote = false - let nextChar = undefined; - let prevChar = null; - - - if (oneRow === '') { - continue; // Skip blank rows - } else if (oneRow.indexOf(internalDelimiter) !== -1) { - result.errorMessages += '\nRow #' + i + ' contains illegal sequence of characters (' + internalDelimiter + ')'; - break; - } else { - nextChar = oneRow[1]; - } - - let newStr = ''; - numColumns = 1; - - // This "while loop" performs the very meticulous task of removing double quotes that - // are used by Excel to encase special characters as user string value data, - // and manages to correctly identify columns that are defined with or without - // double quotes and to process the comma delimiter correctly when encountered - // as a user value within a column. Such a column would have to be encased in - // double quotes; a comma found outside double quotes IS a delimiter. - - while (j < oneRow.length) { - if (oneRow[j] === '"') { - if (inQuote === false) { - if (prevChar !== delimiter && prevChar !== null) { - result.errorMessages += '\nMismatched double quotes or illegal whitespace around delimiter at row #' + (i + 1) + ' near column #' + numColumns; - break; - } else { - inQuote = true; - } - } else { - if (nextChar === '"') { - newStr += '"'; - ++j; - } else if ((nextChar !== delimiter) && (nextChar !== undefined)) { - result.errorMessages += '\nRow #' + (i + 1) + ' is badly formatted at column #' + numColumns + '. Perhaps an unescaped double quote.'; - break; - } else if (nextChar === delimiter) { - ++numColumns; - inQuote = false; - newStr += internalDelimiter; - prevChar = delimiter; - j += 2; - nextChar = oneRow[j+1]; - continue; - } else { - ++numColumns; - inQuote = false; - break; - } - } - } else { - if (oneRow[j] === delimiter && inQuote === false) { - newStr += internalDelimiter; - ++numColumns; - } else { - newStr += oneRow[j]; - } - } - prevChar = oneRow[j]; - ++j; - nextChar = oneRow[j+1]; // can result in undefined at the end - } - - if (result.errorMessages === '' && inQuote !== false) { - result.errorMessages += '\nMismatched double quotes at row #' + (i + 1); - break; - } else if (result.errorMessages === '' && numColumns < jsonKeyNames.length) { - result.errorMessages += '\nNot enough columns (' + jsonKeyNames.length + ') at row #' + (i + 1); - break; - } - - filteredDictElems.push(newStr); - } - - if (result.errorMessages !== '') { - return result; - } - - // Perform further checks on data that is now in JSON form - if (filteredDictElems.length < 2) { - result.errorMessages += '\nNot enough row data found in import file. Need at least a header row and one row of data'; - return result; - } - - // Now that we have something reliably parsed into sanitized columns lets run some checks - // and convert it all into an array of JSON objects to push to the back end if all the - // checks pass. - - let headers = filteredDictElems[0].split(internalDelimiter); - - // check that headers are included in proper order - for (let i=0; i < jsonKeyNames.length; ++i) { - if (csvHeaderNames[i] !== headers[i]) { - result.errorMessages += 'Row 1 header key at column #' + (i + 1) + ' is a mismatch. Expected row header must contain at least:\n' + printDictKeys; - return result; - } - } - - // Convert the ASCII rows of data into an array of JSON obects that omit the header - // row which is not sent to the back end. - - for (let i = 1; i < filteredDictElems.length; i++) { - let data = filteredDictElems[i].split(internalDelimiter); - let obj = {}; - for (let j = 0; j < data.length && j < jsonKeyNames.length; j++) { - let value = data[j].trim(); - if (mandatory[j] === true && value === '') { - result.errorMessages += '\n' + csvHeaderNames[j] + ' at row #' + (i+1) + ' is empty but requires a value.'; - } - obj[jsonKeyNames[j]] = value; - } - result.jsonObjArray.push(obj); - } - - if (result.errorMessages !== '') { - // If we have errors, return empty parse result even though some things - // may have parsed properly. We do not want to encourage the caller - // to think the data is good for use. - result.jsonObjArray = []; - } - - return result; + let printDictKeys = ''; + let result = { jsonObjArray: [], errorMessages: '' }; + + // Validate that all parallel arrays passed in have same number of elements; + // this would be a developer error. + + let checkLength = csvHeaderNames.length; + + if (checkLength !== jsonKeyNames.length || checkLength !== mandatory.length) { + result.errorMessages = 'interanl error: csvHeaderNames, jsonKeyNames, and mandatory arrays parameters are not the same length'; + return result; + } + + if (checkLength < 1) { + result.errorMessages = 'interanl error: csvHeaderNames, jsonKeyNames, and mandatory arrays have no entries'; + return result; + } + + // Make a nice string to print in the error case to tell user what is the + // required heaer row format + + for (let i = 0; i < csvHeaderNames.length; ++i) { + if (i === 0) { + printDictKeys = csvHeaderNames[i]; + } else { + printDictKeys += ',' + csvHeaderNames[i]; + } + } + + let dictElems = rawCsvData.split('\n'); + let numColumns = 0; + let filteredDictElems = []; + + // The task of the following loop is to convert raw CSV rows into easily parseable + // and streamlined versions of the rows with an internalDelimiter replacing the standard + // comma; it is presumed (and checked) that the internalDelimiter cannot exist as a valid + // sequence of characters in the user's data. + + // This conversion process also strips leading and trailing whitespace from each row, + // discards empty rows, correctly interprets and removes all double quotes that programs like + // Excel use to support user columns that contain special characters, most notably, the comma + // delimiter. A double-quote that is contained within a double-quoted column value + // must appear in this raw data as a sequence of two double quotes. Furthermore, any column + // value in the raw CSV data that does not contain a delimiter may or may not be enclosed in + // double quotes. It is the Excel convention to not use double qoutes unless necessary, and + // there is no reasonable way to tell Excel to surround every column value with double quotes. + // Any files that were directly "exported" by CLAMP itself from the Managing Dictionaries + // capability, surround all columns with double quotes. + + for (let i = 0; i < dictElems.length; i++) { + + let oneRow = dictElems[i].trim(); + let j = 0; + let inQuote = false + let nextChar = undefined; + let prevChar = null; + + + if (oneRow === '') { + continue; // Skip blank rows + } else if (oneRow.indexOf(internalDelimiter) !== -1) { + result.errorMessages += '\nRow #' + i + ' contains illegal sequence of characters (' + internalDelimiter + ')'; + break; + } else { + nextChar = oneRow[1]; + } + + let newStr = ''; + numColumns = 1; + + // This "while loop" performs the very meticulous task of removing double quotes that + // are used by Excel to encase special characters as user string value data, + // and manages to correctly identify columns that are defined with or without + // double quotes and to process the comma delimiter correctly when encountered + // as a user value within a column. Such a column would have to be encased in + // double quotes; a comma found outside double quotes IS a delimiter. + + while (j < oneRow.length) { + if (oneRow[j] === '"') { + if (inQuote === false) { + if (prevChar !== delimiter && prevChar !== null) { + result.errorMessages += '\nMismatched double quotes or illegal whitespace around delimiter at row #' + (i + 1) + ' near column #' + numColumns; + break; + } else { + inQuote = true; + } + } else { + if (nextChar === '"') { + newStr += '"'; + ++j; + } else if ((nextChar !== delimiter) && (nextChar !== undefined)) { + result.errorMessages += '\nRow #' + (i + 1) + ' is badly formatted at column #' + numColumns + '. Perhaps an unescaped double quote.'; + break; + } else if (nextChar === delimiter) { + ++numColumns; + inQuote = false; + newStr += internalDelimiter; + prevChar = delimiter; + j += 2; + nextChar = oneRow[j + 1]; + continue; + } else { + ++numColumns; + inQuote = false; + break; + } + } + } else { + if (oneRow[j] === delimiter && inQuote === false) { + newStr += internalDelimiter; + ++numColumns; + } else { + newStr += oneRow[j]; + } + } + prevChar = oneRow[j]; + ++j; + nextChar = oneRow[j + 1]; // can result in undefined at the end + } + + if (result.errorMessages === '' && inQuote !== false) { + result.errorMessages += '\nMismatched double quotes at row #' + (i + 1); + break; + } else if (result.errorMessages === '' && numColumns < jsonKeyNames.length) { + result.errorMessages += '\nNot enough columns (' + jsonKeyNames.length + ') at row #' + (i + 1); + break; + } + + filteredDictElems.push(newStr); + } + + if (result.errorMessages !== '') { + return result; + } + + // Perform further checks on data that is now in JSON form + if (filteredDictElems.length < 2) { + result.errorMessages += '\nNot enough row data found in import file. Need at least a header row and one row of data'; + return result; + } + + // Now that we have something reliably parsed into sanitized columns lets run some checks + // and convert it all into an array of JSON objects to push to the back end if all the + // checks pass. + + let headers = filteredDictElems[0].split(internalDelimiter); + + // check that headers are included in proper order + for (let i = 0; i < jsonKeyNames.length; ++i) { + if (csvHeaderNames[i] !== headers[i]) { + result.errorMessages += 'Row 1 header key at column #' + (i + 1) + ' is a mismatch. Expected row header must contain at least:\n' + printDictKeys; + return result; + } + } + + // Convert the ASCII rows of data into an array of JSON obects that omit the header + // row which is not sent to the back end. + + for (let i = 1; i < filteredDictElems.length; i++) { + let data = filteredDictElems[i].split(internalDelimiter); + let obj = {}; + for (let j = 0; j < data.length && j < jsonKeyNames.length; j++) { + let value = data[j].trim(); + if (mandatory[j] === true && value === '') { + result.errorMessages += '\n' + csvHeaderNames[j] + ' at row #' + (i + 1) + ' is empty but requires a value.'; + } + obj[jsonKeyNames[j]] = value; + } + result.jsonObjArray.push(obj); + } + + if (result.errorMessages !== '') { + // If we have errors, return empty parse result even though some things + // may have parsed properly. We do not want to encourage the caller + // to think the data is good for use. + result.jsonObjArray = []; + } + + return result; } diff --git a/runtime/ui-react/src/utils/CsvToJson.test.js b/runtime/ui-react/src/utils/CsvToJson.test.js index 88fa7a472..e36e85554 100644 --- a/runtime/ui-react/src/utils/CsvToJson.test.js +++ b/runtime/ui-react/src/utils/CsvToJson.test.js @@ -25,244 +25,244 @@ import CsvToJson from './CsvToJson' describe('Verify CsvToJson', () => { - const hdrNames= [ - "Element Short Name", - "Element Name", - "Element Description", - "Element Type", - "Sub-Dictionary" - ]; - - const jsonKeyNames = [ - "shortName", - "name", - "description", - "type", - "subDictionary" - ]; - - const mandatory = [ true, true, true, true, false ]; - - it('Test CsvToJson No Error Case, Quoted Columns', () => { - - let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; - rawCsv += '"alertType","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; - - let expectedResult = { - errorMessages: '', - jsonObjArray: [ - { - description: "Type of Alert", - name: "Alert Type", - shortName: "alertType", - subDictionary: "", - type: "string" - } - ] - }; - - expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); - }); - - it('Test CsvToJson No Error Case, Unquoted Columns', () => { - - let rawCsv = 'Element Short Name,Element Name,Element Description,Element Type,Sub-Dictionary\n'; - rawCsv += 'alertType,Alert Type,Type of Alert,string,,admin,2020-06-11T13:56:14.927437Z'; - - let expectedResult = { - errorMessages: '', - jsonObjArray: [ - { - description: "Type of Alert", - name: "Alert Type", - shortName: "alertType", - subDictionary: "", - type: "string" - } - ] - }; - - expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); - }); - - it('Test CsvToJson Properly Escaped Double Quote and Delimiter', () => { - - let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; - rawCsv += '"alertType","Alert ""Type""","Type of Alert, Varies","string","","admin","2020-06-11T13:56:14.927437Z"'; - - let errorMessage = ''; - - let expectedResult = { - errorMessages: errorMessage, - jsonObjArray: [ - { - description: "Type of Alert, Varies", - name: 'Alert "Type"', - shortName: 'alertType', - subDictionary: "", - type: "string", - } - - ] - }; - - expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); - }); - - - it('Test CsvToJson Error Header Mismatch Error Case', () => { - - let rawCsv = '"Element Short Names","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; - rawCsv += '"alertType","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; - - let errorMessage = 'Row 1 header key at column #1 is a mismatch. Expected row header must contain at least:\n'; - errorMessage += 'Element Short Name,Element Name,Element Description,Element Type,Sub-Dictionary'; - - let expectedResult = { - errorMessages: errorMessage, - jsonObjArray: [] - }; - - expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); - }); - - it('Test CsvToJson Error Mismatched Double Quotes in Column', () => { - - let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; - rawCsv += '"alert"Type","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; - - let errorMessage = '\nRow #2 is badly formatted at column #1. Perhaps an unescaped double quote.' - - let expectedResult = { - errorMessages: errorMessage, - jsonObjArray: [] - }; - - expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); - }); - - it('Test CsvToJson Error Illegal Whitespace', () => { - - let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; - rawCsv += 'alertType , "Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; - - let errorMessage = '\nMismatched double quotes or illegal whitespace around delimiter at row #2 near column #2'; - - let expectedResult = { - errorMessages: errorMessage, - jsonObjArray: [] - }; - - expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); - }); - - it('Test CsvToJson Error Too Few Data Columns', () => { - - let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; - rawCsv += '"alertType","Alert Type","Type of Alert"'; - - let errorMessage = '\nNot enough columns (5) at row #2'; - - let expectedResult = { - errorMessages: errorMessage, - jsonObjArray: [] - }; - - expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); - }); - - it('Test CsvToJson Error Wrong Header Column Order', () => { - - let rawCsv = '"Element Name","Element Short Name","Element Description","Element Type","Sub-Dictionary"\n'; - rawCsv += '"alertType","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; - - let errorMessage = 'Row 1 header key at column #1 is a mismatch. Expected row header must contain at least:\n'; - errorMessage += 'Element Short Name,Element Name,Element Description,Element Type,Sub-Dictionary'; - - let expectedResult = { - errorMessages: errorMessage, - jsonObjArray: [] - }; - - expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); - }); - - it('Test CsvToJson Error Not Enough Rows', () => { - - let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; - - let errorMessage = '\nNot enough row data found in import file. Need at least a header row and one row of data'; - - let expectedResult = { - errorMessages: errorMessage, - jsonObjArray: [] - }; - - expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); - }); - - it('Test CsvToJson Error Mandatory Field Is Empty', () => { - - let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; - rawCsv += '"","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; - - let expectedResult = { - errorMessages: '\nElement Short Name at row #2 is empty but requires a value.', - jsonObjArray: [] - }; - - expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); - }); - - it('Test CsvToJson Error Mismatched Double Quotes At End', () => { - - let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; - rawCsv += '"alertType","Alert Type","Alert Type Description","string","admin","2020-06-11T13:56:14.927437Z'; - - let expectedResult = { - errorMessages: '\nMismatched double quotes at row #2', - jsonObjArray: [] - }; - - expect(CsvToJson(rawCsv, ',', '||', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); - }); - - it('Test CsvToJson Error Mismatched Mandatory Array Parameters', () => { - - let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; - rawCsv += '"alertType","Alert Type","Alert Type Description","string","admin","2020-06-11T13:56:14.927437Z'; - - let expectedResult = { - errorMessages: 'interanl error: csvHeaderNames, jsonKeyNames, and mandatory arrays parameters are not the same length', - jsonObjArray: [] - }; - - expect(CsvToJson(rawCsv, ',', '||', hdrNames, jsonKeyNames, [ true ])).toEqual(expectedResult); - }); - - it('Test CsvToJson Error Empty Mandatory Array Parameters', () => { - - let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; - rawCsv += '"alertType","Alert Type","Alert Type Description","string","admin","2020-06-11T13:56:14.927437Z'; - - let expectedResult = { - errorMessages: 'interanl error: csvHeaderNames, jsonKeyNames, and mandatory arrays have no entries', - jsonObjArray: [] - }; - - expect(CsvToJson(rawCsv, ',', '||', [], [], [])).toEqual(expectedResult); - }); - - it('Test CsvToJson Error Illegal Data Contains Internal Delimiter', () => { - - let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; - rawCsv += '"alertType","Alert Type","Alert Type||Description","string","admin","2020-06-11T13:56:14.927437Z'; - - let expectedResult = { - errorMessages: '\nRow #1 contains illegal sequence of characters (||)', - jsonObjArray: [] - }; - - expect(CsvToJson(rawCsv, ',', '||', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); - }); + const hdrNames = [ + "Element Short Name", + "Element Name", + "Element Description", + "Element Type", + "Sub-Dictionary" + ]; + + const jsonKeyNames = [ + "shortName", + "name", + "description", + "type", + "subDictionary" + ]; + + const mandatory = [true, true, true, true, false]; + + it('Test CsvToJson No Error Case, Quoted Columns', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; + + let expectedResult = { + errorMessages: '', + jsonObjArray: [ + { + description: "Type of Alert", + name: "Alert Type", + shortName: "alertType", + subDictionary: "", + type: "string" + } + ] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson No Error Case, Unquoted Columns', () => { + + let rawCsv = 'Element Short Name,Element Name,Element Description,Element Type,Sub-Dictionary\n'; + rawCsv += 'alertType,Alert Type,Type of Alert,string,,admin,2020-06-11T13:56:14.927437Z'; + + let expectedResult = { + errorMessages: '', + jsonObjArray: [ + { + description: "Type of Alert", + name: "Alert Type", + shortName: "alertType", + subDictionary: "", + type: "string" + } + ] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Properly Escaped Double Quote and Delimiter', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert ""Type""","Type of Alert, Varies","string","","admin","2020-06-11T13:56:14.927437Z"'; + + let errorMessage = ''; + + let expectedResult = { + errorMessages: errorMessage, + jsonObjArray: [ + { + description: "Type of Alert, Varies", + name: 'Alert "Type"', + shortName: 'alertType', + subDictionary: "", + type: "string", + } + + ] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + + it('Test CsvToJson Error Header Mismatch Error Case', () => { + + let rawCsv = '"Element Short Names","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; + + let errorMessage = 'Row 1 header key at column #1 is a mismatch. Expected row header must contain at least:\n'; + errorMessage += 'Element Short Name,Element Name,Element Description,Element Type,Sub-Dictionary'; + + let expectedResult = { + errorMessages: errorMessage, + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Mismatched Double Quotes in Column', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alert"Type","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; + + let errorMessage = '\nRow #2 is badly formatted at column #1. Perhaps an unescaped double quote.' + + let expectedResult = { + errorMessages: errorMessage, + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Illegal Whitespace', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += 'alertType , "Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; + + let errorMessage = '\nMismatched double quotes or illegal whitespace around delimiter at row #2 near column #2'; + + let expectedResult = { + errorMessages: errorMessage, + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Too Few Data Columns', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert Type","Type of Alert"'; + + let errorMessage = '\nNot enough columns (5) at row #2'; + + let expectedResult = { + errorMessages: errorMessage, + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Wrong Header Column Order', () => { + + let rawCsv = '"Element Name","Element Short Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; + + let errorMessage = 'Row 1 header key at column #1 is a mismatch. Expected row header must contain at least:\n'; + errorMessage += 'Element Short Name,Element Name,Element Description,Element Type,Sub-Dictionary'; + + let expectedResult = { + errorMessages: errorMessage, + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Not Enough Rows', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + + let errorMessage = '\nNot enough row data found in import file. Need at least a header row and one row of data'; + + let expectedResult = { + errorMessages: errorMessage, + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Mandatory Field Is Empty', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"'; + + let expectedResult = { + errorMessages: '\nElement Short Name at row #2 is empty but requires a value.', + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Mismatched Double Quotes At End', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert Type","Alert Type Description","string","admin","2020-06-11T13:56:14.927437Z'; + + let expectedResult = { + errorMessages: '\nMismatched double quotes at row #2', + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '||', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Mismatched Mandatory Array Parameters', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert Type","Alert Type Description","string","admin","2020-06-11T13:56:14.927437Z'; + + let expectedResult = { + errorMessages: 'interanl error: csvHeaderNames, jsonKeyNames, and mandatory arrays parameters are not the same length', + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '||', hdrNames, jsonKeyNames, [true])).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Empty Mandatory Array Parameters', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert Type","Alert Type Description","string","admin","2020-06-11T13:56:14.927437Z'; + + let expectedResult = { + errorMessages: 'interanl error: csvHeaderNames, jsonKeyNames, and mandatory arrays have no entries', + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '||', [], [], [])).toEqual(expectedResult); + }); + + it('Test CsvToJson Error Illegal Data Contains Internal Delimiter', () => { + + let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n'; + rawCsv += '"alertType","Alert Type","Alert Type||Description","string","admin","2020-06-11T13:56:14.927437Z'; + + let expectedResult = { + errorMessages: '\nRow #1 contains illegal sequence of characters (||)', + jsonObjArray: [] + }; + + expect(CsvToJson(rawCsv, ',', '||', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult); + }); }) diff --git a/runtime/ui-react/src/utils/OnapConstants.js b/runtime/ui-react/src/utils/OnapConstants.js index 8460340d1..c5b96d4c2 100644 --- a/runtime/ui-react/src/utils/OnapConstants.js +++ b/runtime/ui-react/src/utils/OnapConstants.js @@ -24,9 +24,9 @@ // Maintain a list of ONAP CLAMP UI "constants" that can be used by any componenet within CLAMP const OnapConstants = { - defaultLoopName: "Empty (NO loop loaded yet)", - microServiceType: "MICRO-SERVICE-POLICY", - operationalPolicyType: "OPERATIONAL_POLICY_TYPE" + defaultLoopName: "Empty (NO loop loaded yet)", + microServiceType: "MICRO-SERVICE-POLICY", + operationalPolicyType: "OPERATIONAL_POLICY_TYPE" }; export default OnapConstants; diff --git a/runtime/ui-react/src/utils/OnapUtils.js b/runtime/ui-react/src/utils/OnapUtils.js index 316a0d65f..9ba938240 100644 --- a/runtime/ui-react/src/utils/OnapUtils.js +++ b/runtime/ui-react/src/utils/OnapUtils.js @@ -23,43 +23,43 @@ export default class OnapUtils { - constructor() { - this.clickBlocked = false; - } + constructor() { + this.clickBlocked = false; + } - static jsonEditorErrorFormatter(errors) { + static jsonEditorErrorFormatter(errors) { - let messages = []; - let messagesOutputString = null; + let messages = []; + let messagesOutputString = null; - // errors is an array of JSON Editor "error" objects, where each - // object looks like this: + // errors is an array of JSON Editor "error" objects, where each + // object looks like this: - // { - // message: "Please populate the required property "Threshold"" - // path: "root.signatures.0" - // property: "required" - // } + // { + // message: "Please populate the required property "Threshold"" + // path: "root.signatures.0" + // property: "required" + // } - // In this function we concatenate all the messages, removing any duplicates, - // and adding a newline between each message. The result returned is a single - // string that can be displayed to the user in an alert message + // In this function we concatenate all the messages, removing any duplicates, + // and adding a newline between each message. The result returned is a single + // string that can be displayed to the user in an alert message - if (!Array.isArray(errors)) { - console.error('jsoneEditorErrorFormatter was passed a non-array argument'); - } else { - for (let ii=0; ii < errors.length; ++ii) { - if (!messages.includes(errors[ii].message)) { - messages.push(errors[ii].message); - if (messagesOutputString) { - messagesOutputString += '\n' + errors[ii].message; - } else { - messagesOutputString = errors[ii].message; - } - } - } - } + if (!Array.isArray(errors)) { + console.error('jsoneEditorErrorFormatter was passed a non-array argument'); + } else { + for (let ii = 0; ii < errors.length; ++ii) { + if (!messages.includes(errors[ii].message)) { + messages.push(errors[ii].message); + if (messagesOutputString) { + messagesOutputString += '\n' + errors[ii].message; + } else { + messagesOutputString = errors[ii].message; + } + } + } + } - return messagesOutputString; - } + return messagesOutputString; + } } |