Improve mod/genprocessor code coverage
[dcaegen2/platform.git] / mod / runtimeapi / runtime-web / src / test / java / org / onap / dcae / runtime / web / service / GraphServiceImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18 package org.onap.dcae.runtime.web.service;
19
20 import org.onap.dcae.runtime.core.Node;
21 import org.onap.dcae.runtime.web.Helper;
22 import org.onap.dcae.runtime.web.exception.MainGraphAlreadyExistException;
23 import org.onap.dcae.runtime.web.exception.MainGraphNotFoundException;
24 import org.onap.dcae.runtime.web.models.GraphRequest;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.Ignore;
28
29 import java.util.*;
30
31 import static org.junit.Assert.*;
32
33 // TODO: Running into null pointer issue. GraphServiceImpl depends upon FlowGraphParser but that is null.  FlowGraphParser
34 // relies on BlueprintCreator which means blueprint generator.  This needs to be mocked I believe.  Or the fix maybe
35 // with getting Spring to work right (ugh).  Ignore for now.
36 @Ignore
37 public class GraphServiceImplTest {
38
39     private GraphService graphService;
40     private GraphRequest graphRequest;
41
42     @Before
43     public void setUp() throws Exception{
44         graphService = new GraphServiceImpl();
45         graphRequest = new GraphRequest();
46         graphRequest.setId("1234");
47         graphRequest.setName("nifi-main");
48         graphRequest.setDescription("mock graph");
49     }
50
51     @Test
52     public void testInitializeMainFlowGraph() throws Exception{
53         graphService.initializeMainGraph(graphRequest);
54         assertEquals(graphService.getMainGraph().getName(),"nifi-main");
55         assertTrue(graphService.getMainGraph().isMain());
56     }
57
58     @Test
59     public void testThrowConflictIfMainGraphAlreadyExist() throws Exception{
60         graphService.initializeMainGraph(graphRequest);
61         try {
62             graphService.initializeMainGraph(graphRequest);
63             fail("Exception has to be thrown since Main Graph already exists");
64         }catch(MainGraphAlreadyExistException ex){
65             assertEquals("Can not initialize the main graph, it already exists",ex.getMessage());
66         }
67     }
68
69     @Test
70     public void testMainGraphNotFound() throws Exception{
71         try{
72             graphService.getMainGraph();
73             fail("Error should be thrown if the main graph is not initialized");
74         }catch (MainGraphNotFoundException ex){
75         }
76     }
77
78     @Test
79     public void testAddNodesWithEdgeGeneratesBlueprints() throws Exception{
80         /*
81         TODO: FIX
82         //arrange
83         graphService.initializeMainGraph(graphRequest);
84         DistributeGraphRequest distributeGraphRequest = new DistributeGraphRequest();
85         distributeGraphRequest.setActions(Helper.getAddNodesWithEdge());
86         Set<Node> nodes = prepareTestNodes();
87
88         //act
89         Map<String,String> result = graphService.distribute(distributeGraphRequest);
90
91         //assert
92         assertEquals(nodes.size(),graphService.getMainGraph().getNodes().size());
93         //assertArrayEquals(nodes.toArray(),graphService.getMainGraph().getNodes().toArray());
94         assertEquals(Helper.prepareExpectedResult(),result);
95         */
96     }
97
98     private Set<Node> prepareTestNodes() {
99         Set<Node> nodes = new HashSet<Node>();
100         nodes.add(new Node("comp123","HelloWorld",
101                 Helper.loadFileContent("src/test/data/compspecs/componentSpec_hello_world.json")));
102         nodes.add(new Node("comp456","Toolbox",
103                 Helper.loadFileContent("src/test/data/compspecs/componentSpec_New_Toolbox.json")));
104         return nodes;
105     }
106
107 }