CSIT Fix for SDC-2585
[sdc.git] / catalog-dao / src / test / java / org / openecomp / sdc / be / resources / ArtifactDaoTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.resources;
22
23 import fj.data.Either;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.openecomp.sdc.be.config.ConfigurationManager;
27 import org.openecomp.sdc.be.dao.api.IGenericSearchDAO;
28 import org.openecomp.sdc.be.dao.api.ResourceUploadStatus;
29 import org.openecomp.sdc.be.dao.es.ElasticSearchClient;
30 import org.openecomp.sdc.be.resources.api.IResourceUploader;
31 import org.openecomp.sdc.be.resources.data.ESArtifactData;
32 import org.openecomp.sdc.be.utils.DAOConfDependentTest;
33 import org.springframework.test.context.ContextConfiguration;
34 import org.springframework.test.context.TestExecutionListeners;
35 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
36 import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
37 import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
38 import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
39
40 import javax.annotation.Resource;
41
42 import static org.junit.Assert.*;
43
44 @RunWith(SpringJUnit4ClassRunner.class)
45 @ContextConfiguration("classpath:application-context-test.xml")
46 @TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class,
47                 DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class }) // ,
48                                                                                                                                                                                                 // CassandraUnitTestExecutionListener.class})
49 public class ArtifactDaoTest extends DAOConfDependentTest {
50         private static final String TEST_IMAGES_DIRECTORY = "src/test/resources/images";
51
52         @Resource
53         ElasticSearchClient esclient;
54
55         @Resource(name = "resource-upload")
56         private IResourceUploader daoUploader;
57         ESArtifactData arData;
58
59         @Resource(name = "resource-dao")
60         private IGenericSearchDAO resourceDAO;
61
62         private String nodeTypeVersion = "1.0.0";
63
64         private static ConfigurationManager configurationManager;
65
66         @Test
67         public void testSaveNewArtifact() {
68                 // daoUploader = new ArtifactUploader(artifactDAO);
69                 if (daoUploader == null) {
70                         assertTrue(false);
71                 }
72                 String strData = "qweqwqweqw34e4wrwer";
73
74                 String myNodeType = "MyNewNodeType";
75
76                 ESArtifactData arData = new ESArtifactData("artifactNewMarina11", strData.getBytes());
77
78                 ResourceUploadStatus status = daoUploader.saveArtifact(arData, true);
79
80                 assertEquals(status, ResourceUploadStatus.OK);
81
82                 daoUploader.deleteArtifact(arData.getId());
83
84         }
85
86         @Test
87         public void testGetArtifact() {
88
89                 String myNodeType = "MyNodeType";
90
91                 // resourceDAO.save(indexedNodeType);
92                 ESArtifactData arData = getArtifactData(myNodeType, nodeTypeVersion);
93
94                 ESArtifactData getData = null;
95                 Either<ESArtifactData, ResourceUploadStatus> getArtifactStatus = daoUploader
96                                 .getArtifact(myNodeType + "- dassasd" + ":" + nodeTypeVersion + ":updatedArtifact");
97                 if (getArtifactStatus.isRight()) {
98                         daoUploader.saveArtifact(arData, true);
99                         getArtifactStatus = daoUploader.getArtifact(arData.getId());
100                 }
101                 assertNotNull(getArtifactStatus.left().value());
102
103         }
104
105
106         @Test
107         public void testUpdateArtifact() {
108                 if (daoUploader == null) {
109                         fail();
110                 }
111                 ResourceUploadStatus status = ResourceUploadStatus.OK;
112
113                 String myNodeType = "MyUpdatedNodeType";
114
115                 ESArtifactData arData = getArtifactData(myNodeType, nodeTypeVersion);
116                 Either<ESArtifactData, ResourceUploadStatus> getArtifactStatus = daoUploader.getArtifact(arData.getId());
117
118                 if (!getArtifactStatus.isLeft())
119                         status = daoUploader.saveArtifact(arData, false);
120
121                 String payload1 = "new payloadjfdsgh";
122                 arData.setDataAsArray(payload1.getBytes());
123
124                 status = daoUploader.updateArtifact(arData);
125
126                 assertEquals(status, ResourceUploadStatus.OK);
127         }
128
129         private ESArtifactData getArtifactData(String componentName, String componentVersion) {
130                 String strData = "qweqwqweqw34e4wrwer";
131
132         return new ESArtifactData("updatedArtifact", strData.getBytes());
133         }
134 }