0d38decdf6dafa86117f43d54466666f887d98f9
[ccsdk/cds.git] /
1 /*
2  * Copyright (C) 2019 Bell Canada. All rights reserved.
3  *
4  * NOTICE:  All the intellectual and technical concepts contained herein are
5  * proprietary to Bell Canada and are protected by trade secret or copyright law.
6  * Unauthorized copying of this file, via any medium is strictly prohibited.
7  */
8
9 package org.onap.ccsdk.cds.cdssdclistener.handler;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import com.google.protobuf.ByteString;
14 import io.grpc.ManagedChannel;
15 import io.grpc.inprocess.InProcessChannelBuilder;
16 import io.grpc.inprocess.InProcessServerBuilder;
17 import io.grpc.stub.StreamObserver;
18 import io.grpc.testing.GrpcCleanupRule;
19 import java.io.File;
20 import java.io.IOException;
21 import java.nio.file.Paths;
22 import org.apache.commons.io.FileUtils;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.onap.ccsdk.cds.cdssdclistener.client.CdsSdcListenerAuthClientInterceptor;
28 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status;
29 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementOutput;
30 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementServiceGrpc.BluePrintManagementServiceImplBase;
31 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintUploadInput;
32 import org.onap.ccsdk.cds.controllerblueprints.management.api.FileChunk;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.boot.context.properties.EnableConfigurationProperties;
35 import org.springframework.boot.test.context.SpringBootTest;
36 import org.springframework.test.context.junit4.SpringRunner;
37
38
39 @RunWith(SpringRunner.class)
40 @EnableConfigurationProperties({BluePrintProcesssorHandler.class, CdsSdcListenerAuthClientInterceptor.class})
41 @SpringBootTest(classes = {BluePrintProcessorHandlerTest.class})
42 public class BluePrintProcessorHandlerTest {
43
44     @Autowired
45     private BluePrintProcesssorHandler bluePrintProcesssorHandler;
46
47     @Autowired
48     private CdsSdcListenerAuthClientInterceptor cdsSdcListenerAuthClientInterceptor;
49
50     @Rule
51     public GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
52
53     private static final String CBA_ARCHIVE = "src/test/resources/testcba.zip";
54     private static final String SUCCESS_MSG = "Successfully uploaded CBA";
55     private static final int SUCCESS_CODE = 200;
56     private ManagedChannel channel;
57
58     @Before
59     public void setUp() throws IOException {
60         final BluePrintManagementServiceImplBase serviceImplBase = new BluePrintManagementServiceImplBase() {
61             @Override
62             public void uploadBlueprint(BluePrintUploadInput request,
63                 StreamObserver<BluePrintManagementOutput> responseObserver) {
64                 responseObserver.onNext(getBluePrintManagementOutput());
65                 responseObserver.onCompleted();
66             }
67         };
68
69         // Generate server name.
70         String serverName = InProcessServerBuilder.generateName();
71
72         // Create a server, add service, start, and register.
73         grpcCleanup.register(
74             InProcessServerBuilder.forName(serverName).addService(serviceImplBase).directExecutor().build().start());
75
76         // Create a client channel.
77         channel = grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build());
78     }
79
80     @Test
81     public void testApplicationEndPointSucess() throws IOException {
82         // Arrange
83         BluePrintUploadInput request = generateRequest();
84
85         // Act
86         Status output = bluePrintProcesssorHandler.sendRequest(request, channel);
87
88         // Verify
89         assertEquals(SUCCESS_CODE, output.getCode());
90         assertTrue(output.getMessage().contains(SUCCESS_MSG));
91     }
92
93     private BluePrintUploadInput generateRequest() throws IOException {
94         File file = Paths.get(CBA_ARCHIVE).toFile();
95         byte[] bytes = FileUtils.readFileToByteArray(file);
96         FileChunk fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(bytes)).build();
97
98         return BluePrintUploadInput.newBuilder().setFileChunk(fileChunk).build();
99     }
100
101     private BluePrintManagementOutput getBluePrintManagementOutput() {
102         return BluePrintManagementOutput.newBuilder()
103                 .setStatus(Status.newBuilder().setMessage(SUCCESS_MSG).setCode(200).build())
104                 .build();
105     }
106 }