From a60b092dc80fbac6dc5e4434c22387ba1672d36c Mon Sep 17 00:00:00 2001 From: eHanan Date: Tue, 16 Jul 2019 15:27:06 +0000 Subject: [PATCH] Simulate Request for SDC to get CSAR Change-Id: I1b711b3eee8623d79ac9e00307ce48d6207d64d6 Issue-ID: SO-1951 Signed-off-by: eHanan --- .../integration-etsi-testing/so-simulators/pom.xml | 2 +- .../so-simulators/sdc-simulator/pom.xml | 2 +- .../java/org/onap/so/sdc/simulator/Constant.java | 18 +++- .../so/sdc/simulator/SdcSimulatorApplication.java | 3 +- .../so/sdc/simulator/SdcSimulatorController.java | 30 ++++++- .../sdc/simulator/providers/ResourceProvider.java | 36 ++++++++ .../simulator/providers/ResourceProviderImpl.java | 88 ++++++++++++++++++ .../src/main/resources/csar/default_csar_file.csar | Bin 0 -> 3982 bytes .../sdc/simulator/SdcSimulatorControllerTest.java | 56 +++++++++--- .../providers/ResourceProviderImplTest.java | 98 +++++++++++++++++++++ 10 files changed, 313 insertions(+), 20 deletions(-) create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/providers/ResourceProvider.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/providers/ResourceProviderImpl.java create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/resources/csar/default_csar_file.csar create mode 100644 plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/providers/ResourceProviderImplTest.java diff --git a/plans/so/integration-etsi-testing/so-simulators/pom.xml b/plans/so/integration-etsi-testing/so-simulators/pom.xml index feca047f..5e582c89 100644 --- a/plans/so/integration-etsi-testing/so-simulators/pom.xml +++ b/plans/so/integration-etsi-testing/so-simulators/pom.xml @@ -44,7 +44,7 @@ org.springframework.boot spring-boot-starter-tomcat - provided + compile org.springframework.boot diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/pom.xml b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/pom.xml index 6a1baccf..397d4a7c 100644 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/pom.xml +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/pom.xml @@ -1,5 +1,5 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> so-simulators org.onap.so.simulators diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/Constant.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/Constant.java index c4e9c468..d03bb7bf 100644 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/Constant.java +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/Constant.java @@ -20,7 +20,21 @@ package org.onap.so.sdc.simulator; */ public class Constant { - public static final String BASE_URL = "/sdc/simulator/v1"; + public static final String BASE_URL = "/simulator/sdc/v1/catalog"; - private Constant() {} + public static final String HEALTHY = "healthy"; + + public static final String DEFAULT_CSAR_NAME = "default_csar_file"; + + public static final String DOT = "."; + + public static final String DOT_CSAR = DOT + "csar"; + + public static final String DEFAULT_CSAR_NAME_WITH_EXT = DEFAULT_CSAR_NAME + DOT_CSAR; + + public static final String DEFAULT_CSAR_PATH = "/csar/" + DEFAULT_CSAR_NAME_WITH_EXT; + + + private Constant() { + } } diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/SdcSimulatorApplication.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/SdcSimulatorApplication.java index 6bcd04d8..abb183b1 100644 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/SdcSimulatorApplication.java +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/SdcSimulatorApplication.java @@ -27,8 +27,9 @@ import org.springframework.boot.web.servlet.support.SpringBootServletInitializer /** * @author Waqas Ikram (waqas.ikram@est.tech) */ -@SpringBootApplication +@SpringBootApplication(scanBasePackages = {"org.onap"}) public class SdcSimulatorApplication extends SpringBootServletInitializer { + public static void main(final String[] args) { SpringApplication.run(SdcSimulatorApplication.class, args); } diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/SdcSimulatorController.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/SdcSimulatorController.java index 12e179f8..953df6fc 100644 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/SdcSimulatorController.java +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/SdcSimulatorController.java @@ -20,30 +20,52 @@ package org.onap.so.sdc.simulator; +import java.util.Optional; +import javax.ws.rs.core.MediaType; +import org.onap.so.sdc.simulator.providers.ResourceProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; -import javax.ws.rs.core.MediaType; /** * @author Waqas Ikram (waqas.ikram@est.tech) */ @RestController -@RequestMapping(path = Constant.BASE_URL, produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON) +@RequestMapping(path = Constant.BASE_URL) public class SdcSimulatorController { + private ResourceProvider resourceProvider; + + public SdcSimulatorController(@Autowired final ResourceProvider resourceProvider) { + this.resourceProvider = resourceProvider; + } + private static final Logger LOGGER = LoggerFactory.getLogger(SdcSimulatorController.class); - @GetMapping(value = "/healthcheck") + @GetMapping(value = "/healthcheck", produces = MediaType.APPLICATION_JSON) @ResponseStatus(code = HttpStatus.OK) public String healthCheck() { LOGGER.info("Running health check ..."); - return "healthy"; + return Constant.HEALTHY; } + @GetMapping(value = "/resources/{csarId}/toscaModel", produces = MediaType.APPLICATION_OCTET_STREAM) + public ResponseEntity getCsar(@PathVariable("csarId") final String csarId) { + LOGGER.info("Running getCsar for {} ...", csarId); + final Optional resource = resourceProvider.getResource(csarId); + if (resource.isPresent()) { + return new ResponseEntity<>(resource.get(), HttpStatus.OK); + } + LOGGER.error("Unable to find csar: {}", csarId); + + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } } diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/providers/ResourceProvider.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/providers/ResourceProvider.java new file mode 100644 index 00000000..83994b4b --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/providers/ResourceProvider.java @@ -0,0 +1,36 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.so.sdc.simulator.providers; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Optional; + +/** + * @author Eoin Hanan (eoin.hanan@est.tech) + */ +public interface ResourceProvider { + + Optional getResource(final String csarId); + + Optional getInputStream(final String csarId) throws IOException; + +} diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/providers/ResourceProviderImpl.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/providers/ResourceProviderImpl.java new file mode 100644 index 00000000..302dcb69 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/java/org/onap/so/sdc/simulator/providers/ResourceProviderImpl.java @@ -0,0 +1,88 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.so.sdc.simulator.providers; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Optional; +import org.onap.so.sdc.simulator.Constant; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.ClassPathResource; +import org.springframework.stereotype.Service; +import org.springframework.util.StreamUtils; + +/** + * @author Eoin Hanan (eoin.hanan@est.tech) + */ +@Service +public class ResourceProviderImpl implements ResourceProvider { + + private static final Logger LOGGER = LoggerFactory.getLogger(ResourceProvider.class); + + private final String resourceLocation; + + public ResourceProviderImpl(@Value("${sdc.resource.location:/app/csars/}") final String resourceLocation) { + this.resourceLocation = resourceLocation; + } + + @Override + public Optional getResource(final String csarId) { + try { + final Optional optionalInputStream = getInputStream(csarId); + if (optionalInputStream.isPresent()) { + return Optional.of(StreamUtils.copyToByteArray(optionalInputStream.get())); + } + } catch (final IOException ioException) { + LOGGER.warn("Unable to create file stream ...", ioException); + } + + return Optional.empty(); + } + + @Override + public Optional getInputStream(final String csarId) throws IOException { + final Path filePath = Paths.get(resourceLocation, csarId + ".csar"); + if (Files.exists(filePath)) { + return Optional.of(Files.newInputStream(filePath)); + } + + LOGGER.info("Couldn't find file on file system '{}', will return default csar", filePath); + final ClassPathResource classPathResource = new ClassPathResource(getDefaultCsarPath(), this.getClass()); + if (classPathResource.exists()) { + return Optional.of(classPathResource.getInputStream()); + } + + LOGGER.error("Couldn't find default csar in classpath ...."); + return Optional.empty(); + } + + /* + * Used in test + */ + String getDefaultCsarPath() { + return Constant.DEFAULT_CSAR_PATH; + } +} \ No newline at end of file diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/resources/csar/default_csar_file.csar b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/main/resources/csar/default_csar_file.csar new file mode 100644 index 0000000000000000000000000000000000000000..63b70ec629632ab9eef4d66a7a23261ec3575ff1 GIT binary patch literal 3982 zcmb7G2{fDO8V(5}Xxdt8>6vP+wJVD0=0_|IwbwXm3BpwpDhRbyBI?xDjytVUwY0Ts zIa-XJw#YEy+FEYgbU~*oouMtpQb&9LpXMeq;_~Kv|Ce);|9QUm``+(;9!D%3Aq9h? zF8brC)i2*3D3~G)h=>dh#Q8^(blqGfU?P(v+Fn!Ofaa(eF_-{i5)Ol{J{HrukO1|P_5l;Eir4@HUH7}ng<6oZ+XSi>#!~H;h|x% z1UxZv8_QMYS*|=kNE-~`x`x{TF0mOL(9CzKBb4>qpui8;!PN%iyaxQ1rR}v04rtyG z7zsndM&lX(JN>6%1*mKe?CRiR0q79$k+{{upoM%nv%K*U+Fu5CQ1zrN7mi_c9W|KK zHsyl|<=dSJbe~Z36D2ERjjhTB=N1lysTxF{b}tZ$7?%y)>271HRTks1_j;Y%*PGYu z)WQ+ubDq-1(Z0e5D+`48=?#o9(%dgm$x3*S4wCl>(xzJBR7qAWL*0+&7j^DcePze! z{;T6B6jr9^I>rzM9;);cEjU2;d4i0b5q}PfV z+79l0K8}RJ)HmM_QV@x#O(5WPVsV7f)g6I0n>~0oz0!?IWSZY|v+%tBUDNE*`)LZm_*_j=1`>DDxF(I{S@n?ik9rj_h$=>hsO${te36gz? z5@%brU#ded|2otod!{A;?=;bU*8BI+PODP2QZ+$>UjR$Ks(DhrBo9!qVX56IDBGX7 z&pNsPW>8|6K}@~z@f7zO_5AxW`i&kf%V8b@P&nrq+LxwGQbR~Ft;ZLs+tT4tA2~l$nOYVlBvb_i zi)3QjGi02zl|~b&#qe^)!J-GhYXDDe>d*GE9B&M?Ma!ONnKY7~I}T~RBqOFOA~T;a1CR zuVSU?RBV{|@Kr|NqxVk;4FFoHD9TDlldS03{}c5!S#hj^T4O7Yix<9NKdyItLD8q3 zp1L$8*k(s0stz8=rRzPQ+>JGq*fVyt;F*zKLDfh7^pbZ;%JO;13&o#y?X>q9*o`Ul zPLDBrlvQ<;xmz-uK{I#ppc~NaT>RD3kS7gOYLDOl8Hqw)?J;jEdGTjQ1J%28@dN8b za-RuONWB@wDpiBQ&4N^1ST{WK(5Xswr^|gU z4$BX9466vYjpYQsyJ9T!V0O+jn8G~hn4)>x-mDo;7YhxADJPvN^erF6kbguZ7D`NZ zCK1y|Dh+ScMpk#syphwIc)TRrId6q?PV!=Dlok}4Ut}`FQLW1qrVQets4~i9dGUh#K~hAOh@BsfsX8Y;Ppn zk=jPwEKrG8-N@sThD}*-NND1u&#tG`V;5e@UqaourWP05=n|YSg_a_^G|z_3G8d=M zsL;F0M;behB}vb;l&hCw^Zuv^+)=I{S6bKiJ+{Aa>7HKu>+xE{D|hbs#22;ga3K}F zlKi~V=qYmD%-N$Y=%UOMnfJpE7}qvCAon*>*9{Vq5* zr10o{{J&8In{;V+JI#O*+;5?7pQEkP+Oy9p%qW$|DaSgzrOnNebH1;OZam+;paxxf zr(lNX1yH)6tIbg^U7n_`$nVmhw(_yFwZNQm`KB^GV9V|U2Q>3sb`H8W54tj6bj&GB z2j}$+T_AEdIG~v?Lvze}aS#U?Eb)QC#9&BnziD{;Gi|S5!2!*Dk$AlT8bn%c`P%ki z%MZXs;y7BksUd)R=}RtYsRz5P+~2`?!i6ofm20p|7N-IiGe|>=OXUu*ZAiNCk1aCr5t`XY@{AV z&rJ75O%MrL*Dh;y3X<$EizO6?5&GYwp=yf9?>5W;6N8fDNCZJ$K-^x6(fV6`>+di!gf^`01vqAqHN+uHZ^_Gk$(q?E4x zAeaepiy#?GN=Zf_8^q{{6%dqy^LKaPUshuUfTrzuJ^r|m+hfNHV6zT)yWWq(vXT4a zu;g#TvQ;n0;o_}*$PKb%1^B#-=Z literal 0 HcmV?d00001 diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/SdcSimulatorControllerTest.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/SdcSimulatorControllerTest.java index e0d44e93..5c7a77a8 100644 --- a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/SdcSimulatorControllerTest.java +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/SdcSimulatorControllerTest.java @@ -21,17 +21,23 @@ package org.onap.so.sdc.simulator; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Optional; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.onap.so.sdc.simulator.providers.ResourceProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.server.LocalServerPort; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.MediaType; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -42,8 +48,11 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ActiveProfiles("test") @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@Configuration public class SdcSimulatorControllerTest { + private static final String MOCKER_SDC_CONTROLLER_BEAN = "mockResourceProvider"; + @LocalServerPort private int port; @@ -51,16 +60,41 @@ public class SdcSimulatorControllerTest { private TestRestTemplate restTemplate; @Test - public void testHealthCheck() { - final HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON); + public void test_healthCheck_matchContent() { + final String url = getBaseUrl() + "/healthcheck"; + final ResponseEntity object = restTemplate.getForEntity(url, String.class); + + assertEquals(Constant.HEALTHY, object.getBody()); + + } + + @Test + public void test_getCsar_validCsarId_matchContent() { + + final String url = getBaseUrl() + "/resources/" + Constant.DEFAULT_CSAR_NAME + "/toscaModel"; - final HttpEntity request = new HttpEntity<>(headers); - final String url = "http://localhost:" + port + Constant.BASE_URL + "/healthcheck"; - final ResponseEntity object = restTemplate.exchange(url, HttpMethod.GET, request, String.class); + final ResponseEntity response = restTemplate.getForEntity(url, byte[].class); - assertEquals("healthy", object.getBody()); + assertTrue(response.hasBody()); + assertEquals(3982, response.getBody().length); + + assertEquals(HttpStatus.OK, response.getStatusCode()); + } + + @Test + public void test_getCsar_invalidCsar_internalServerError() { + final ResourceProvider mockedResourceProvider = Mockito.mock(ResourceProvider.class); + Mockito.when(mockedResourceProvider.getResource(Mockito.anyString())).thenReturn(Optional.empty()); + final SdcSimulatorController objUnderTest = new SdcSimulatorController(mockedResourceProvider); + + final ResponseEntity response = objUnderTest.getCsar(Constant.DEFAULT_CSAR_NAME); + + assertFalse(response.hasBody()); + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + } + private String getBaseUrl() { + return "http://localhost:" + port + Constant.BASE_URL; } } diff --git a/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/providers/ResourceProviderImplTest.java b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/providers/ResourceProviderImplTest.java new file mode 100644 index 00000000..a06d1e71 --- /dev/null +++ b/plans/so/integration-etsi-testing/so-simulators/sdc-simulator/src/test/java/org/onap/so/sdc/simulator/providers/ResourceProviderImplTest.java @@ -0,0 +1,98 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.so.sdc.simulator.providers; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertFalse; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.onap.so.sdc.simulator.Constant; +import org.springframework.core.io.ClassPathResource; +import org.springframework.util.StreamUtils; + +/** + * @author Waqas Ikram (waqas.ikram@est.tech) + * @author Eoin Hanan (eoin.hanan@est.tech) + */ +public class ResourceProviderImplTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private static final String DUMMY_CONTENT = "Hell world"; + + @Test + public void test_getResource_withValidPath_matchContent() throws IOException { + final File folder = temporaryFolder.newFolder(); + final Path file = Files.createFile(folder.toPath().resolve("empty.csar")); + + Files.write(file, DUMMY_CONTENT.getBytes()); + + final ResourceProviderImpl objUnderTest = new ResourceProviderImpl(folder.getPath()); + + assertArrayEquals(DUMMY_CONTENT.getBytes(), objUnderTest.getResource("empty").get()); + } + + @Test + public void test_getResource_withoutValidPath_matchContent() throws IOException { + final ClassPathResource classPathResource = new ClassPathResource(Constant.DEFAULT_CSAR_PATH, this.getClass()); + + final byte[] expectedResult = StreamUtils.copyToByteArray(classPathResource.getInputStream()); + + final ResourceProviderImpl objUnderTest = new ResourceProviderImpl(""); + + assertArrayEquals(expectedResult, objUnderTest.getResource(Constant.DEFAULT_CSAR_NAME).get()); + } + + @Test + public void test_getResource_unbleToreadFileFromClasspath_emptyOptional() throws IOException { + + final ResourceProviderImpl objUnderTest = new ResourceProviderImpl("") { + @Override + String getDefaultCsarPath() { + return "/some/dummy/path"; + } + }; + assertFalse(objUnderTest.getResource(Constant.DEFAULT_CSAR_NAME).isPresent()); + + } + + @Test + public void test_getResource_withValidPathAndUnabletoRead_emptyOptional() throws IOException { + final File folder = temporaryFolder.newFolder(); + final Path file = Files.createFile(folder.toPath().resolve("empty.csar")); + + Files.write(file, DUMMY_CONTENT.getBytes()); + file.toFile().setReadable(false); + + final ResourceProviderImpl objUnderTest = new ResourceProviderImpl(folder.getPath()); + + assertFalse(objUnderTest.getResource("empty").isPresent()); + + } + +} \ No newline at end of file -- 2.16.6