aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/asdc/parser/ToscaParserImpl2Test.java
blob: 9f572feb7deecd7a73cf06d5685e9c217d963a9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package org.onap.vid.asdc.parser;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.common.collect.ImmutableList;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.onap.vid.controllers.WebConfig;
import org.onap.vid.model.VfModule;
import org.onap.vid.model.VolumeGroup;
import org.onap.vid.properties.AsdcClientConfiguration;
import org.onap.portalsdk.core.util.SystemProperties;
import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
import org.onap.sdc.toscaparser.api.Group;
import org.onap.sdc.toscaparser.api.NodeTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;

@Test
@ContextConfiguration(classes = { WebConfig.class, AsdcClientConfiguration.class, SystemProperties.class })
@WebAppConfiguration
public class ToscaParserImpl2Test extends AbstractTestNGSpringContextTests {

    private final String myUUID = "myUUID";
    private static final Logger log = Logger.getLogger(ToscaParserImpl2Test.class);

    @Autowired
    private ToscaParserImpl2 toscaParserImpl2;

    @Autowired
    private WebApplicationContext wac;

    @BeforeMethod
    private void verifyWiring() {
        Assert.assertNotNull(wac);
        Assert.assertNotNull(toscaParserImpl2);
    }

    @Test
    public void testGetNFModuleFromVf() throws Exception {
        ISdcCsarHelper csarHelper = getMockedSdcCsarHelper();

        Map<String, VfModule> vfModulesFromVF = toscaParserImpl2.getVfModulesFromVF(csarHelper, myUUID);

        assertThat(vfModulesFromVF, allOf(
                aMapWithSize(2),
                hasKey("withoutVol"),
                hasKey("withVol")
        ));

        verify(csarHelper, only()).getVfModulesByVf(anyString());
    }

    @Test
    public void testGetVolumeGroupsFromVF() throws Exception {
        ISdcCsarHelper csarHelper = getMockedSdcCsarHelper();

        Map<String, VolumeGroup> volumeGroupsFromVF = toscaParserImpl2.getVolumeGroupsFromVF(csarHelper, myUUID);

        assertThat(volumeGroupsFromVF, allOf(
                aMapWithSize(1),
                hasKey("withVol")
        ));

        verify(csarHelper, only()).getVfModulesByVf(anyString());
    }

    private ISdcCsarHelper getMockedSdcCsarHelper() {
        ISdcCsarHelper csarHelper = mock(ISdcCsarHelper.class);

//        ThreadLocalsHolder.setCollector(new ExceptionCollector("c:\\temp\\foo"));

        Group withVol = createMinimalGroup("withVol", true);
        Group withoutVol = createMinimalGroup("withoutVol", false);

        when(csarHelper.getVfModulesByVf(myUUID))
                .thenReturn(ImmutableList.of(withVol, withoutVol));

        return csarHelper;
    }

    private static Group createMinimalGroup(String name, boolean isVolumeGroup) {
        LinkedHashMap<String, Object>
                templates,
                properties,
                metadata,
                customDef,
                vfModule,
                vfModuleProperties,
                volumeGroup;

        templates = new LinkedHashMap<>();
        templates.put("type", "org.onap.groups.VfModule");

        properties = addNewNamedMap(templates, "properties");
        properties.put("volume_group", isVolumeGroup);

        metadata = addNewNamedMap(templates, "metadata");

        ArrayList<NodeTemplate> memberNodes = new ArrayList<>();

        customDef = new LinkedHashMap<>();
        vfModule = addNewNamedMap(customDef, "org.onap.groups.VfModule");
        vfModuleProperties = addNewNamedMap(vfModule, "properties");
//        vfModule.put("derived_from", "tosca.groups.Root");
//        vfModule.put("description", "Grouped all heat resources which are in the same VF Module");

        volumeGroup = addNewNamedMap(vfModuleProperties, "volume_group");
//        volumeGroup.put("description", "volume_group");
        volumeGroup.put("type", "boolean");
        volumeGroup.put("default", false);
        volumeGroup.put("required", true);


        Group group = new Group(
                name,
                templates,
                memberNodes,
                customDef
        );

        try {
            log.info(String.format("Built a group: %s",
                    (new com.fasterxml.jackson.databind.ObjectMapper())
                            .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
                            .writeValueAsString(group)
            ));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        return group;
    }

    private static LinkedHashMap<String, Object> addNewNamedMap(LinkedHashMap<String, Object> root, String key) {
        LinkedHashMap<String, Object> properties = new LinkedHashMap<>();
        root.put(key, properties);
        return properties;
    }

}