aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-core/catalog-mgr/src/test/java/org/openo/commontosca/catalog/wrapper/PackageWrapperTest.java
blob: 38d25e5f14a089222df9a35616daafe21bb1b43e (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
 * Copyright 2016 ZTE Corporation.
 *
 * 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.
 */
package org.openo.commontosca.catalog.wrapper;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openo.commontosca.catalog.CatalogAppConfiguration;
import org.openo.commontosca.catalog.common.Config;
import org.openo.commontosca.catalog.common.HttpServerAddrConfig;
import org.openo.commontosca.catalog.common.HttpServerPathConfig;
import org.openo.commontosca.catalog.common.MsbAddrConfig;
import org.openo.commontosca.catalog.db.dao.DaoManager;
import org.openo.commontosca.catalog.db.entity.PackageData;
import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
import org.openo.commontosca.catalog.db.resource.PackageManager;
import org.openo.commontosca.catalog.db.util.H2DbServer;
import org.openo.commontosca.catalog.db.util.HibernateSession;
import org.openo.commontosca.catalog.entity.EnumOnboardState;
import org.openo.commontosca.catalog.entity.EnumOperationalState;
import org.openo.commontosca.catalog.entity.EnumProcessState;
import org.openo.commontosca.catalog.entity.EnumUsageState;
import org.openo.commontosca.catalog.entity.response.CsarFileUriResponse;
import org.openo.commontosca.catalog.entity.response.PackageMeta;
import org.openo.commontosca.catalog.model.parser.EnumPackageFormat;
import org.openo.commontosca.catalog.model.parser.ModelParserFactory;
import org.openo.commontosca.catalog.model.parser.yaml.zte.ToscaYamlModelParser;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;

import javax.ws.rs.core.Response;

public class PackageWrapperTest {
  private static String resourcePath;
  
  static {
    MsbAddrConfig.setMsbAddress("http://10.74.44.28:80");
    HttpServerAddrConfig.setHttpServerAddress("http://127.0.0.1:8080");
    HttpServerPathConfig.setHttpServerPath("../tomcat/webapps/ROOT/");
    
    CatalogAppConfiguration configuration = new CatalogAppConfiguration();
    Config.setConfigration(configuration);
    Config.getConfigration().setParserType("zte");
    ModelParserFactory.getInstance().put(EnumPackageFormat.TOSCA_YAML, new TestYamlModelParser());
  }
  
  private static PackageManager manager;


  /**
   * startup db session before class.
   * @throws Exception e
   */
  @BeforeClass
  public static void setUpBeforeClass() throws CatalogResourceException {
    H2DbServer.startUp();
    DaoManager.getInstance().setSessionFactory(HibernateSession.init());
    manager = PackageManager.getInstance();
    CatalogAppConfiguration configuration = new CatalogAppConfiguration();
    Config.setConfigration(configuration);
    System.out.println("Set up before class");
  }

  /**
   * create data before test.
   */
  @Before
  public void setUp() throws Exception {
    ArrayList<PackageData> packageList = manager.queryPackage(null, null, null, null, null);
    if (packageList != null && packageList.size() > 0) {
      for (int i = 0; i < packageList.size(); i++) {
        String packageOid = packageList.get(i).getCsarId();
        manager.deletePackage(packageOid);
      }
    }
    ArrayList<PackageData> packageList2 = manager.queryPackage(null, null, null, null, null);
    if (packageList2 != null && packageList2.size() > 0) {
      for (int j = 0; j < packageList2.size(); j++) {
        if (packageList2.get(j).getCsarId().equals("123456")) {
          manager.updatePackage(getPackageData(), "123456");
        } else {
          PackageData packageData = new PackageData();
          packageData = getPackageData();
          manager.addPackage(packageData);
        }
      }
    } else {
      PackageData packageData = new PackageData();
      packageData = getPackageData();
      manager.addPackage(packageData);
    }
  }

  // @Ignore
  @Test
  public void testUploadPackage() throws Exception {
    InputStream ins = null;
    Response result = null;
    Response result1 = null;
    Response result2 = null;
    // PackageData packageData = new PackageData();
    // packageData = getPackageData();

    FormDataContentDisposition fileDetail =
        FormDataContentDisposition.name("fileName").fileName("NanocellGW.csar").build();

    try {
      resourcePath = HibernateSession.class.getResource("/").toURI().getPath();
    } catch (URISyntaxException e1) {
      e1.printStackTrace();
    }
    final String filename = "NanocellGW.csar";
    File packageFile = new File(resourcePath + filename);
    try {
      ins = new FileInputStream(packageFile);
    } catch (FileNotFoundException e2) {
      e2.printStackTrace();
    }
    if (ins != null) {
      try {
        result = PackageWrapper.getInstance().uploadPackage(ins, fileDetail, null);
      } catch (Exception e3) {
        e3.printStackTrace();
      }
    }
    assertNotNull(result);
    assertEquals(200, result.getStatus());
    assertNotNull(result.getEntity());

    try {
      result1 = PackageWrapper.getInstance().uploadPackage(null, fileDetail, null);
    } catch (Exception e4) {
      e4.printStackTrace();
    }
    assertEquals(500, result1.getStatus());

    try {
      result2 = PackageWrapper.getInstance().uploadPackage(ins, null, null);
    } catch (Exception e5) {
      e5.printStackTrace();
    }
    assertEquals(500, result2.getStatus());
  }

  @Test
  public void testQueryPackageById() throws Exception {
    PackageMeta metas = new PackageMeta();
    metas = getPackageMeta();

    Response result = PackageWrapper.getInstance().queryPackageById("123456");
    assertEquals(200, result.getStatus());
    //assertEquals(metas, result.getEntity());
  }

  @Test
  public void testQueryPackageByCond() {
    ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();
    metas = getPackageMetaList();
    System.out.println("Test query package by Id");
    Response result =
        PackageWrapper.getInstance().queryPackageListByCond("NanocellGW", "ZTE", "V1.0", "false",
            "NSAR");
    assertEquals(200, result.getStatus());
    //assertEquals(metas, result.getEntity());
  }

  @Test
  public void testUpdatePackageStatus() {
    System.out.println("Test update package status");
    Response result =
        PackageWrapper.getInstance().updatePackageStatus("123456", "Enabled", "NotInUse", "true",
            "onBoarding", "true");
    assertEquals(200, result.getStatus());
  }

  @Test
  public void testGetCsarFileUri() {
    System.out.println("Test get csar file uri ");
    CsarFileUriResponse expectResult = new CsarFileUriResponse();
    String csarFileUri =
        getDownloadUriHead() + "/NSAR/ZTE/NanocellGW/v1.0/NanocellGW/images/segw.img";
    String localUri = HttpServerPathConfig.getHttpServerPath()
        + "NSAR/ZTE/NanocellGW/v1.0/NanocellGW/images/segw.img";
    File srcDir = new File(localUri);
    String localPath = srcDir.getAbsolutePath().replace("\\", "/");

    expectResult.setDownloadUri(csarFileUri);
    expectResult.setLocalPath(localPath);
    Response result = PackageWrapper.getInstance().getCsarFileUri("123456", "/images/segw.img");
    assertEquals(200, result.getStatus());
    //assertEquals(expectResult, result.getEntity());
  }

  @Test
  public void testDelPackage() {
    System.out.println("Test delete package ");
    Response result = PackageWrapper.getInstance().delPackage("123456");
    assertEquals(204, result.getStatus());
    try {
      Thread.sleep(5000);
    } catch (InterruptedException e1) {
      e1.printStackTrace();
    }
  }

  /**
   * delete data after test.
   */
  @After
  public void tearDown() throws Exception {
    ArrayList<PackageData> packageList = manager.queryPackageByCsarId("123456");
    if (packageList != null && packageList.size() != 0) {
      manager.deletePackage("123456");
    } else {
      return;
    }
    System.out.println("Tear down");
  }

  /**
   * destory db session after class.
   * @throws Exception e
   */
  @AfterClass
  public static void tearDownAfterClass() {
    try {
      HibernateSession.destory();
      DaoManager.getInstance().setDaoNull();
      H2DbServer.shutDown();
    } catch (Exception e1) {
      Assert.fail("Exception" + e1.getMessage());
    }
  }

  private PackageData getPackageData() {
    PackageData packageData = new PackageData();
    packageData.setCsarId("123456");
    packageData.setCreateTime("2016-06-29 03:33:15");
    packageData.setDeletionPending("false");
    packageData.setDownloadUri("/NSAR/ZTE/NanocellGW/v1.0/");
    packageData.setFormat("yml");
    packageData.setModifyTime("2016-06-29 03:33:15");
    packageData.setName("NanocellGW");
    packageData.setOnBoardState("non-onBoarded");
    packageData.setOperationalState("Disabled");
    packageData.setProvider("ZTE");
    packageData.setSize("0.93M");
    packageData.setType("NSAR");
    packageData.setUsageState("InUse");
    packageData.setVersion("V1.0");
    packageData.setProcessState("normal");
    return packageData;
  }

  private ArrayList<PackageMeta> getPackageMetaList() {
    PackageMeta meta = new PackageMeta();
    meta = getPackageMeta();
    ArrayList<PackageMeta> metas = new ArrayList<PackageMeta>();
    metas.add(meta);
    return metas;
  }
  
  private PackageMeta getPackageMeta() {
    PackageMeta meta = new PackageMeta();
    meta.setCreateTime("2016-06-29 03:33:15");
    meta.setCsarId("123456");
    meta.setDeletionPending(false);
    meta.setDownloadUri(getDownloadUriHead() 
        + "/NSAR/ZTE/NanocellGW/v1.0/NanocellGW.csar");
    meta.setFormat("yml");
    meta.setModifyTime("2016-06-29 03:33:15");
    meta.setName("NanocellGW");
    meta.setOperationalState(EnumOperationalState.valueOf("Disabled"));
    meta.setProvider("ZTE");
    meta.setSize("0.93M");
    meta.setType("NSAR");
    meta.setUsageState(EnumUsageState.valueOf("InUse"));
    meta.setVersion("V1.0");
    meta.setOnBoardState(EnumOnboardState.nonOnBoarded.getValue());
    meta.setProcessState(EnumProcessState.valueOf("normal"));
    return meta;
  }
  
  private String getDownloadUriHead() {
    return MsbAddrConfig.getMsbAddress() + "/files/catalog-http";
  }
}