Add unit test cases for UI components
[sdc.git] / catalog-ui / src / app / ng2 / pipes / entity-filter.pipe.ts
index 29b21cb..e1b9521 100644 (file)
-/*-
- * ============LICENSE_START=======================================================
- * SDC
- * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-import {Pipe, PipeTransform} from "@angular/core";
-import {Component, Resource} from "app/models";
-import {ComponentType, DEFAULT_MODEL_NAME} from "app/utils/constants";
-
-export interface ISearchFilter {
-    [key:string]: string;
-}
-
-export interface IEntityFilterObject {
-    // Types
-    selectedComponentTypes?:Array<string>;
-    selectedResourceSubTypes?:Array<string>;
-    // Categories
-    selectedCategoriesModel?:Array<string>;
-    // Statuses
-    selectedStatuses?:Array<string>;
-    // Models
-    selectedModels?:Array<string>;
-    // distributed
-    distributed?:Array<string>;
-    // search
-    search?:ISearchFilter;
-    
-}
-
-@Pipe({name: 'entity-filter'})
-export class EntityFilterPipe implements PipeTransform{
-    constructor() {
-    }
-
-    public static transform(components:Array<Component>, filter:IEntityFilterObject) {
-        let filteredComponents:Array<Component> = components;
-
-        // filter by type
-        // --------------------------------------------------------------------------
-        if ((filter.selectedComponentTypes && filter.selectedComponentTypes.length > 0) || (filter.selectedResourceSubTypes && filter.selectedResourceSubTypes.length > 0)) {
-            let filteredTypes = [];
-            angular.forEach(components, (component:Component):void => {
-                // Filter by component type
-                let typeLower:string = component.componentType.toLowerCase();
-                let typeFirstCapital:string = typeLower.charAt(0).toUpperCase() + typeLower.slice(1);
-                if (filter.selectedComponentTypes.indexOf(typeFirstCapital) !== -1) {
-                    filteredTypes.push(component);
-                }
-
-                // Filter by resource sub type, only in case the resource checkbox was not selected (because in this case we already added all the components in above section).
-                if (component.isResource() && filter.selectedComponentTypes.indexOf("Resource") === -1 && filter.selectedResourceSubTypes.length > 0) {
-                    //filteredComponents.pop(); // Remove the last inserted component.
-                    let resource:Resource = <Resource>component;
-                    if (filter.selectedResourceSubTypes.indexOf(resource.getComponentSubType()) !== -1) {
-                        filteredTypes.push(component);
-                    }
-                }
-            });
-            filteredComponents = filteredTypes;
-        }
-
-        // filter by categories & subcategories & groupings
-        // --------------------------------------------------------------------------
-        if (filter.selectedCategoriesModel && filter.selectedCategoriesModel.length > 0) {
-            let filteredCategories = [];
-            angular.forEach(filteredComponents, (component:Component):void => {
-                let componentCategory = component.categoryNormalizedName +
-                    ((component.subCategoryNormalizedName) ? '.' + component.subCategoryNormalizedName : '');
-                if (component.componentType === ComponentType.RESOURCE) {
-                    componentCategory = 'resourceNewCategory.' + componentCategory;
-                } else if (component.componentType === ComponentType.SERVICE) {
-                    componentCategory = 'serviceNewCategory.' + componentCategory;
-                }
-                if (filter.selectedCategoriesModel.indexOf(componentCategory) !== -1) {
-                    filteredCategories.push(component);
-                }
-            });
-            filteredComponents = filteredCategories;
-        }
-
-        // filter by statuses
-        // --------------------------------------------------------------------------
-        if (filter.selectedStatuses && filter.selectedStatuses.length > 0) {
-
-            let filteredStatuses = [];
-            angular.forEach(filteredComponents, (component:Component):void => {
-                if (filter.selectedStatuses.indexOf(component.lifecycleState) > -1) {
-                    filteredStatuses.push(component);
-                }
-                //if status DISTRIBUTED && CERTIFIED are selected the component will added in  CERTIFIED status , not need to add twice
-                if (filter.selectedStatuses.indexOf('DISTRIBUTED') > -1 && !(filter.selectedStatuses.indexOf('CERTIFIED') > -1)) {
-                    if (component.distributionStatus && component.distributionStatus.indexOf('DISTRIBUTED') > -1 && component.lifecycleState.indexOf('CERTIFIED') > -1) {
-                        filteredStatuses.push(component);
-                    }
-                }
-            });
-            filteredComponents = filteredStatuses;
-        }
-
-        // filter by statuses and distributed
-        // --------------------------------------------------------------------------
-        if (filter.distributed != undefined && filter.distributed.length > 0) {
-            let filterDistributed:Array<any> = filter.distributed;
-            let filteredDistributed = [];
-            angular.forEach(filteredComponents, (entity) => {
-                filterDistributed.forEach((distribute) => {
-                    let distributeItem = distribute.split(',');
-                    distributeItem.forEach((item) => {
-                        if (item !== undefined && entity.distributionStatus === item) {
-                            filteredDistributed.push(entity);
-                        }
-                    })
-                });
-            });
-            filteredComponents = filteredDistributed;
-        }
-
-        // filter by model
-        // --------------------------------------------------------------------------
-        if (filter.selectedModels && filter.selectedModels.length > 0) {
-            let filteredModels = [];
-            let allSelectedModels =  [].concat.apply([], filter.selectedModels);
-            angular.forEach(filteredComponents, (component:Component):void => {
-                if (component.model && allSelectedModels.indexOf(component.model) > -1) {
-                    filteredModels.push(component);
-                } else if (!component.model && allSelectedModels.indexOf(DEFAULT_MODEL_NAME) > -1) {
-                       filteredModels.push(component);
-                }
-            });
-            filteredComponents = filteredModels;
-        }
-
-        // filter by search
-        // --------------------------------------------------------------------------
-        if (filter.search != undefined) {
-            Object.keys(filter.search).forEach((searchKey) => {
-                let searchVal = filter.search[searchKey];
-                if (searchVal) {
-                    searchVal = searchVal.toLowerCase();
-                    filteredComponents = filteredComponents.filter((component:Component) =>
-                        component[searchKey].toLowerCase().indexOf(searchVal) !== -1);
-                }
-            });
-        }
-
-        return filteredComponents;
-    }
-
-    public transform(components:Array<Component>, filter:IEntityFilterObject) {
-        return EntityFilterPipe.transform(components, filter);
-    }
-}
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * SDC\r
+ * ================================================================================\r
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+import {Pipe, PipeTransform} from "@angular/core";\r
+import {Component, Resource} from "app/models";\r
+import {ComponentType, DEFAULT_MODEL_NAME} from "app/utils/constants";\r
+\r
+export interface ISearchFilter {\r
+    [key:string]: string;\r
+}\r
+\r
+const angular = require('angular');\r
+\r
+export interface IEntityFilterObject {\r
+    // Types\r
+    selectedComponentTypes?:Array<string>;\r
+    selectedResourceSubTypes?:Array<string>;\r
+    // Categories\r
+    selectedCategoriesModel?:Array<string>;\r
+    // Statuses\r
+    selectedStatuses?:Array<string>;\r
+    // Models\r
+    selectedModels?:Array<string>;\r
+    // distributed\r
+    distributed?:Array<string>;\r
+    // search\r
+    search?:ISearchFilter;\r
+    \r
+}\r
+\r
+@Pipe({name: 'entity-filter'})\r
+export class EntityFilterPipe implements PipeTransform{\r
+    constructor() {\r
+    }\r
+\r
+    public static transform(components:Array<Component>, filter:IEntityFilterObject) {\r
+        let filteredComponents:Array<Component> = components;\r
+\r
+        // filter by type\r
+        // --------------------------------------------------------------------------\r
+        if ((filter.selectedComponentTypes && filter.selectedComponentTypes.length > 0) || (filter.selectedResourceSubTypes && filter.selectedResourceSubTypes.length > 0)) {\r
+            let filteredTypes = [];\r
+            angular.forEach(components, (component:Component):void => {\r
+                // Filter by component type\r
+                let typeLower:string = component.componentType.toLowerCase();\r
+                let typeFirstCapital:string = typeLower.charAt(0).toUpperCase() + typeLower.slice(1);\r
+                if (filter.selectedComponentTypes.indexOf(typeFirstCapital) !== -1) {\r
+                    filteredTypes.push(component);\r
+                }\r
+\r
+                // Filter by resource sub type, only in case the resource checkbox was not selected (because in this case we already added all the components in above section).\r
+                if (component.isResource() && filter.selectedComponentTypes.indexOf("Resource") === -1 && filter.selectedResourceSubTypes.length > 0) {\r
+                    //filteredComponents.pop(); // Remove the last inserted component.\r
+                    let resource:Resource = <Resource>component;\r
+                    if (filter.selectedResourceSubTypes.indexOf(resource.getComponentSubType()) !== -1) {\r
+                        filteredTypes.push(component);\r
+                    }\r
+                }\r
+            });\r
+            filteredComponents = filteredTypes;\r
+        }\r
+\r
+        // filter by categories & subcategories & groupings\r
+        // --------------------------------------------------------------------------\r
+        if (filter.selectedCategoriesModel && filter.selectedCategoriesModel.length > 0) {\r
+            let filteredCategories = [];\r
+            angular.forEach(filteredComponents, (component:Component):void => {\r
+                let componentCategory = component.categoryNormalizedName +\r
+                    ((component.subCategoryNormalizedName) ? '.' + component.subCategoryNormalizedName : '');\r
+                if (component.componentType === ComponentType.RESOURCE) {\r
+                    componentCategory = 'resourceNewCategory.' + componentCategory;\r
+                } else if (component.componentType === ComponentType.SERVICE) {\r
+                    componentCategory = 'serviceNewCategory.' + componentCategory;\r
+                }\r
+                if (filter.selectedCategoriesModel.indexOf(componentCategory) !== -1) {\r
+                    filteredCategories.push(component);\r
+                }\r
+            });\r
+            filteredComponents = filteredCategories;\r
+        }\r
+\r
+        // filter by statuses\r
+        // --------------------------------------------------------------------------\r
+        if (filter.selectedStatuses && filter.selectedStatuses.length > 0) {\r
+\r
+            let filteredStatuses = [];\r
+            angular.forEach(filteredComponents, (component:Component):void => {\r
+                if (filter.selectedStatuses.indexOf(component.lifecycleState) > -1) {\r
+                    filteredStatuses.push(component);\r
+                }\r
+                //if status DISTRIBUTED && CERTIFIED are selected the component will added in  CERTIFIED status , not need to add twice\r
+                if (filter.selectedStatuses.indexOf('DISTRIBUTED') > -1 && !(filter.selectedStatuses.indexOf('CERTIFIED') > -1)) {\r
+                    if (component.distributionStatus && component.distributionStatus.indexOf('DISTRIBUTED') > -1 && component.lifecycleState.indexOf('CERTIFIED') > -1) {\r
+                        filteredStatuses.push(component);\r
+                    }\r
+                }\r
+            });\r
+            filteredComponents = filteredStatuses;\r
+        }\r
+\r
+        // filter by statuses and distributed\r
+        // --------------------------------------------------------------------------\r
+        if (filter.distributed != undefined && filter.distributed.length > 0) {\r
+            let filterDistributed:Array<any> = filter.distributed;\r
+            let filteredDistributed = [];\r
+            angular.forEach(filteredComponents, (entity) => {\r
+                filterDistributed.forEach((distribute) => {\r
+                    let distributeItem = distribute.split(',');\r
+                    distributeItem.forEach((item) => {\r
+                        if (item !== undefined && entity.distributionStatus === item) {\r
+                            filteredDistributed.push(entity);\r
+                        }\r
+                    })\r
+                });\r
+            });\r
+            filteredComponents = filteredDistributed;\r
+        }\r
+\r
+        // filter by model\r
+        // --------------------------------------------------------------------------\r
+        if (filter.selectedModels && filter.selectedModels.length > 0) {\r
+            let filteredModels = [];\r
+            let allSelectedModels =  [].concat.apply([], filter.selectedModels);\r
+            angular.forEach(filteredComponents, (component:Component):void => {\r
+                if (component.model && allSelectedModels.indexOf(component.model) > -1) {\r
+                    filteredModels.push(component);\r
+                } else if (!component.model && allSelectedModels.indexOf(DEFAULT_MODEL_NAME) > -1) {\r
+                       filteredModels.push(component);\r
+                }\r
+            });\r
+            filteredComponents = filteredModels;\r
+        }\r
+\r
+        // filter by search\r
+        // --------------------------------------------------------------------------\r
+        if (filter.search != undefined) {\r
+            Object.keys(filter.search).forEach((searchKey) => {\r
+                let searchVal = filter.search[searchKey];\r
+                if (searchVal) {\r
+                    searchVal = searchVal.toLowerCase();\r
+                    filteredComponents = filteredComponents.filter((component:Component) =>\r
+                        component[searchKey].toLowerCase().indexOf(searchVal) !== -1);\r
+                }\r
+            });\r
+        }\r
+\r
+        return filteredComponents;\r
+    }\r
+\r
+    public transform(components:Array<Component>, filter:IEntityFilterObject) {\r
+        return EntityFilterPipe.transform(components, filter);\r
+    }\r
+}\r