291abee7f97db7b376f2e8e8baff502ace48653f
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. 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.core.model.types;
22
23 import org.apache.commons.io.IOUtils;
24 import org.apache.commons.lang3.StringUtils;
25 import org.junit.Test;
26 import org.openecomp.sdc.common.errors.SdcRuntimeException;
27 import org.openecomp.sdc.versioning.dao.types.Version;
28
29 import java.io.IOException;
30
31 import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSettersExcluding;
32 import static org.hamcrest.MatcherAssert.assertThat;
33 import static org.junit.Assert.assertArrayEquals;
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.assertTrue;
36 import static org.junit.Assert.fail;
37 import static org.mockito.BDDMockito.given;
38 import static org.mockito.Mockito.mock;
39
40
41 public class ServiceArtifactEntityTest {
42
43     private static final byte[] BYTE_ARRAY = new byte[] {0xA, 0xB, 0xC, 0xD};
44
45     @Test
46     public void shouldHaveValidGettersAndSetters() {
47         assertThat(ServiceArtifactEntity.class,
48                 hasValidGettersAndSettersExcluding("entityType", "firstClassCitizenId", "serviceArtifact"));
49     }
50
51     @Test
52     public void shouldReturnNonEmptyEntityType() {
53         ServiceArtifactEntity entity =
54                 new ServiceArtifactEntity();
55         assertTrue(StringUtils.isNoneEmpty(entity.getEntityType()));
56     }
57
58     @Test
59     public void shouldHaveFirstClassCitizenIdEqualToVspId() {
60         ServiceArtifactEntity entity =
61                 new ServiceArtifactEntity(createServiceArtifact());
62         assertEquals(entity.getId(), entity.getFirstClassCitizenId());
63     }
64
65     @Test
66     public void serviceArtifactGetterShouldReturnCorrectData() throws IOException {
67         ServiceArtifact serviceArtifact = createServiceArtifact();
68         ServiceArtifactEntity entity =
69                 new ServiceArtifactEntity(serviceArtifact);
70
71         ServiceArtifact actual = entity.getServiceArtifact();
72
73         assertEquals(serviceArtifact.getVspId(), actual.getVspId());
74         assertEquals(serviceArtifact.getVersion(), actual.getVersion());
75         assertEquals(serviceArtifact.getName(), actual.getName());
76         assertArrayEquals(IOUtils.toByteArray(serviceArtifact.getContent()), IOUtils.toByteArray(actual.getContent()));
77     }
78
79     @Test(expected = SdcRuntimeException.class)
80     public void shouldFailOnNullContentBytesSupplied() {
81         ServiceArtifact serviceArtifactMock = mock(ServiceArtifact.class);
82         given(serviceArtifactMock.getContent()).willAnswer(invocation -> { throw new IOException("Test exception"); } );
83         ServiceArtifactEntity entity =
84                 new ServiceArtifactEntity(serviceArtifactMock);
85     }
86
87     private static ServiceArtifact createServiceArtifact() {
88         ServiceArtifact serviceArtifact = new ServiceArtifact();
89         serviceArtifact.setVspId("someIdd");
90         serviceArtifact.setVersion(new Version("best"));
91         serviceArtifact.setName("name");
92         serviceArtifact.setContentData(BYTE_ARRAY);
93         return serviceArtifact;
94     }
95 }